/** Copyright (c) 1997 Sams.net Publishing. All Rights Reserved.
 ** Teach Yourself Java 1.1, Laura Lemay and Charles L. Perkins.
 ** Adapted from Listing 12.2:  The final ButtonActionsTest
 ** applet (1.02 version).
 **
 ** Modified to SimpleApplet8 by John Pais, January 1998.      **/
 
Toggle your browser's Back and Forward buttons to compare the running applet to this code.
Click here to run a fresh copy of the applet:  SimpleApplet8.html

/** USE EXISTING JAVA CLASSES **/

import java.awt.*;
import java.applet.Applet;

/** EXTEND THE JAVA APPLET CLASS **/

public class SimpleApplet8 extends Applet
   {Button redButton,blueButton,greenButton,
      whiteButton,blackButton;                                                        // Declare the buttons, and
      Font font = new Font("TimesRoman",Font.BOLD,20);              // define the font for the
                                                                                                   // window text.
     public void init()                                                                       // Change the gray background
       {setBackground(Color.white);                                                // to white.
         setLayout(new FlowLayout(FlowLayout.CENTER,10,10));   // Define the Layout Manager
                                                                                                   // for the arrangement of the
         redButton = new Button("Red");                                           // buttons. Create the labeled
         add(redButton);                                                                   // buttons, and add them to
         blueButton = new Button("Blue");                                         // the layout.
         add(blueButton);
         greenButton = new Button("Green");
         add(greenButton);
         whiteButton = new Button("White");
         add(whiteButton);
         blackButton = new Button("Black");
         add(blackButton);
         }
 
Toggle your browser's Back and Forward buttons to compare the running applet to this code.
Click here to run a fresh copy of the applet:  SimpleApplet8.html
 
     /** HANDLE THE BUTTON ACTION EVENT **/
 
     public boolean action(Event evt, Object obj)
        {if (evt.target instanceof Button)                               // Detect a button event, and then
          {changeBackgroundColor((Button) evt.target);        // call the changeBackgroundColor
            return true;}                                                         // method.
          else
           {return false;}
          }
 
     /** THE CHANGE BACKGROUND COLOR METHOD **/
 
     void changeBackgroundColor(Button b)
        {if (b == redButton) setBackground(Color.red);
          else if (b == blueButton) setBackground(Color.blue);
          else if (b == greenButton) setBackground(Color.green);
          else if (b == whiteButton) setBackground(Color.white);
          else setBackground(Color.black);

          repaint();
          }
 
Toggle your browser's Back and Forward buttons to compare the running applet to this code.
Click here to run a fresh copy of the applet:  SimpleApplet8.html
 
     /** CREATE THE CURRENT FRAME **/
 
     public void paint(Graphics g)
        {g.setFont(font);
          if (getBackground() == Color.blue) g.setColor(Color.black);   // Make sure window text
          else g.setColor(Color.blue);                                                  // is visible on every
          g.drawString("To change the background color,",65,85);         // choice of background
          g.drawString("click on a button--that's all this",65,125);           // color.
          g.drawString("applet does.",65,165);
          }
      }