gpt4 book ai didi

java - 使用计时器在 JFreeChart 上闪烁 XYImageAnnotation

转载 作者:行者123 更新时间:2023-12-01 04:57:40 25 4
gpt4 key购买 nike

我在 JFreeChart - XYLineChart 上绘制图像时遇到困难。主要问题是注释的 x 和 y 坐标是实时动态更新的。因此,在我的代码中添加注释并清除注释以绘制新注释会导致闪烁,这对用户来说很烦人。

我已经使用图形检查了 JAVA 上使用 update() 、 Paint () 或 repaint() 方法的一些闪烁问题的示例,但似乎无法在 JFreeChart 上实现。

您是否有任何想法如何消除闪烁或使用 JFreeChart 上的一个 bufferedImage 而不是注释的解决方法?

更具体地说,这里是绘制的线条和图像:

Screenshot

因此,这个十字线(作为缓冲图像)应该随着 x 轴和 y 轴的更新值在绘图线上上下移动。但不幸的是,这种运动会导致闪烁。

这是我绘制图像的代码部分 - 我猜我无法提供 SSCCE,因为有超过 15 个类和 5k 的书面代码:

// After a button clicked on panel
SomeButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent event) {

// The chart and XYPlot is already drawn at this point


// Reading the image
try {
myPicture = ImageIO
.read(new File("\\\\Users2\\blabla\\Data\\MyPictures\\x.png"));
} catch (IOException e) {
e.printStackTrace();
}

// Setting up a timer
timer2 = new java.util.Timer();

Object source = event.getSource();
if (source == SomeButton) {

// Setting up a task
task2 = new java.util.TimerTask() {
@Override
public void run() {
double x1;
double y1;
try {
// Getting different x and y values from a microcontroller instantaneously
if (microContConnected()) {

x1 = microCont.getX();
y1 = microCont.getY();

// creating the annotation
XYImageAnnotation ImageAnn = new XYImageAnnotation(x1, y1, myPicture);

// Here is the drawing and clearing made !
plot.addAnnotation(ImageAnn);
pause(50);
plot.clearAnnotations();
}

} catch (SerialPortException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
timer2.scheduleAtFixedRate(task2, 50, 50);
}
}
});

最佳答案

看来我自己找到了解决方案;我没有将图像添加到绘图中,而是使用渲染器,并且在使用新坐标添加和删除图片之间没有暂停功能。添加和删除的顺序也是相反的。我必须说,以这种方式工作让我感到惊讶。没有留下任何闪烁;它像剪切图形或双缓冲一样平滑。 :) 这是新代码:

    // renderer
final XYLineAndShapeRenderer renderer = (XYLineAndShapeRenderer) plot.getRenderer();

// Reading the image
try {
myPicture = ImageIO.read(new File("\\\\Users2\\blabla\\Data\\MyPictures\\x.png"));
} catch (IOException e) {
e.printStackTrace();
}

// Setting up a timer
timer2 = new java.util.Timer();

Object source = event.getSource();
if (source == someButton) {

task2 = new java.util.TimerTask() {
@Override
public void run() {
if (check == true) {
if (microContConnected()) {

x1 = microCont.getX();
y1 = microCont.getY();

renderer.removeAnnotations();

XYImageAnnotation img2 = new XYImageAnnotation(
x1, y1, myPicture);
renderer.addAnnotation(img2,
Layer.FOREGROUND);
}
}
}
};
timer2.scheduleAtFixedRate(task2, 50, 50);
}

关于java - 使用计时器在 JFreeChart 上闪烁 XYImageAnnotation,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13844489/

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