Atualizar Gráfico jfreeChart com dados dentro de um arquivo txt

Boa noite a todos,
Sou novo aqui no fórum, estive procurando sobre “Como atualizar gráfico no Java” e fiquei meio perdido.
O problema é o seguinte, vou fazer um velocímetro, as informações de velocidade são atualizadas a cada “x” segundos em um arquivo .txt. Eu estou acessando esse arquivo e desenhando o gráfico com o JFreeChart mas quando atualiza o arquivo .txt com a nova velocidade o meu gráfico não atualiza. Vou postar aqui o meu código.

public static void main(String[] args) throws FileNotFoundException, IOException, InterruptedException {
        Velocimetro velocimetro = new Velocimetro("Velocímetro - Teste 1");
        velocimetro.pack();
        velocimetro.setVisible(true);
        while (true) {
            velocimetro.lerVelocidadeTxt();
            velocimetro.tempoRel();
        }
}

Eu sei que ele consegue pegar a nova informação da velocidade mas não esta mostrando no Gráfico, se souberem de uma forma mais fácil de fazer isso…
Ja tentei usar o repaint, update, etc… mas não atualiza de forma alguma.

Obrigado.


public class Velocimetro extends JFrame {

    private static String velocidadeValor = "0";

    public static String getVelocidadeValor() {
        return velocidadeValor;
    }

    public static void setVelocidadeValor(String velocidade) {
        velocidadeValor = velocidade;
    }
    public static DefaultValueDataset dataset;

    public static void setDefaultValueDataSet(DefaultValueDataset data) {
        dataset = data;
    }

    public static DefaultValueDataset getDefaultValueDataSet() {
        return dataset;
    }

    public static class DemoPanel extends JPanel //implements FocusListener//ChangeListener
    {
        JSlider slider;
        DefaultValueDataset xdataset;

        public static JFreeChart createStandardDialChart(String s, String s1, ValueDataset valuedataset, double d, double d1, double d2, int i) {
            DialPlot dialplot = new DialPlot();
            dialplot.setDataset(valuedataset);
            dialplot.setDialFrame(new StandardDialFrame());
            dialplot.setBackground(new DialBackground());
            DialTextAnnotation dialtextannotation = new DialTextAnnotation(s1);
            dialtextannotation.setFont(new Font("Dialog", 1, 14));
            dialtextannotation.setRadius(0.69999999999999996D);
            dialplot.addLayer(dialtextannotation);
            DialValueIndicator dialvalueindicator = new DialValueIndicator(0);
            dialplot.addLayer(dialvalueindicator);
            StandardDialScale standarddialscale = new StandardDialScale(d, d1, -120D, -300D, 10D, 4);
            standarddialscale.setMajorTickIncrement(d2);
            standarddialscale.setMinorTickCount(i);
            standarddialscale.setTickRadius(0.88D);
            standarddialscale.setTickLabelOffset(0.14999999999999999D);
            standarddialscale.setTickLabelFont(new Font("Dialog", 0, 14));
            dialplot.addScale(0, standarddialscale);
            dialplot.addPointer(new org.jfree.chart.plot.dial.DialPointer.Pin());
            DialCap dialcap = new DialCap();
            dialplot.setCap(dialcap);
            return new JFreeChart(s, dialplot);
        }

        public DemoPanel() throws InterruptedException, FileNotFoundException, IOException {

            xdataset = new DefaultValueDataset(Integer.parseInt(velocidadeValor));
            System.out.println("O valor xdataset = " + xdataset.getValue());

            JFreeChart jfreechart = createStandardDialChart(
                    "Velocidade",
                    "Km/h",
                    xdataset,
                    0D, 160D, 10D, 4);

            DialPlot dialplot = (DialPlot) jfreechart.getPlot();
            StandardDialRange standarddialrange = new StandardDialRange(100D, 160D, Color.red);
            standarddialrange.setInnerRadius(0.52000000000000002D);
            standarddialrange.setOuterRadius(0.55000000000000004D);
            dialplot.addLayer(standarddialrange);
            StandardDialRange standarddialrange1 = new StandardDialRange(60D, 100D, Color.orange);
            standarddialrange1.setInnerRadius(0.52000000000000002D);
            standarddialrange1.setOuterRadius(0.55000000000000004D);
            dialplot.addLayer(standarddialrange1);
            StandardDialRange standarddialrange2 = new StandardDialRange(0D, 60D, Color.green);
            standarddialrange2.setInnerRadius(0.52000000000000002D);
            standarddialrange2.setOuterRadius(0.55000000000000004D);
            dialplot.addLayer(standarddialrange2);

            GradientPaint gradientpaint = new GradientPaint(new Point(), new Color(255, 255, 255), new Point(), new Color(170, 170, 220));
            DialBackground dialbackground = new DialBackground(gradientpaint);
            dialbackground.setGradientPaintTransformer(new StandardGradientPaintTransformer(GradientPaintTransformType.VERTICAL));
            dialplot.setBackground(dialbackground);

            dialplot.removePointer(0);
            org.jfree.chart.plot.dial.DialPointer.Pointer pointer = new org.jfree.chart.plot.dial.DialPointer.Pointer();
            pointer.setFillPaint(Color.yellow);
            dialplot.addPointer(pointer);

            ChartPanel chartpanel = new ChartPanel(jfreechart);
            chartpanel.setPreferredSize(new Dimension(400, 400));

            add(chartpanel);
        }
    }

    public Velocimetro(String s) throws FileNotFoundException, IOException, InterruptedException {
        super(s);
        setDefaultCloseOperation(3);
        setContentPane(createDemoPanel());
    }

    public static JPanel createDemoPanel() throws FileNotFoundException, IOException, InterruptedException {
        BufferedReader leitor = new BufferedReader(new FileReader("velocidade.txt"));
        velocidadeValor = leitor.readLine();
        return new DemoPanel();
    }

    public void lerVelocidadeTxt() throws FileNotFoundException, IOException, InterruptedException {
        try {
            BufferedReader leitor = new BufferedReader(new FileReader("velocidade.txt"));
            velocidadeValor = leitor.readLine();
        } catch (IOException e) {
            System.out.println("Nao encontrou o arquivo velocidade.txt===========");
        }

        DemoPanel d = new DemoPanel();
        d.validate();
        System.out.println("Cheguei aqui dentro............" + Velocimetro.getVelocidadeValor());
   
    public void tempoRel() {
        try {
            Thread.sleep(5000);

        } catch (InterruptedException e) {
            System.out.println("Nao leu o arquivo===========");

        }
    }
}

Alteração para o gráfico atualizar automaticamente, conforme o arquivo txt é alterado.

Classe Velocimetro:

[code]public class Velocimetro extends JFrame {

    private static String velocidadeValor = "0";  
  
    private static JFreeChart jfreechart;
    
    public static String getVelocidadeValor() {  
        return velocidadeValor;  
    }  
  
    public static void setVelocidadeValor(String velocidade) {  
        velocidadeValor = velocidade;  
    }  
    public static DefaultValueDataset dataset;  
  
    public static void setDefaultValueDataSet(DefaultValueDataset data) {  
        dataset = data;  
    }  
  
    public static DefaultValueDataset getDefaultValueDataSet() {  
        return dataset;  
    }  
  
    public static class DemoPanel extends JPanel //implements FocusListener//ChangeListener  
    {  
        JSlider slider;  
        DefaultValueDataset xdataset;  
  
        public static JFreeChart createStandardDialChart(String s, String s1, ValueDataset valuedataset, double d, double d1, double d2, int i) {  
            DialPlot dialplot = new DialPlot();  
            dialplot.setDataset(valuedataset);  
            dialplot.setDialFrame(new StandardDialFrame());  
            dialplot.setBackground(new DialBackground());  
            DialTextAnnotation dialtextannotation = new DialTextAnnotation(s1);  
            dialtextannotation.setFont(new Font("Dialog", 1, 14));  
            dialtextannotation.setRadius(0.69999999999999996D);  
            dialplot.addLayer(dialtextannotation);  
            DialValueIndicator dialvalueindicator = new DialValueIndicator(0);  
            dialplot.addLayer(dialvalueindicator);  
            StandardDialScale standarddialscale = new StandardDialScale(d, d1, -120D, -300D, 10D, 4);  
            standarddialscale.setMajorTickIncrement(d2);  
            standarddialscale.setMinorTickCount(i);  
            standarddialscale.setTickRadius(0.88D);  
            standarddialscale.setTickLabelOffset(0.14999999999999999D);  
            standarddialscale.setTickLabelFont(new Font("Dialog", 0, 14));  
            dialplot.addScale(0, standarddialscale);  
            dialplot.addPointer(new org.jfree.chart.plot.dial.DialPointer.Pin());  
            DialCap dialcap = new DialCap();  
            dialplot.setCap(dialcap);  
            return new JFreeChart(s, dialplot);  
        }  
  
        public DemoPanel() throws InterruptedException, FileNotFoundException, IOException {  
  
            xdataset = new DefaultValueDataset(Integer.parseInt(velocidadeValor));  
            System.out.println("O valor xdataset = " + xdataset.getValue());  
  
            jfreechart = createStandardDialChart(  
                    "Velocidade",   //Título do gráfico
                    "Km/h",         //Unidade de medida do gráfico
                    xdataset,       //Valor indicado no velocímetro
                    0D,             //Marcador Mínimo do velocímetro
                    200D,           //Marcador Máximo do velocímetro
                    10D,            //Marcador da medida (proporção)
                    10);            //Divisões do mostrador de medida
  
            //indicativo das cores (VERMELHO, VERDE E AMARELO)
            DialPlot dialplot = (DialPlot) jfreechart.getPlot();  
            StandardDialRange standarddialrange = new StandardDialRange(120D, 200D, Color.red);  
            standarddialrange.setInnerRadius(0.52000000000000002D);  
            standarddialrange.setOuterRadius(0.55000000000000004D);  
            dialplot.addLayer(standarddialrange);  
            StandardDialRange standarddialrange1 = new StandardDialRange(100D, 120D, Color.yellow);  
            standarddialrange1.setInnerRadius(0.52000000000000002D);  
            standarddialrange1.setOuterRadius(0.55000000000000004D);  
            dialplot.addLayer(standarddialrange1);  
            StandardDialRange standarddialrange2 = new StandardDialRange(0D, 100D, Color.green);  
            standarddialrange2.setInnerRadius(0.52000000000000002D);  
            standarddialrange2.setOuterRadius(0.55000000000000004D);  
            dialplot.addLayer(standarddialrange2);  
  
            GradientPaint gradientpaint = new GradientPaint(new Point(), new Color(255, 255, 255), new Point(), new Color(170, 170, 220));  
            DialBackground dialbackground = new DialBackground(gradientpaint);  
            dialbackground.setGradientPaintTransformer(new StandardGradientPaintTransformer(GradientPaintTransformType.VERTICAL));  
            dialplot.setBackground(dialbackground);  
  
            //Ponteiro
            dialplot.removePointer(0);  
            org.jfree.chart.plot.dial.DialPointer.Pointer pointer = new org.jfree.chart.plot.dial.DialPointer.Pointer();  
            pointer.setFillPaint(Color.blue);  
            dialplot.addPointer(pointer);  
  
            ChartPanel chartpanel = new ChartPanel(jfreechart);  
            chartpanel.setPreferredSize(new Dimension(400, 400));  
  
            add(chartpanel); 
        }  
    }  
  
    public Velocimetro(String s) throws FileNotFoundException, IOException, InterruptedException {  
        super(s);  
        setDefaultCloseOperation(3);  
        setContentPane(createDemoPanel());  
    }  
  
    
    public void atualizaVelocimetro() throws IOException, FileNotFoundException, InterruptedException{
        setDefaultCloseOperation(3);  
        setContentPane(createDemoPanel());
    }
    
    public static JPanel createDemoPanel() throws FileNotFoundException, IOException, InterruptedException {  
        BufferedReader leitor = new BufferedReader(new FileReader("C:\\temp\\velocidade.txt"));  
        velocidadeValor = leitor.readLine();  
        return new DemoPanel();  
    }  
  
    public void lerVelocidadeTxt() throws FileNotFoundException, IOException, InterruptedException {  
        try {  
            BufferedReader leitor = new BufferedReader(new FileReader("C:\\temp\\velocidade.txt"));  
            velocidadeValor = leitor.readLine();  
        } catch (IOException e) {  
            System.out.println("Nao encontrou o arquivo velocidade.txt===========");  
        }  
  
        DemoPanel d = new DemoPanel();  
        d.validate();  
        System.out.println("Cheguei aqui dentro............" + Velocimetro.getVelocidadeValor());  
        
    }
/**
 *
 * @throws java.io.IOException
 */
public void tempoRel() throws IOException {  
        try {  
            Thread.sleep(5000);  
            atualizaVelocimetro();
        } catch (InterruptedException e) {  
            System.out.println("Nao leu o arquivo===========");  
  
        }  
    }  
}  [/code]

Classe TestaVelocimetro:

[code]public class TestaVelocimetro {

public static void main(String[] args) throws FileNotFoundException, IOException, InterruptedException {
    Velocimetro velocimetro = new Velocimetro("Velocímetro - Teste 1");
    velocimetro.pack();
    velocimetro.setVisible(true);
    while (true) {
        velocimetro.lerVelocidadeTxt();
        velocimetro.tempoRel();
        velocimetro.pack();
    }
}

}[/code]