Thermometer in Java with JFreeChart

0 comentarios
Definitely one of the most interesting componentes of JFreeChart packet is the Thermometer. As part of my thesis I need to use that but when I tried it I managed to use it. It isn't very easy to find the way to use a thermometer component and If you want to get the source code, you have to purchase the JFreeChart Developer Guide.


Much people haven't got a clue how to use it but in this post I'm going to show how we can use the thermometer en our projects, so they are going to look much better.

I haven't read the licence and I can explain nothing about that, but looking for some examples on the net I found a page where the autor show the JFreeChart Demo source code decompiled by him. I supose the decompiling of jfreechart is not allowed, but really is well worth checking it.

Here is the source code:
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package com.rolandopalermo.telecontrol.test;

/**
 *
 * @author Rolando Palermo
 */
import java.awt.*;
import javax.swing.JPanel;
import javax.swing.JSlider;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.ThermometerPlot;
import org.jfree.data.general.DefaultValueDataset;
import org.jfree.data.general.ValueDataset;
import org.jfree.ui.ApplicationFrame;
import org.jfree.ui.RectangleInsets;


public class ThermometerDemo1 extends ApplicationFrame {
    
    static class ContentPanel extends JPanel implements ChangeListener {
        JSlider slider;
        DefaultValueDataset dataset;
        
        private static JFreeChart createChart(ValueDataset valuedataset) {
            ThermometerPlot thermometerplot = new ThermometerPlot(valuedataset);
            JFreeChart jfreechart = new JFreeChart("Thermometer Demo 1", JFreeChart.DEFAULT_TITLE_FONT, thermometerplot, true);
            thermometerplot.setInsets(new RectangleInsets(5D, 5D, 5D, 5D));
            thermometerplot.setPadding(new RectangleInsets(10D, 10D, 10D, 10D));
            thermometerplot.setThermometerStroke(new BasicStroke(2.0F));
            thermometerplot.setThermometerPaint(Color.lightGray);
            thermometerplot.setUnits(1);
            thermometerplot.setGap(3);
            return jfreechart;
        }

        public void stateChanged(ChangeEvent changeevent) {
                dataset.setValue(new Integer(slider.getValue()));
        }

        public ContentPanel() {
                super(new BorderLayout());
                slider = new JSlider(0, 100, 50);
                slider.setPaintLabels(true);
                slider.setPaintTicks(true);
                slider.setMajorTickSpacing(25);
                slider.addChangeListener(this);
                add(slider, "South");
                dataset = new DefaultValueDataset(slider.getValue());
                add(new ChartPanel(createChart(dataset)));
        }
    }

    public ThermometerDemo1(String s) {
        super(s);
        JPanel jpanel = createDemoPanel();
        setContentPane(jpanel);
    }

    public static JPanel createDemoPanel() {
        return new ContentPanel();
    }

    public static void main(String args[]) {
        ThermometerDemo1 thermometerdemo1 = new ThermometerDemo1("Thermometer Demo 1");
        thermometerdemo1.pack();
        thermometerdemo1.setVisible(true);
    }
    
}
Additionally you have to add these jars (They are in the /lib directory of the JFreechart folder) to your project's classpath:


You can see whole of the source code here http://code.google.com/p/bluelight/
You can get JFreechart project here http://sourceforge.net/projects/jfreechart/files/
That's all by now. I hope it useful.

Don't forget to leave a comment ^^'

0 comentarios:

Publicar un comentario