// steve eilertsen // Three Gogga objects each running in their own thread // Gogga constructor - x position, y position, direction, colour import java.awt.Color; public class MakeSquareThread { public static void main(String[]args) throws InterruptedException { SquareGoggaThread bug = new SquareGoggaThread(6,5,1,Color.red); SquareGoggaThread beetle = new SquareGoggaThread(5,6,1,java.awt.Color.blue); SquareGoggaThread lice = new SquareGoggaThread(4,7,1,java.awt.Color.green); // Wrap each Gogga in its own Thread. Each SquareGogga // implements Runnable, its run() method (which calls // makeSquare()) becomes the code that the thread executes. Thread bugThread = new Thread(bug); Thread beetleThread = new Thread(beetle); Thread liceThread = new Thread(lice); // Starting all three threads lets bug, // beetle and lice all draw their squares at the same time. bugThread.start(); beetleThread.start(); liceThread.start(); // join() makes main() wait for all three threads to finish // before the program exits, so we don't quit while they are // still mid-square. bugThread.join(); beetleThread.join(); liceThread.join(); } } ============================================================== // steve eilertsen // Three Gogga objects each running in their own thread import it.*; import java.awt.Color; public class SquareGoggaThread extends Gogga implements Runnable { public SquareGoggaThread(int x, int y, int d, Color r) { super(x,y,d,r); } public void makeSquareThread() { for(int loop = 1; loop <=4; loop++) { this.move(); this.move(); this.move(); this.turnLeft(); } } // run() is what a Thread calls when it is started. // By putting the square-drawing loop here (and having it // simply call makeSquare()), each SquareGogga can be handed // to its own Thread and they will all draw at the same time. public void run() { makeSquareThread(); } } ==========================================