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===========");
}
}
}