在下面的代码中,我正在进行一个实验,我需要每秒绘制近10个图形(时间间隔100),总共50个图形。但是,当我将时间间隔TIME INTERVAL(第120行向下向下)从200毫秒减小时到100毫秒,代码将引发以下异常。

我已经厌倦了invokeLater等,但是我无法解决此问题。我在这里粘贴了生成下面图形的整个代码。请看一下,让我知道该怎么办。提前致谢

Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: Series index out of bounds
    at org.jfree.data.xy.XYSeriesCollection.getSeries(XYSeriesCollection.java:261)
    at org.jfree.data.xy.XYSeriesCollection.getSeriesKey(XYSeriesCollection.java:303)
    at org.jfree.chart.labels.AbstractXYItemLabelGenerator.createItemArray(AbstractXYItemLabelGenerator.java:268)
    at org.jfree.chart.labels.AbstractXYItemLabelGenerator.generateLabelString(AbstractXYItemLabelGenerator.java:238)
    at org.jfree.chart.labels.StandardXYToolTipGenerator.generateToolTip(StandardXYToolTipGenerator.java:165)
    at org.jfree.chart.renderer.xy.AbstractXYItemRenderer.addEntity(AbstractXYItemRenderer.java:1747)
    at org.jfree.chart.renderer.xy.XYLineAndShapeRenderer.drawSecondaryPass(XYLineAndShapeRenderer.java:1196)
    at org.jfree.chart.renderer.xy.XYLineAndShapeRenderer.drawItem(XYLineAndShapeRenderer.java:918)
    at org.jfree.chart.plot.XYPlot.render(XYPlot.java:3776)
    at org.jfree.chart.plot.XYPlot.draw(XYPlot.java:3337)
    at org.jfree.chart.JFreeChart.draw(JFreeChart.java:1226)
    at org.jfree.chart.ChartPanel.paintComponent(ChartPanel.java:1612)
    at javax.swing.JComponent.paint(JComponent.java:1054)
    at javax.swing.JComponent.paintToOffscreen(JComponent.java:5221)
    at javax.swing.RepaintManager$PaintManager.paintDoubleBuffered(RepaintManager.java:1508)
    at javax.swing.RepaintManager$PaintManager.paint(RepaintManager.java:1439)
    at javax.swing.RepaintManager.paint(RepaintManager.java:1236)
    at javax.swing.JComponent._paintImmediately(JComponent.java:5169)
    at javax.swing.JComponent.paintImmediately(JComponent.java:4980)
    at javax.swing.RepaintManager$3.run(RepaintManager.java:796)
    at javax.swing.RepaintManager$3.run(RepaintManager.java:784)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:784)
    at javax.swing.RepaintManager.paintDirtyRegions(RepaintManager.java:757)
    at javax.swing.RepaintManager.prePaintDirtyRegions(RepaintManager.java:706)
    at javax.swing.RepaintManager.access$1000(RepaintManager.java:62)
    at javax.swing.RepaintManager$ProcessingRunnable.run(RepaintManager.java:1647)
    at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:251)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:733)
    at java.awt.EventQueue.access$200(EventQueue.java:103)
    at java.awt.EventQueue$3.run(EventQueue.java:694)
    at java.awt.EventQueue$3.run(EventQueue.java:692)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:703)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:242)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:161)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:150)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:146)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:138)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:91)




import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Rectangle;
import java.awt.Shape;
import java.awt.geom.Ellipse2D;
import javax.swing.JFrame;
import javax.swing.JPanel;
import org.jfree.chart.*;
import org.jfree.chart.axis.NumberAxis;
import org.jfree.chart.event.ChartChangeListener;
import org.jfree.chart.plot.PlotOrientation;
import org.jfree.chart.plot.XYPlot;
import org.jfree.chart.renderer.xy.XYItemRenderer;
import org.jfree.data.xy.XYSeries;
import org.jfree.data.xy.XYSeriesCollection;
import org.jfree.util.ShapeUtilities;

public class FastChart extends JFrame {
   private XYSeries [] xySeries ;
   private XYPlot xyPlot;
   private XYSeriesCollection xySeriesCollection;
   private String title;

   private ChartPanel chartPanel;

   public  FastChart(String s) {
      super(s);
      init(s);
   }

   private void init(String s){
      title = s;
      xySeries = new XYSeries[12];
      for (int i = 0; i < xySeries.length; i++) {
         xySeries[i]    = new XYSeries("Plot "+i);
      }
      xySeriesCollection = new XYSeriesCollection();
      JFreeChart chart = ChartFactory.createScatterPlot(
            title, "X", "Y", xySeriesCollection,
            PlotOrientation.VERTICAL, true, true, false);

      xyPlot =  chart.getXYPlot();
      xyPlot.setDomainCrosshairVisible(true);
      xyPlot.setRangeCrosshairVisible(true);

      chartPanel = createChartPanel(chart);

      add(chartPanel, BorderLayout.CENTER);
      JPanel control = new JPanel();

      add(control, BorderLayout.SOUTH);

      setDefaultCloseOperation(DISPOSE_ON_CLOSE);
      pack();
      setLocationRelativeTo(null);
      setVisible(true);
   }

   private ChartPanel createChartPanel(JFreeChart chart) {

      XYItemRenderer renderer = xyPlot.getRenderer();
      renderer.setSeriesPaint(0, Color.magenta);
      renderer.setSeriesPaint(1, Color.green);
      renderer.setSeriesPaint(2, Color.blue);
      renderer.setSeriesPaint(4, Color.black);
      renderer.setSeriesPaint(3, Color.yellow);
      Shape cross = ShapeUtilities.createDiagonalCross(3, 0);
      Shape plus = ShapeUtilities.createRegularCross(4,0);

      for (int i = 0; i <=3; i++) {
         renderer.setSeriesShape(0+i,    new Rectangle(-1, -1, 2, 2));
         renderer.setSeriesShape(4+i,    new Ellipse2D.Float(-2F, -2F, 5F, 5F));
         renderer.setSeriesShape(8+i,    cross);
      }
      // X axis
      NumberAxis domain = (NumberAxis) xyPlot.getDomainAxis();
      domain.setVerticalTickLabels(true);
      // Y axis
      return new ChartPanel(chart);
   }


   public  void plot2d( final int iSeriesN,  final double [] dX,  final double [] dY, final String sT){

      if (dY.length != dX.length){
         throw new IllegalArgumentException("Error! inputs x and y have to be of same size.");
      }
      chartPanel.getChart().removeChangeListener((ChartChangeListener) chartPanel);

      xySeries[iSeriesN].clear();
      xySeriesCollection.removeAllSeries();
      xySeries[iSeriesN]= addXY(iSeriesN,  dX, dY);
      xySeries[iSeriesN].setKey(sT);
      for (int i = 0; i < xySeries.length; i++) {
         xySeriesCollection.addSeries(xySeries[i]);
      }

      chartPanel.getChart().addChangeListener((ChartChangeListener) chartPanel);
      xyPlot.setDataset(xySeriesCollection);
   }

   public XYSeries addXY(final int iSeriesN, final double [] dX, final double [] dY){
      XYSeries series = new XYSeries("Plot ");

      for (int i = 0; i < dX.length; i++) {
         series.add(dX[i], dY[i]);
      }
      return series;
   }
   public  void animatePlot(){
      Thread thread = null;
      thread = new Thread (){
         public void run() {
            final double [] x = new double[1000];
            final double [] y = new double[1000];

            try{
               for (int k = 1; k < 50; k++) {
                  try {
                     Thread.sleep(200);  //**** TIME INTERVAL *****
                  } catch (InterruptedException e) {
                     e.printStackTrace();
                  }
                  for (int i = 0; i < y.length; i++) {
                     x[i] = i;
                     y[i] = i*i*k;
                  }
                  plot2d(k % 12, x, y,"plot#"+k);
               }
            } catch (Exception e){
               System.out.println();
            }
         }
      };
      thread.start();
   }

   public static void main(String args[]) {
      FastChart  demo = new FastChart("X Y Plot");
      demo.animatePlot();
   }
}

最佳答案

您正在从Thread thread更新模型。要在EDT上进行更新,请使用here所示的javax.swing.Timer,或here所示的SwingWorker

附录:为了验证该假设,我在javax.swing.Timer中添加了以下init()

Timer t = new Timer(200, new ActionListener() {
    int k = 0;

    @Override
    public void actionPerformed(ActionEvent e) {
        if (k++ < 19) {
            final double[] x = new double[1000];
            final double[] y = new double[1000];
            for (int i = 0; i < y.length; i++) {
                x[i] = i;
                y[i] = i * i * k;
            }
            plot2d(k % 12, x, y, "plot#" + k);
        }
    }
});
t.start();


我还在invokeLater()中使用了main()

EventQueue.invokeLater(new Runnable() {

    @Override
    public void run() {
        FastChart demo = new FastChart("X Y Plot");
    }
});

关于java - 快速绘制多个图形的时序问题,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20056614/

10-15 16:22