开发者

Processing AnimationThread as a background task in Swing

I am writing an application using Processing PApplet ( http://processing.org/ ) inside a Swing gui and those two are not a happy marriage. I have a listener for JButton which starts a Swing Worker to run some long calculations and then plot the outcomes. The problem is that only the calculations run in Swing Worker not the Processing Animation Thread responsible for plotting, in the result the gui looks like unoccupied (progress bar and Jbutton generateProcessing are not busy), while in reality it is still plotting for a good minute or so.

When profiling the application I can see Swing Worker thread running the calculations, then it dies out and the done() methods is executed and prints "Finished" message to terminal, then the Animation Thread immediately kicks in (after the plot is ready it doesn't go off but goes to sleep, but that's fine I guess).

The expected behaviuor would be to have the done() method called when both Animation Thread and the Swing Worker jobs are done.

My listener looks like this (timeSlicerToProcessing is a class extending PApplet)

private class ListenGenerateProcessing implements ActionListener {
    public void actionPerformed(ActionEvent e) {

        SwingWorker<开发者_如何学PythonVoid, Void> worker = new SwingWorker<Void, Void>() {

            // Executed in background thread
            public Void doInBackground() {

                    generateProcessing.setEnabled(false);
                    progressBar.setIndeterminate(true);

                                            //omitted

                        timeSlicerToProcessing.setBurnIn(Integer
                                .valueOf(burnInParser.getText()));

                                                    timeSlicerToProcessing.Impute(); //lengthy computations

                        timeSlicerToProcessing.init();

                return null;
            }// END: doInBackground()

            // Executed in event dispatch thread
            public void done() {

                System.out.println("Finished. \n");
                generateProcessing.setEnabled(true);
                progressBar.setIndeterminate(false);

            }// END: done
        };

        worker.execute();
    }
}// END: ListenGenerateProcessing

Edit:

Processing does it's plotting in a draw() method, You don't call it directly, from what I know both setup() and draw() get called on init()

This essentially looks like this:

public class MyProcessingSketch extends PApplet { 
  public void setup () { 
    size( 200, 200 ); 
    background(100); 
  } 

  public void draw () { 
    drawStuff(); 
  } 
}


One problem I see is that this code:

generateProcessing.setEnabled(false);
progressBar.setIndeterminate(true);

should be called in the actionPerformed method before the SwingWorker is declared and started, as it needs to be called on the EDT. Your code snippet however doesn't show us where the animation is done, so it's difficult to say what else could be wrong with it.

Edit:
Based on your latest comments (which I've added to your original post) I'm still not sure what the problem is, mostly due to my ignorance regarding how pApplets work, however I wonder if the init method should be called on the EDT, perhaps in the SwingWorker's done method.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜