// this version works with Java 1.0 (VJ++ 1.1) import java.awt.*; import java.applet.*; public class RedGreenV10 extends // Frame Applet { public void init() // public RedGreenV10() { setLayout(new BorderLayout(2,2)); b = new Button("start"); c = new myCanvas(); add("West", b); add("Center", c); } // constructor public boolean handleEvent(Event ae) { if (ae.id == Event.WINDOW_DESTROY) System.exit(0); return super.handleEvent(ae); } // handleEvent public boolean action(Event ae, Object s) { if (s.equals("start")) handleStart(); else if (s.equals("stop")) handleStop(); else return super.action(ae, s); return true; } // action private void handleStart() { b.setLabel("stop"); c.start(); } // handleStart private void handleStop() { b.setLabel("start"); c.stop(); } // handlStop /** public static void main(String[] args) { Frame f = new RedGreenV10(); f.resize(300, 200); f.show(); } // main **/ // instance variables private Button b; private myCanvas c; } // RedGreen class myCanvas extends Canvas { public void paint(Graphics gc) { if (color == 0) gc.setColor(new Color(200, 50, 50)); // red else gc.setColor(new Color(50, 200, 50)); // green gc.fillRect(5,5,90,40); } // paint public void start() { color = 1; repaint(); } public void stop() { color = 0; repaint(); } private int color = 0; // 0 == red, 1 == green }