SwingWorker

This commit is contained in:
Denis Molony 2020-05-17 17:30:31 +10:00
parent 10658f09c2
commit 73ef60954a

View File

@ -21,6 +21,7 @@ import javax.swing.JTabbedPane;
import javax.swing.JTextArea; import javax.swing.JTextArea;
import javax.swing.ScrollPaneConstants; import javax.swing.ScrollPaneConstants;
import javax.swing.SwingConstants; import javax.swing.SwingConstants;
import javax.swing.SwingWorker;
import javax.swing.event.ChangeEvent; import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener; import javax.swing.event.ChangeListener;
@ -65,7 +66,7 @@ public class DataPanel extends JTabbedPane implements DiskSelectionListener,
boolean assemblerTextValid; boolean assemblerTextValid;
DataSource currentDataSource; DataSource currentDataSource;
private Animation animation; private Worker animation;
final MenuHandler menuHandler; final MenuHandler menuHandler;
@ -361,8 +362,8 @@ public class DataPanel extends JTabbedPane implements DiskSelectionListener,
{ {
if (animation != null) if (animation != null)
animation.cancel (); animation.cancel ();
animation = new Animation ((SHRPictureFile2) dataSource); animation = new Worker ((SHRPictureFile2) dataSource);
animation.start (); animation.execute ();
// System.out.println ("new animation"); // System.out.println ("new animation");
} }
} }
@ -553,17 +554,15 @@ public class DataPanel extends JTabbedPane implements DiskSelectionListener,
} }
// ---------------------------------------------------------------------------------// // ---------------------------------------------------------------------------------//
class Animation extends Thread class Worker extends SwingWorker<Void, Integer>
// ---------------------------------------------------------------------------------// // ---------------------------------------------------------------------------------//
{ {
boolean running; volatile boolean running;
SHRPictureFile2 image; SHRPictureFile2 image;
int delay;
public Animation (SHRPictureFile2 image) public Worker (SHRPictureFile2 image)
{ {
this.image = image; this.image = image;
delay = image.getDelay ();
} }
public void cancel () public void cancel ()
@ -572,22 +571,29 @@ public class DataPanel extends JTabbedPane implements DiskSelectionListener,
} }
@Override @Override
public void run () protected Void doInBackground () throws Exception
{ {
running = true; running = true;
try try
{ {
while (running) while (running)
{ {
sleep (delay); Thread.sleep (image.getDelay ());
image.nextFrame (); publish (0);
update ();
} }
} }
catch (InterruptedException e) catch (InterruptedException e)
{ {
e.printStackTrace (); e.printStackTrace ();
} }
return null;
}
@Override
protected void process (List<Integer> chunks)
{
image.nextFrame ();
update ();
} }
} }
} }