import java.awt.*; import java.applet.*; public class BinomialFall extends Applet implements Runnable { Thread thread; Ball green, red, purple; Image needle; // int late = 0; boolean started = false; Image appletImg; Graphics appletG; // public void init() { // } public void start() { appletImg = createImage(size().width, size().height); appletG = appletImg.getGraphics(); green = new Ball("green", getImage(getDocumentBase(), "greenball.gif"), this, getBackground()); red = new Ball("red", getImage(getDocumentBase(), "redball.gif"), this, getBackground()); purple = new Ball("purple", getImage(getDocumentBase(), "purpleball.gif"), this, getBackground()); needle = getImage(getDocumentBase(), "blackball.gif"); started = true; drawBackground(appletG); if(thread == null) { thread = new Thread(this); thread.start(); } // repaint(); } public void stop() { if(thread != null) { thread.stop(); thread = null; } } public void run() { while(thread != null) { repaint(); try { thread.sleep(300); } catch (InterruptedException e) {} } } public void update(Graphics g) { drawBackground(appletG); green.move(appletG); // if (late++ > 5) red.move(appletG); purple.move(appletG); // if(late > 6) late = 6; paint(g); } public void drawBackground(Graphics g) { int level; int x = 0,y = 0,i,j; int u = 25; int margin; Rectangle r = bounds(); g.setColor(getBackground()); g.fillRect(0,0, r.width, r.height); g.setColor(getForeground()); g.drawRect(0,0, r.width - 1, r.height - 1); if (r.height < r.width) { level = (r.height - 4*u)/u; } else { level = (r.width - u)/(2*u); } margin = (r.width - (level)*2*u)/2; for (j = r.height - 2*u; j > 2*u; j -= u, margin += u) { for (i = margin; i < r.width - margin - u; i += 2*u) { x = i; y = j; g.drawImage(needle, x, y, this); } } if (started) { started = false; green.setdelta(u,u); green.setVars(x, y, level, 0); green.move(g); red.setdelta(u,u); red.setVars(x, y, level, 2); red.move(g); purple.setdelta(u,u); purple.setVars(x, y, level, 4); purple.move(g); // late = 0; }; } public void paint(Graphics g) { g.drawImage(appletImg, 0, 0, this); } }