Thermometer in Java with JFreeChart

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.

Comentarios

  1. Buena, tu ejemplo me sirvió de mucho, le hice algunas modificaciones para que funcione con un JButton agregándole un ActionListener, muy agradecido, hace ya varios días estaba tratando de hacer algo parecido, pero la clave estaba en "dataset.setValue", para modificar el valor que muestra el termómetro. Saludos desde Bolivia.

    ResponderBorrar
    Respuestas
    1. hola, podrías compartir las modificaciones que le hiciste?

      Borrar
    2. es posible agregar valores de temperatura bajo cero? y si es así, me podrías decir como? desde ya muchas gracias

      Borrar

Publicar un comentario