java - JFreeChart chart is lagging -


this program supposed graph , animate sine wave using thread "move over" points in xyseries draws wave. after few seconds, chart starts flash , zooms in , out randomly. don't know if problem code or computer (probably code).

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

your example incorrectly synchronized in updates chart's dataset waverunner. chart's model displayed in chartpanel, should updated only on event dispatch thread. instead, pace animation using swing timer, shown here , here, or swingworker, shown here.


Comments

Popular posts from this blog

javascript - RequestAnimationFrame not working when exiting fullscreen switching space on Safari -

Python ctypes access violation with const pointer arguments -