java图片实时更新,Java中的实时图形

I have an application which updates a variable about between 5 to 50 times a second and I am looking for some way of drawing a continuous XY plot of this change in real-time.

Though JFreeChart is not recommended for such a high update rate, many users still say that it works for them. I've tried using this demo and modified it to display a random variable, but it seems to use up 100% CPU usage all the time. Even if I ignore that, I do not want to be restricted to JFreeChart's ui class for constructing forms (though I'm not sure what its capabilities are exactly). Would it be possible to integrate it with Java's "forms" and drop-down menus? (as are available in VB) Otherwise, are there any alternatives I could look into?

EDIT: I'm new to Swing, so I've put together a code just to test the functionality of JFreeChart with it (while avoiding the use of the ApplicationFrame class of JFree since I'm not sure how that will work with Swing's combo boxes and buttons). Right now, the graph is being updated immediately and CPU usage is high. Would it be possible to buffer the value with new Millisecond() and update it maybe twice a second? Also, can I add other components to the rest of the JFrame without disrupting JFreeChart? How would I do that? frame.getContentPane().add(new Button("Click")) seems to overwrite the graph.

package graphtest;

import java.util.Random;

import javax.swing.JFrame;

import org.jfree.chart.ChartFactory;

import org.jfree.chart.ChartPanel;

import org.jfree.chart.JFreeChart;

import org.jfree.chart.axis.ValueAxis;

import org.jfree.chart.plot.XYPlot;

import org.jfree.data.time.Millisecond;

import org.jfree.data.time.TimeSeries;

import org.jfree.data.time.TimeSeriesCollection;

public class Main {

static TimeSeries ts = new TimeSeries("data", Millisecond.class);

public static void main(String[] args) throws InterruptedException {

gen myGen = new gen();

new Thread(myGen).start();

TimeSeriesCollection dataset = new TimeSeriesCollection(ts);

JFreeChart chart = ChartFactory.createTimeSeriesChart(

"GraphTest",

"Time",

"Value",

dataset,

true,

true,

false

);

final XYPlot plot = chart.getXYPlot();

ValueAxis axis = plot.getDomainAxis();

axis.setAutoRange(true);

axis.setFixedAutoRange(60000.0);

JFrame frame = new JFrame("GraphTest");

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

ChartPanel label = new ChartPanel(chart);

frame.getContentPane().add(label);

//Suppose I add combo boxes and buttons here later

frame.pack();

frame.setVisible(true);

}

static class gen implements Runnable {

private Random randGen = new Random();

public void run() {

while(true) {

int num = randGen.nextInt(1000);

System.out.println(num);

ts.addOrUpdate(new Millisecond(), num);

try {

Thread.sleep(20);

} catch (InterruptedException ex) {

System.out.println(ex);

}

}

}

}

}

解决方案

If your variable is updating that fast, there's no point in updating a chart every time.

Have you thought about buffering the variable changes, and refreshing the chart on a different thread, say, every 5s ? You should find that JFreeChart can handle such update rates well.

Since JFreeChart is a normal desktop library, you can integrate it with a standard Swing application very easily. Or, you can use it to chart via a web application (by rendering to a JPEG/PNG etc. JFreeChart can generate image maps automatically as well, so you can use mouseovers etc.)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值