gpt4 book ai didi

java - JFreeChart MeterPlot

转载 作者:行者123 更新时间:2023-11-30 06:21:56 25 4
gpt4 key购买 nike

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

以下是我的代码:汽车.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);
}

最佳答案

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

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

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

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

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

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

image

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);

}
}

关于java - JFreeChart MeterPlot,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19831953/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com