I'm currently doing Agent project in java. At some point I need to show a meter for eg: battery level. I've got 5 agents in my program, each agent creates it's own meter plot with name on it, but somehow they are not updating the dataset. OR they are updating the dataset just that it isnt show on the meterplot. Any idea ?
Following is my code : car.java
{
private CarMeter meter;
meter = new CarMeter(getLocalName());
meter.update(currentBatteryCharge);
}
meterplot.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);
}
update
method work for me. What the problem for you with it? – Scupper