sleep(100)-Swing动态生成折线图

如何做一个实时动态Swing折线图呢?
由于脑海中构想了如下图这样一个画面,所以经过查资料写代码,实现了视觉化的图景。分享之。。。

[img]http://dl.iteye.com/upload/attachment/274178/ad647a1c-f43b-31c0-91a2-5b11ec80c778.jpg[/img]

public class JFSwingDynamicChart extends JFrame implements ActionListener {
private TimeSeries series;
private double lastValue = 100.0;

/**
* 构造
*/
public JFSwingDynamicChart() {
getContentPane().setBackground(Color.green);
}

/**
* 创建应用程序界面
*/
public void createUI() {
this.series = new TimeSeries("Math.random()-随机数据", Millisecond.class);
TimeSeriesCollection dataset = new TimeSeriesCollection(this.series);
ChartPanel chartPanel = new ChartPanel(createChart(dataset));
chartPanel.setPreferredSize(new java.awt.Dimension(500, 270));

JPanel buttonPanel = new JPanel();
buttonPanel.setBorder(BorderFactory.createEmptyBorder(4, 4, 4, 4));
add(chartPanel);
add(buttonPanel, BorderLayout.SOUTH);
}

/**
* 根据结果集构造JFreechart报表对象
*
* @param dataset
* @return
*/
private JFreeChart createChart(XYDataset dataset) {
JFreeChart result = ChartFactory.createTimeSeriesChart("Swing动态折线图", "系统当前时间",
"动态数值变化", dataset, true, true, false);
XYPlot plot = (XYPlot) result.getPlot();
ValueAxis axis = plot.getDomainAxis();
axis.setAutoRange(true);
axis.setFixedAutoRange(60000.0);
axis = plot.getRangeAxis();
axis.setRange(0.0, 200.0);
return result;
}

public void actionPerformed(ActionEvent e) {
}

/**
* 动态运行
*/
public void dynamicRun() {
while (true) {
double factor = 0.90 + 0.2 * Math.random();
this.lastValue = this.lastValue * factor;
Millisecond now = new Millisecond();
this.series.add(new Millisecond(), this.lastValue);
try {
Thread.currentThread().sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}

// 主函数入口
public static void main(String[] args) {
JFSwingDynamicChart jsdChart = new JFSwingDynamicChart();
jsdChart.setTitle("Swing动态折线图");
jsdChart.createUI();
jsdChart.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
jsdChart.setBounds(100, 100, 900, 600);
jsdChart.setVisible(true);
// Color c=new Color((int)(Math.random()*256),(int)(Math.random()*256),(int)(Math.random()*256));

jsdChart.dynamicRun();
}

}



-------------------------
[b][color=blue]Thread.currentThread().sleep(100);[/color][/b] //线程交替,给绘制线程以时间可以解决此异常
Exception in thread "main" org.jfree.data.general.SeriesException: You are attempting to add an observation for the time period Wed Jul 07 11:36:28 CST 2010 but the series already contains an observation for that time period. Duplicates are not permitted.  Try using the addOrUpdate() method.
at org.jfree.data.time.TimeSeries.add(TimeSeries.java:529)
at org.jfree.data.time.TimeSeries.add(TimeSeries.java:572)
at org.jfree.data.time.TimeSeries.add(TimeSeries.java:558)
at chapter09.JFSwingDynamicChart.dynamicRun(JFSwingDynamicChart.java:76)
at chapter09.JFSwingDynamicChart.main(JFSwingDynamicChart.java:96)
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值