     // An applet that says "Hello World" in a big bold font,
     // with a button to change the color of the message.
     
     import java.awt.*;         // Defines basic classes for GUI programming.
     import java.awt.event.*;   // Defines classes for working with events.
     import java.applet.*;      // Defines the applet class.
     
     public class ColoredHelloWorldApplet
                       extends Applet implements ActionListener {
             // Defines a subclass of Applet.  The "implements ActionListener"
             // part says that objects of type ColoredHelloApplet are
             // capable of listening for ActionEvents.  This is necessary
             // if the applet is to respond to events from the button.
     
        int colorNum;     // Keeps track of which color is displayed;
                          //     1 for red, 2 for blue, 3 for green.
                          
        Font textFont;    // The font in which the message is displayed.
                          // A font object represent a certain size and
                          // style of text drawn on the screen.
        
        
        public void init() {
     
               // This routine is called by the system to initialize the applet.
               // It sets up the font and initial color for the message and 
               // it adds a button to the apple for changing the message color.
               
            setBackground(Color.lightGray);
                  // The applet is filled with the background color before
                  // the paint method is called.  The button and the message
                  // in this applet will appear on a light gray background.
     
            colorNum = 1;   // The color of the message is set to red.
            
            textFont = new Font("Serif",Font.BOLD,24);
                  // Create a font object representing a big, bold font.
     
            Button bttn = new Button("Change Color");
                  // Create a new button.  "ChangeColor" is the text
                  // displayed on the button.

            bttn.addActionListener(this);  
                    // Set up the bttn to send an "action event" to this applet
                    // when the user clicks the button.  The parameter, this,
                    // is a name for the applet object that we are creating.
                    
            add(bttn);  // Add the button to the applet, so that it 
                        // will appear on the screen.
     
        }  // end init()
        
        
        public void paint(Graphics g) {
              
              // This routine is called by the system whenever the content of the
              // applet needs to be drawn or redrawn.  It displays the message
              // "Hello World" in the proper color and font.
     
           switch (colorNum) {         // Set the color.
              case 1:
                 g.setColor(Color.red);
                 break;
              case 2:
                 g.setColor(Color.blue);
                 break;
              case 3:
                 g.setColor(Color.green);
                 break;
           }
           
           g.setFont(textFont);       // Set the font.
           
           g.drawString("Hello World!", 20,70);    // Draw the message.
     
        }  // end paint()
        
     
        public void actionPerformed(ActionEvent evt) {
        
              // This routine is called by the system when the user clicks
              // on the button.  The response is to change the colorNum
              // which determines the color of the message, and to call
              // repaint() to see that the applet is redrawn with the
              // new color.
              
           if (colorNum == 1)       // Change colorNum.
              colorNum = 2;
           else if (colorNum == 2)
              colorNum = 3;
           else
              colorNum = 1;

           repaint();  // Tell system that this applet needs to be redrawn
           
        }  // end init()
        
     } // end class ColoredHelloWorldApplet
