gpt4 book ai didi

java - JFreeChart 图表滞后

转载 作者:行者123 更新时间:2023-11-30 03:51:01 24 4
gpt4 key购买 nike

该程序应该通过使用线程“移动”绘制波的 XYSeries 中的点来绘制正弦波的图形和动画。几秒钟后,图表开始闪烁并随机放大和缩小。我不知道这是我的代码问题还是我的计算机问题(可能是代码问题)。

import java.awt.geom.Point2D;
import static java.lang.Math.PI;
import javax.swing.JFrame;
import static javax.swing.JFrame.EXIT_ON_CLOSE;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYLineAndShapeRenderer;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;

public class WaveGraph extends JFrame
{
double numOfWaves = 2;
double waveAmp = 2;
double waveLength = 10;
double waveRes = 20;
double median = ((waveLength*numOfWaves)/2);
int numOfPoints = (int)(numOfWaves*waveRes);
boolean going = true;
boolean left = true;
XYSeriesCollection dataset = new XYSeriesCollection();
Point2D.Double[] points;

WaveGraph()
{
XYSeries buoys = new XYSeries("Buoys");
XYSeries series = new XYSeries("Wave");
points = new Point2D.Double[numOfPoints];
for(int i=0; i<numOfPoints; i++)
{
Point2D.Double temp = new Point2D.Double();
temp.x = i*(waveLength/waveRes);
temp.y = waveAmp*Math.sin((2*PI)/waveLength*temp.x);
points[i] = temp;
series.add(points[i].x, points[i].y);
if(i==(numOfPoints/2)-1)
buoys.add(median, points[i].y);
}

buoys.add(median, 0);
dataset.addSeries(series);
dataset.addSeries(buoys);
JFreeChart chart = ChartFactory.createXYLineChart(
"Sine Wave", // chart title
"Wavelength(meters)", // x axis label
"Amplitude (meters)", dataset, // data
PlotOrientation.VERTICAL,
false, // include legend
false, // tooltips
false // urls
);
XYPlot plot = (XYPlot)chart.getPlot();
XYLineAndShapeRenderer renderer
= (XYLineAndShapeRenderer) plot.getRenderer();
renderer.setSeriesShapesFilled(1, true);
renderer.setSeriesShapesVisible(1, true);
NumberAxis domain = (NumberAxis)plot.getDomainAxis();
domain.setRange(0.00, waveLength*numOfWaves);
NumberAxis range = (NumberAxis)plot.getRangeAxis();
range.setRange(-waveAmp*1.3, waveAmp*1.3);
ChartPanel cp = new ChartPanel(chart);
cp.setPreferredSize(new java.awt.Dimension(500, 270));
setContentPane(cp);
setDefaultCloseOperation(EXIT_ON_CLOSE);
pack();
setLocationRelativeTo(null);
setVisible(true);
WaveRunner run = new WaveRunner();
}

class WaveRunner extends Thread
{
WaveRunner()
{
this.start();
}

public void run()
{
double[] temp = new double[numOfPoints];
XYSeries temp2, temp3;
while(going)
{
temp2 = dataset.getSeries(0);
temp3 = dataset.getSeries(1);
temp2.delete(0, numOfPoints-1);
temp3.delete(0, 1);
for(int i=0; i<numOfPoints; i++)
{
if(left){
try{
temp[i] = points[i+1].y;
}
catch(java.lang.ArrayIndexOutOfBoundsException exc){
temp[numOfPoints-1] = points[0].y;
}}
else{
try{
temp[i] = points[i-1].y;
}
catch(java.lang.ArrayIndexOutOfBoundsException exc){
temp[0] = points[numOfPoints-1].y;
}}
}
for(int i=0; i<numOfPoints; i++)
{
points[i].y = temp[i];
temp2.add(points[i].x, points[i].y);
if(i==(numOfPoints/2))
{
temp3.add(median, points[i].y);
temp3.add(median, 0);
}
}
try
{
Thread.sleep(50);
}
catch(InterruptedException exc)
{}
}
}
}

public static void main(String[] args)
{
WaveGraph test = new WaveGraph();
}
}

最佳答案

您的示例未正确同步,因为它从 WaveRunner 更新了图表的数据集。图表的模型显示在 ChartPanel 中,因此应仅在 event dispatch thread 上更新它。 。相反,使用 Swing Timer 调整动画速度,如图herehere ,或SwingWorker ,如图here .

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

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