2

我目前正在用java做代理项目。在某些时候,我需要显示一个仪表,例如:电池电量。我的程序中有 5 个代理,每个代理创建自己的仪表图,上面有名称,但不知何故他们没有更新数据集。或者他们正在更新数据集,只是它没有显示在仪表图上。任何想法 ?

以下是我的代码: car.java

{
    private CarMeter meter;
    meter = new CarMeter(getLocalName());
    meter.update(currentBatteryCharge);
}

米图.java

public class CarMeter extends ApplicationFrame {

 private static DefaultValueDataset dataset1;

public CarMeter(String s) {
    super(s);
    JPanel panel = createPanel();
    panel.setPreferredSize(new Dimension(500, 350));
    setContentPane(panel);
    pack();
    setVisible(true);
}

public CarMeter() {
    super("s");
    JPanel panel = createPanel();
    panel.setPreferredSize(new Dimension(500, 350));
    setContentPane(panel);
    pack();
    setVisible(true);
}

private static JFreeChart createChart(ValueDataset valuedataset) {
    MeterPlot meterplot = new MeterPlot(valuedataset);
  //set minimum and maximum value
    meterplot.setRange(new Range(0.0D, 100000D));

    meterplot.addInterval(new MeterInterval("Battery LOW", new Range(0.0D, 10000D), 
            Color.red, new BasicStroke(2.0F), new Color(255, 0, 0, 128)));

    meterplot.addInterval(new MeterInterval("Moderate", new Range(10001D, 90000D), 
            Color.yellow, new BasicStroke(2.0F), new Color(255, 255, 0, 64)));

    meterplot.addInterval(new MeterInterval("Battery FULL", new Range(90001D, 100000D),
            Color.green, new BasicStroke(2.0F), new Color(0, 255, 0, 64)));

    meterplot.setNeedlePaint(Color.darkGray);
    meterplot.setDialBackgroundPaint(Color.white);
    meterplot.setDialOutlinePaint(Color.black);
    meterplot.setDialShape(DialShape.CHORD);
    meterplot.setMeterAngle(180);
    meterplot.setTickLabelsVisible(true);
    meterplot.setTickLabelFont(new Font("Arial", 1, 12));
    meterplot.setTickLabelPaint(Color.black);
    meterplot.setTickSize(5D);
    meterplot.setTickPaint(Color.gray);
    meterplot.setValuePaint(Color.black);
    meterplot.setValueFont(new Font("Arial", 1, 14));
    JFreeChart jfreechart = new JFreeChart("Battery Level",
            JFreeChart.DEFAULT_TITLE_FONT, meterplot, true);
    return jfreechart;
}

public static JPanel createPanel() {
    dataset1 = new DefaultValueDataset(0);
    JFreeChart chart = createChart(dataset1);
    ChartPanel chartpanel = new ChartPanel(chart);
    return chartpanel;
}

public void update(int data){
dataset1.setValue(data);
System.out.println(""+dataset1 +" " +data);
}
4

2 回答 2

4

正如@alex2410 所指出的,您的代码如下所示。正如@Outlaw 引用的答案中所述,绘图(视图)监听数据集(模型)。一些笔记,

  • 不要在构造函数中重复代码;让一个调用另一个。

  • setPreferredSize()当你真的想要覆盖时不要使用getPreferredSize()

  • 不要使用静态成员来引用可重用视图类中的模型。

  • 不要在构造函数中调用公共方法。

  • 请使用连续范围以避免标签中的视觉伪影。

图片

import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Random;
import javax.swing.JPanel;
import javax.swing.Timer;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.DialShape;
import org.jfree.chart.plot.MeterInterval;
import org.jfree.chart.plot.MeterPlot;
import org.jfree.data.Range;
import org.jfree.data.general.DefaultValueDataset;
import org.jfree.data.general.ValueDataset;
import org.jfree.ui.ApplicationFrame;

public class CarMeter extends ApplicationFrame {

    private DefaultValueDataset dataset;
    private int value = 50000;

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new CarMeter();
            }
        });
    }

    public CarMeter(String s) {
        super(s);
        JPanel panel = createPanel();
        setContentPane(panel);
        pack();
        setLocationRelativeTo(null);
        setVisible(true);
        Timer t = new Timer(250, new ActionListener() {
            Random r = new Random();

            @Override
            public void actionPerformed(ActionEvent e) {
                value -= (int) (Math.abs(100 * r.nextGaussian()));
                update(value);
            }
        });
        t.start();
    }

    public CarMeter() {
        this("Test");
    }

    private JFreeChart createChart(ValueDataset valuedataset) {
        MeterPlot meterplot = new MeterPlot(valuedataset);
        meterplot.setRange(new Range(0.0D, 100000D));
        meterplot.addInterval(new MeterInterval("Battery LOW", new Range(0.0D, 10000D),
            Color.red, new BasicStroke(2.0F), new Color(255, 0, 0, 128)));
        meterplot.addInterval(new MeterInterval("Moderate", new Range(10000D, 90000D),
            Color.yellow, new BasicStroke(2.0F), new Color(255, 255, 0, 64)));
        meterplot.addInterval(new MeterInterval("Battery FULL", new Range(90000D, 100000D),
            Color.green, new BasicStroke(2.0F), new Color(0, 255, 0, 64)));

        meterplot.setNeedlePaint(Color.darkGray);
        meterplot.setDialBackgroundPaint(Color.white);
        meterplot.setDialOutlinePaint(Color.black);
        meterplot.setDialShape(DialShape.CHORD);
        meterplot.setMeterAngle(180);
        meterplot.setTickLabelsVisible(true);
        meterplot.setTickLabelFont(new Font("Arial", 1, 14));
        meterplot.setTickLabelPaint(Color.black);
        meterplot.setTickSize(5D);
        meterplot.setTickPaint(Color.gray);
        meterplot.setValuePaint(Color.black);
        meterplot.setValueFont(new Font("Arial", 1, 14));
        JFreeChart jfreechart = new JFreeChart("Battery Level",
            JFreeChart.DEFAULT_TITLE_FONT, meterplot, true);
        return jfreechart;
    }

    private JPanel createPanel() {
        dataset = new DefaultValueDataset(value);
        JFreeChart chart = createChart(dataset);
        ChartPanel chartpanel = new ChartPanel(chart) {
            @Override
            public Dimension getPreferredSize() {
                return new Dimension(640, 480);
            }
        };
        return chartpanel;
    }

    public void update(int data) {
        dataset.setValue(data);

    }
}
于 2013-11-07T10:41:13.070 回答
1

认为这是Jfreechart 的副本 - 根据不断变化的数据刷新图表

您需要以某种方式重新绘制图表,更新方法只会设置数据值,但图表本身保持不变。

为了快速而肮脏的修复而无需进一步研究,我建议再次调用 createChart 方法(在您的更新方法内)并更新图表对象。

只是为了验证这是否可以解决问题。我建议您阅读 GOF 观察者模式,这是一种将 GUI 与数据分离并在需要时更新 GUI 的好方法http://en.wikipedia.org/wiki/Observer_pattern

于 2013-11-07T09:39:22.607 回答