// CS342 Homework #4 // Dan J. Fraser (1219229) import CS342.Hwk4.*; import java.awt.*; /** * Homework 4 class, a fix to the broken applet... */ public class Homework4 extends TheGUI { SpinThread spin_world; // the threadable world... Thread world_thread; // and a variable to hold the thread. int num_threads = 0; // keeps track of how many threads we have running. /*----------------------------------------------------------------------*/ /** * This overrides the default handle_lefthand_button(). It stops the * thread if there is currently one running, otherwise it gracefully does * nothing at all. */ public void handle_lefthand_button() { if (num_threads == 1) { update_label("press righthand button to restart simulation"); num_threads--; world_thread.stop(); spin_world.pause(); world_thread = new Thread(spin_world); world_thread.start(); } } /*----------------------------------------------------------------------*/ /** * This overrides the default handle_righthand_button(). It starts the * thread if there is not one currently running, otherwise it gracefully * does nothing at all. */ public void handle_righthand_button() { if (num_threads == 0) { update_label("press lefthand button to stop simulation"); num_threads++; spin_world = new SpinThread(the_world,the_world_canvas); world_thread = new Thread(spin_world); world_thread.start(); } } } /** * This class creates a threadable version of SpinTheWorld, but with * a feature to pause and unpause the actual updating of the display. */ class SpinThread extends SpinTheWorld implements Runnable { boolean really_update = true; // keeps the state of the pause feature. /*----------------------------------------------------------------------*/ /** * Default constructor. Just calls the superclass constructor. */ public SpinThread(WorldModel the_world, WorldCanvas the_world_canvas) { super(the_world,the_world_canvas); } /*----------------------------------------------------------------------*/ /** * The meat of the thread. Just calls the superclass function which does * the work. */ public void run() { update_canvas_at_regular_times(); } /*----------------------------------------------------------------------*/ /** * Overrides the default update_the_canvas method in the superclass to * implement the pause feature. If really_update is true, things get * updated. Otherwise, we just snooze for a bit. */ public void update_the_canvas() { if (really_update) { the_world.update(the_time_step_size); the_world_canvas.paint(); } else { try { Thread.sleep(the_sleep_interval); } catch (InterruptedException e1) {}; } } /*----------------------------------------------------------------------*/ /** * Part of the pause feature implementation. Just sets really_update to * false. */ public void pause() { really_update = false; } /*----------------------------------------------------------------------*/ /** * Part of the pause feature implementation. Just sets really_update to * true. */ public void unpause() { really_update = true; } }