我目前正在用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);
}