import java.applet.Applet; import java.awt.*; /** * Write a description of the applet class Rhythms1 here. * * @author (your name) * @version (a version number or a date) */ public class Rhythms2 extends Applet implements Runnable { // instance variables - replace the example below with your own private boolean timeToQuit = false; private Thread beat1; private boolean downbeat1 = true; private int onPeriod1 = 500; private int offPeriod1 = 1500; private Thread beat2; private boolean downbeat2 = true; private int onPeriod2 = 500; private int offPeriod2 = 2500; /** * Called by the browser or applet viewer to inform this Applet that it * has been loaded into the system. It is always called before the first * time that the start method is called. */ public void init() { // provide any initialisation necessary for your Applet beat1 = new Thread(this); beat2 = new Thread(this); } /** * Called by the browser or applet viewer to inform this Applet that it * should start its execution. It is called after the init method and * each time the Applet is revisited in a Web page. */ public void start() { // provide any code requred to run each time // web page is visited beat1.start(); beat2.start(); } /** * This may be the most important method in your applet: Here, the * drawing of the applet gets done. "paint" gets called everytime the * applet should be drawn on the screen. So put the code here that * shows the applet. * * @param g the Graphics object for this applet */ public void paint(Graphics g) { // simple text displayed on applet if (downbeat1) g.setColor(Color.red); else g.setColor(Color.black); g.fillOval(0, 0, 200, 100); if (downbeat2) g.setColor(Color.blue); else g.setColor(Color.black); g.fillOval(200, 0, 200, 100); g.setColor(Color.black); g.drawString("Rhythms Applet", 20, 130); } /** * Called by the browser or applet viewer to inform this Applet that * it should stop its execution. It is called when the Web page that * contains this Applet has been replaced by another page, and also * just before the Applet is to be destroyed. If you do not have any * resources that you need to release (such as threads that you may * want to stop) you can remove this method. */ public void stop() { // provide any code that needs to be run when page // is replaced by another page or before Applet is destroyed } /** * Called by the browser or applet viewer to inform this Applet that it * is being reclaimed and that it should destroy any resources that it * has allocated. The stop method will always be called before destroy. * If you do not have any resources that you need to release you can * remove this method. */ public void destroy() { // provide code to be run when Applet is about to be destroyed. timeToQuit = true; beat1.interrupt(); } public void run() { while(! timeToQuit) { try { if (Thread.currentThread() == beat1) { downbeat1 = true; repaint(); beat1.sleep(onPeriod1); downbeat1 = false; repaint(); beat1.sleep(offPeriod1); } else if (Thread.currentThread() == beat2) { downbeat2 = true; repaint(); beat2.sleep(onPeriod2); downbeat2 = false; repaint(); beat2.sleep(offPeriod2); } } catch(InterruptedException e) {} } } }