/* * Double-buffered version of mRectApplet.java */ import java.applet.*; import java.awt.*; public class mObjApplet extends Applet implements Runnable { // Instance variables: int numObj = 3; // number of objects mPoint object[]; // object array Thread thread; // a thread Image image; // for double buffering // Class method: public void initObjects() { /* initialize object array */ object = new mPoint[numObj]; /* initialize a rectangle */ object[0] = new mRectangle(10, 10, 100, 100); object[0].setDelta(1, 1); object[0].setColor(Color.red); /* initialize a circle */ object[1] = new mCircle(200, 10, 100, 100); object[1].setDelta(-1, 1); object[1].setColor(Color.blue); /* initialize a triangle */ object[2] = new mTriangle(10, 200, 100, 100); object[2].setDelta(-1, 1); object[2].setColor(Color.green); image = createImage(size().width, size().height); } // Override java.applet.Applet.init: public void init () { initObjects(); } // Override java.applet.Applet.start: public void start() { if (thread == null) { // Reinitialize: initObjects(); thread = new Thread(this); thread.start(); } } // Override java.applet.Applet.stop: public void stop() { if (thread != null) { thread.stop(); thread = null; } } // Implement java.lang.Runnable.run: public void run() { while(thread != null) { // repaint() invokes update(): repaint(); try { Thread.sleep(20); } catch(InterruptedException e) { // Do nothing }; } } // Override java.awt.Component.update: public void update(Graphics g) { /* Get Off Screen Graphics Context */ Graphics og = image.getGraphics(); /* fill background */ og.setColor(getBackground()); og.fillRect(1, 1, size().width-2, size().height-2); /* draw boundry */ og.setColor(getForeground()); og.drawRect(0, 0, size().width-1, size().height-1); /* move object */ for (int i=0; i