1

I've created a program to handle directed graphs. I also used a raster to display the graph. (I'm still working on cleaning it up, although it should be sufficient to show the computations.) enter image description here

I want a pause in the graphing, so as the items change color during the search it's obvious. However, the pause causes the graphing to stop. Anyone know why that'd be? To be honest, I have never worked with Thread before and I don't know what it does to running processes. enter image description here Thanks for any ideas.

The code snippet in question:

public void showEdge (Raster canvas, Vertex target, Color c) {
    [...]
    canvas.setLine (x1,y2,x2,y2,c);
    try {
        Thread.sleep (1000); }
    catch (InterruptedException e) {
        System.out.println ("Problem with the pause.");
    }
}
camickr
  • 321,443
  • 19
  • 166
  • 288
failure
  • 215
  • 1
  • 3
  • 12

1 Answers1

2

However, the pause causes the graphing to stop.

Sounds like your code is executing on the Event Dispatch Thread (EDT). The EDT is the Thread that handles event processing on the GUI. The Thread.sleep() will sleep the EDT which means the GUI can't repaint itself until the processing is finished.

Don't use Thread.sleep() on the EDT.

Instead you need to create a separate Thread. You should probably use a Swing Worker. The Swing Worker will create the separate Thread for you. You can sleep in this Thread and publish intermediate results for updating the GUi.

Read the section from the Swing tutorial on Concurrency for more information and examples of a Swing Worker.

camickr
  • 321,443
  • 19
  • 166
  • 288