// File: IO25.java (Module 13) // // Author: Rahul Simha // Created: Nov 30, 1998 // // Sound import java.awt.*; import java.awt.event.*; import java.applet.*; public class IO25 extends Applet { Canvas c; AudioClip sound; // Use the AudioClip class in java.applet public void init () { // Frame properties. this.setBackground (Color.cyan); // Must set layout of the panel. this.setLayout (new BorderLayout()); Panel p = new Panel(); // Pressing "start" calls race() Button startb = new Button ("START"); startb.addActionListener ( new ActionListener () { public void actionPerformed (ActionEvent a) { race (); } } ); p.add (startb); this.add (p, BorderLayout.SOUTH); // A canvas to draw the results. c = new Canvas(); c.setBackground (Color.white); this.add (c, BorderLayout.CENTER); // Get audio clip sound = this.getAudioClip (this.getDocumentBase(), "soundtrack.au"); this.setVisible (true); } void race () { Dimension D = c.getSize (); // Finish-line is at the right end of the canvas. int finishLine = D.width; // Create two dog instances with different ID's. Dog d1 = new Dog (1, c); Dog d2 = new Dog (2, c); // Create a Thread instance for each dog. // Note: the class Dog must implement the // Runnable interface. Thread d1Thread = new Thread (d1); Thread d2Thread = new Thread (d2); Dog.startRace(); // Create a sound thread. Soundtrack strack = new Soundtrack (sound); Thread sthread = new Thread (strack); // Start running the threads. d1Thread.start(); d2Thread.start(); sthread.start(); } } class Soundtrack implements Runnable { AudioClip sound; public Soundtrack (AudioClip sound) { this.sound = sound; } public void run () { sound.loop(); } } class Dog implements Runnable { public int position = 20; // Starting position. int ID; // An ID. Canvas c; // The canvas on which to draw. static boolean raceOver; public Dog (int ID, Canvas c) { this.ID = ID; this.c = c; // Draw ID on canvas. Graphics g = c.getGraphics (); g.drawString (""+ID, 5, 20*ID+8); } public void move () { // Move a random amount. int newPosition = position + (int) UniformRandom.uniform (20,30); // Draw new position. Graphics g = c.getGraphics (); int size = newPosition - position; g.fillRect (position, 20*ID, size, 10); position = newPosition; } public void run () { // Compute the finish line distance. int finishLine = c.getSize().width; // While not complete... while (position < finishLine) { try { Thread.sleep ((int)UniformRandom.uniform (300,600)); } catch (InterruptedException e) { System.out.println (e); } // Check whether race is over. if (raceFinished (false, ID)) break; // Move if race is still on. move (); } if (position >= finishLine) raceFinished (true, ID); } public static synchronized void startRace () { raceOver = false; } // The same method is called to check whether the race // is complete or to indicate that it is complete. public static synchronized boolean raceFinished (boolean set, int ID) { boolean returnVal = raceOver; if (!set) returnVal = raceOver; else { // Race overseer sleeps for a while. try { Thread.sleep ((int)UniformRandom.uniform (1000,2000)); } catch (InterruptedException e) { System.out.println (e); } raceOver = true; returnVal = true; } return returnVal; } }