1  /*
  2   *  File:  AppletDemo2.java
  3   *
  4   *  Another applet demonstration:  In addtion to init(), start(),
  5   *  stop(), destroy(), and paint( Graphics ), this applet also
  6   *  traces method update( Graphics ).  What you'll find is that
  7   *  update( Graphics ) isn't *ever* called automatically!  It and
  8   *  repaint() (which calls update) evidently exist for user 
  9   *  applications only.
 10   *  
 11   *  If you load this applet into a browser, inspect the Java 
 12   *  console for messages.  If you load this applet into 
 13   *  appletviewer, messages will be written to standard output.
 14   *
 15   *  Copyright:  Northeast Parallel Architectures Center
 16   *  
 17   */
 18  
 19  import java.applet.Applet;
 20  import java.awt.Font;
 21  import java.awt.Graphics;
 22  
 23  public class AppletDemo2 extends Applet {
 24  
 25    Font f;  // a font
 26    
 27    // Counts the number of method calls:
 28    private int nStart, nUpdate, nPaint, nStop;
 29    
 30    // Stores the message strings:
 31    private String startMsg, updateMsg, paintMsg, stopMsg;
 32    
 33    // Override java.applet.Applet.init():
 34    public void init() {
 35      // Initialize a font:
 36      f = new Font( "SansSerif", Font.BOLD, 18 );
 37      // Initialize counters:
 38      nStart = nUpdate = nPaint = nStop = 0;
 39      // Initialize message strings:
 40      startMsg = "start method called 0 times";
 41      updateMsg = "update method called 0 times";
 42      paintMsg = "paint method called 0 times";
 43      stopMsg = "stop method called 0 times";
 44      // Print a message:
 45      System.out.println( "init() called" );
 46    }
 47    
 48    // Override java.applet.Applet.start():
 49    public void start() {
 50      // Update counter and message string:
 51      startMsg = "start method called " + (++nStart) + " times";
 52      // Print a message:
 53      System.out.println( startMsg );
 54    }
 55    
 56    // Mimic java.awt.Container.update( Graphics ):
 57    public void update( Graphics g ) {
 58      // Update counter and message string:
 59      updateMsg = "update method called " + (++nUpdate) + " times";
 60      // Print a message:
 61      System.out.println( updateMsg );
 62      // Get the size of this applet and clear the foreground:
 63      int w = getSize().width;
 64      int h = getSize().height;
 65      g.setColor( getBackground() );
 66      g.clearRect( 0, 0, w, h );
 67      g.setColor( getForeground() );
 68      // Call the paint method:
 69      paint( g );
 70    }
 71  
 72    // Override java.awt.Container.paint( Graphics ):
 73    public void paint( Graphics g ) {
 74      // Update counter and message string:
 75      paintMsg = "paint method called " + (++nPaint) + " times";
 76      // Print a message:
 77      System.out.println( paintMsg );
 78      // Draw the messages in the applet window:
 79      g.setFont( f );
 80      g.drawString( startMsg, 25, 25 );
 81      g.drawString( updateMsg, 25, 50 );
 82      g.drawString( paintMsg, 25, 75 );
 83      g.drawString( stopMsg, 25, 100 );
 84    }
 85  
 86    // Override java.applet.Applet.stop():
 87    public void stop() {
 88      // Update counter and message string:
 89      stopMsg = "stop method called " + (++nStop) + " times";
 90      // Print a message:
 91      System.out.println( stopMsg );
 92    }
 93    
 94    // Override java.applet.Applet.destroy():
 95    public void destroy() {
 96      // Print a message:
 97      System.out.println( "destroy() called" );
 98    }
 99    
100  }