/*

Stepper

Files: .html, .java, .class
Click in applet window to execute one step.
  What happens when this applet runs:
  1. When user clicks in window, mouseClicked() is called.
  2. This starts thread1.
  3. Within thread1, run() is called.
  4. Within thread1, run() performs one step and calls myPause().
  5. Within thread1, myPause() triggers repaint().
  -  It calls notify(), which relinquishes the thread ...?
  -  and then wait()'s... ?
  6. Now thread1 is waiting.  However, the system is still running,
  -  and because repaint() was called, paint() gets called,
  -  and image1 gets displayed.
  7. User clicks again.  This calls myResume().
  8. myResume() calls notify().  This relinquishes control of thread1....?
  -  to myPause() ...? which now finishes its wait ...? and returns,
  -  so that run() can proceed to the next iteration.
  9. Go back to step 4.
  10. After 10 steps, the loop in run() terminates.  Now nothing happens.

  Alternative to step 7:  milliseconds elapse; myPause() stops
  waiting, and returns, so run() proceeds to next iteration.
  
*/ import java.awt.*; import java.awt.event.*; import java.applet.*; //&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&& public class stepper extends Applet // implements Runnable, ComponentListener, MouseListener { Thread thread1=null; Image image1; int width1,height1,dx,dy,delay; int xmax=256,ymax=xmax,frame; int [][]a = new int[xmax][ymax]; int [][]n = new int[xmax][ymax]; //========== My own methods ========== synchronized public void myPause() { System.out.println ("myPause()"); myUpdate(); repaint(); notify(); try {wait(delay);} catch(Exception e) {} } synchronized public void myResume() { notify(); } public void myPaint(Graphics realg) {realg.drawImage (image1, 0, 0, null);} public void myUpdate() { int x,y; if (image1==null) return; Graphics g=image1.getGraphics(); g.setColor (Color.white); g.fillRect (0, 0, width1, height1); g.setColor(Color.cyan); for (x=0; x