0

I want to animate the drawing of XYSplineRenderer. The animation should be smooth and in the future I want to be able to modify the velocity. But at the moment just a smooth animation would be fine.

I was reading about SwingWorker and also this thread, however I really cannot figure out how to work with XYSeries. How should I add points to series inside doInBackground (it says that XYSeries is not iterable)?

Any example would be really appreciated.

public static void main(String[] args) {
    EventQueue.invokeLater(new Runnable() {

        public void run() {
            GUI t = new GUI();
            t.runCalc();
        }
    });
}


public void runCalc() {
    TwoWorker task = new TwoWorker();
    task.addPropertyChangeListener(new PropertyChangeListener() {
    });
    task.execute();
}

// UNDER CONSTRUCTION !!!
private class TwoWorker extends SwingWorker<Double, Double> 
{
    @Override
    protected XYSeries doInBackground() throws Exception
    {       
        for (int i = 0; i < solution.size(); i++) 
        {
            series.add(solution.point(i).lat(),solution.point(i).lon());
            Thread.sleep(1000);
        }
        return XYSeries;
    }
}

P.S. solution is just the collection of points defined by latitude and longitude. Based on this points, XYSplineRenderer is constructed.

Community
  • 1
  • 1
Klausos Klausos
  • 15,308
  • 51
  • 135
  • 217

1 Answers1

2

Always update Swing components and models, like ChartPanel and XYSeries, on the event dispatch thread. Your example calls series.add() in the background, while @trashgod's example calls publish() in the background. The published results get handled in process(), which does run on the event dispatch thread.

Start with this example and change it like shown here to see how it works.

Community
  • 1
  • 1
Catalina Island
  • 7,027
  • 2
  • 23
  • 42