1  import java.awt.*;
  2  import java.applet.*;
  3  
  4  public class BinomialFall extends Applet implements Runnable {
  5  
  6    Thread thread; 
  7    Ball green, red, purple;
  8    Image needle;
  9  //  int late = 0;
 10    boolean started = false;
 11  
 12    Image appletImg;
 13    Graphics appletG;
 14  
 15  //  public void init() {
 16  //  }
 17  
 18    public void start() {
 19      appletImg = createImage(size().width, size().height);
 20      appletG = appletImg.getGraphics();
 21  
 22      green = new Ball("green", getImage(getDocumentBase(), "greenball.gif"), this, getBackground());
 23      red = new Ball("red", getImage(getDocumentBase(), "redball.gif"), this, getBackground());
 24      purple = new Ball("purple", getImage(getDocumentBase(), "purpleball.gif"), this, getBackground());
 25      needle = getImage(getDocumentBase(), "blackball.gif");
 26    
 27      started = true;
 28      drawBackground(appletG);
 29  
 30      if(thread == null) {
 31        thread = new Thread(this);
 32        thread.start();
 33      }
 34  //    repaint();
 35    }
 36  
 37    public void stop() {
 38      if(thread != null) {
 39        thread.stop();
 40        thread = null;
 41      }
 42    }
 43    
 44    public void run() {
 45      while(thread != null) {
 46        repaint();
 47        try { thread.sleep(300); }
 48        catch (InterruptedException e) {}
 49      }
 50    }  
 51  
 52    public void update(Graphics g) {
 53      drawBackground(appletG);
 54      green.move(appletG);
 55  //    if (late++ > 5)
 56      red.move(appletG);
 57      purple.move(appletG);
 58  //    if(late > 6) late = 6;
 59      paint(g);
 60    }
 61  
 62    public void drawBackground(Graphics g) {
 63      int level;
 64      int x = 0,y = 0,i,j;
 65      int u = 25;
 66      int margin;
 67    
 68      Rectangle r = bounds();
 69    
 70      g.setColor(getBackground());
 71      g.fillRect(0,0, r.width, r.height);
 72      g.setColor(getForeground());
 73    
 74      g.drawRect(0,0, r.width - 1, r.height - 1);
 75      if (r.height < r.width) {
 76        level = (r.height - 4*u)/u;
 77      } else {
 78        level = (r.width - u)/(2*u);
 79      }
 80      margin = (r.width - (level)*2*u)/2;
 81      for (j = r.height - 2*u; j > 2*u; j -= u, margin += u) {
 82        for (i = margin; i < r.width - margin - u; i += 2*u) {
 83          x = i; y = j;
 84          g.drawImage(needle, x, y, this);
 85        }
 86      }
 87    
 88      if (started) {
 89        started = false;
 90    
 91        green.setdelta(u,u);
 92        green.setVars(x, y, level, 0);
 93        green.move(g);
 94    
 95        red.setdelta(u,u);
 96        red.setVars(x, y, level, 2);
 97        red.move(g);
 98    
 99        purple.setdelta(u,u);
100        purple.setVars(x, y, level, 4);
101        purple.move(g);
102    
103  //      late = 0;
104      };
105    }
106  
107    public void paint(Graphics g) {
108      g.drawImage(appletImg, 0, 0, this);
109    }
110    
111  }