1 /* 2 * File: AppletDemo.java 3 * 4 * Applet demonstration: init() and destroy() are called once 5 * and only once, at the beginning and end of the applet's 6 * lifespan, respectively. start() is called immediately after 7 * init() and every time the applet is restarted (after having 8 * been stopped). paint( Graphics ) is always called after 9 * start() and whenever the applet window must be redrawn. 10 * stop() is called immediately before destroy() and every 11 * time the applet is stopped by the browser or appletviewer. 12 * All five of these methods must be public, otherwise the 13 * browser or appletviewer will not be able to call them. 14 * 15 * If you load this applet into a browser, inspect the Java 16 * console for messages. If you load this applet into 17 * appletviewer, messages will be written to standard output. 18 * 19 * Copyright: Northeast Parallel Architectures Center 20 * 21 */ 22 23 import java.applet.Applet; 24 import java.awt.Font; 25 import java.awt.Graphics; 26 27 public class AppletDemo extends Applet { 28 29 Font f; // a font 30 31 // Counts the number of method calls: 32 private int nStart, nPaint, nStop; 33 34 // Stores the message strings: 35 private String startMsg, paintMsg, stopMsg; 36 37 // Override java.applet.Applet.init(): 38 public void init() { 39 // Initialize a font: 40 f = new Font( "SansSerif", Font.BOLD, 18 ); 41 // Initialize counters: 42 nStart = nPaint = nStop = 0; 43 // Initialize message strings: 44 startMsg = "start method called 0 times"; 45 paintMsg = "paint method called 0 times"; 46 stopMsg = "stop method called 0 times"; 47 // Print a message: 48 System.out.println( "init() called" ); 49 } 50 51 // Override java.applet.Applet.start(): 52 public void start() { 53 // Update counter and message string: 54 startMsg = "start method called " + (++nStart) + " times"; 55 // Print a message: 56 System.out.println( startMsg ); 57 } 58 59 // Override java.awt.Container.paint( Graphics ): 60 public void paint( Graphics g ) { 61 // Update counter and message string: 62 paintMsg = "paint method called " + (++nPaint) + " times"; 63 // Print a message: 64 System.out.println( paintMsg ); 65 // Draw the messages in the applet window: 66 g.setFont( f ); 67 g.drawString( startMsg, 25, 25 ); 68 g.drawString( paintMsg, 25, 50 ); 69 g.drawString( stopMsg, 25, 75 ); 70 } 71 72 // Override java.applet.Applet.stop(): 73 public void stop() { 74 // Update counter and message string: 75 stopMsg = "stop method called " + (++nStop) + " times"; 76 // Print a message: 77 System.out.println( stopMsg ); 78 } 79 80 // Override java.applet.Applet.destroy(): 81 public void destroy() { 82 // Print a message: 83 System.out.println( "destroy() called" ); 84 } 85 86 }