/** Changes SimpleApplet10 to implement a GridBagLayout
 ** (version 1.1 of the AWT) and uses multiple classes.
 **
 ** Modified to SimpleApplet11 by John Pais, June 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:  SimpleApplet11.html
 
/** USE EXISTING JAVA CLASSES **/
 
import java.awt.*;
import java.awt.event.*;                            // Needed for version 1.1 of the AWT.
import java.applet.Applet;
 
/** EXTEND THE JAVA APPLET CLASS **/

public class SimpleApplet11 extends Applet
 {backgroundChangeButtons button;                               // Declare instance variables:
  Canvas colorCanvas;                                                   // backgroundChangeButtons (a 2nd class),
  colorChangess RGBcolorChange, HSBcolorChange;     // a Canvas, and two colorChangess
                                                                                    //  instances (a 3rd class).
  Button redButton,greenButton,blueButton,
             whiteButton,grayButton,blackButton,
             yellowButton,magentaButton,cyanButton,
             pinkButton;                                                       // Declare the buttons, and define
  Font font = new Font("TimesRoman",Font.BOLD,20);    // the font for the window text.
  FontMetrics fontm = getFontMetrics(font);
 
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:  SimpleApplet11.html
 
  public void init()
   {setBackground(Color.white);
    GridBagLayout gridbag = new GridBagLayout();                        // Define a GridBagLayout
    GridBagConstraints constraints = new GridBagConstraints();       // and GridBagConstraints.
    setLayout(gridbag);                                                                   // Note that it is DIFFICULT
                                                                                                    // to understand the total
   /***************************************************       // cumulative effect of
    ** DEFAULT VALUES OF GridBagConstraints                       // several components and
    ***************************************************       // their constraints.
    ** constraints.anchor = GridBagConstraints.CENTER;  [EAST,NORTH,NORTHEAST,NORTHWEST,etc.]
    ** constraints.fill = GridBagConstraints.NONE;      [BOTH,HORIZONTAL,VERTICAL]
    ** constraints.gridx = GridBagConstraints.RELATIVE; [0,1,2,3,4,etc.]
    ** constraints.gridy = GridBagConstraints.RELATIVE; [0,1,2,3,4,etc.]
    ** constraints.gridwidth = 1;                       [RELATIVE,REMAINDER,1,2,3,4,etc.]
    ** constraints.gridheight = 1;                      [RELATIVE,REMAINDER,1,2,3,4,etc.]
    ** constraints.ipadx = 0;                             [1,2,3,4,etc.]
    ** constraints.ipady = 0;                             [1,2,3,4,etc.]
    ** constraints.insets = new Insets(0,0,0,0);   [1,2,3,4,etc.]
    ** constraints.weightx = 0.0;                       [floats]
    ** constraints.weighty = 0.0;                       [floats]
    ***************************************************/
 
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:  SimpleApplet11.html
 
   /*************************************************************************
    ** GridBagConstraints for Red, Green, and Blue buttons (Begin First Row)
    *************************************************************************/
    constraints.anchor = GridBagConstraints.CENTER;
    constraints.fill = GridBagConstraints.BOTH;             // Fill grid cell in both directions.
    constraints.gridx = GridBagConstraints.RELATIVE;
    constraints.gridy = GridBagConstraints.RELATIVE;
    constraints.gridwidth = 1;
    constraints.gridheight = 1;
    constraints.ipadx = 0;
    constraints.ipady = 0;
    constraints.insets = new Insets(2,2,2,2);                     // Space 2 pixels between buttons.
    constraints.weightx = 1.0;                                         // Use one grid cell per button in first row.
    constraints.weighty = 0.0;
 
    redButton = new Button("Red");                                    // Create the labeled buttons, and
    button = new backgroundChangeButtons(this,Color.red); // the corresponding backgroundChangeButtons
    redButton.addActionListener(button);                             // color objects to carry the color change.
    gridbag.setConstraints(redButton,constraints);                 // Register listeners and add the Buttons
    add(redButton);                                                             // to the GridBagLayout.
 
    greenButton = new Button("Green");
    button = new backgroundChangeButtons(this,Color.green);
    greenButton.addActionListener(button);
    gridbag.setConstraints(greenButton,constraints);
    add(greenButton);
 
    blueButton = new Button("Blue");
    button = new backgroundChangeButtons(this,Color.blue);
    blueButton.addActionListener(button);
    gridbag.setConstraints(blueButton,constraints);
    add(blueButton);
 
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:  SimpleApplet11.html
 
   /*******************************************************
    ** GridBagConstraints for White button (End First Row)
    *******************************************************/
    constraints.anchor = GridBagConstraints.CENTER;
    constraints.fill = GridBagConstraints.BOTH;                       // Fill each grid cell in both directions.
    constraints.gridx = GridBagConstraints.RELATIVE;
    constraints.gridy = GridBagConstraints.RELATIVE;
    constraints.gridwidth = GridBagConstraints.REMAINDER; // End the first row with this button.
    constraints.gridheight = 1;
    constraints.ipadx = 0;
    constraints.ipady = 0;
    constraints.insets = new Insets(2,2,2,2);        // Space 2 pixels between buttons.
    constraints.weightx = 1.0;                            // Use one grid cell per button in current row.
    constraints.weighty = 0.0;
 
    whiteButton = new Button("White");
    button = new backgroundChangeButtons(this,Color.white);
    whiteButton.addActionListener(button);
    gridbag.setConstraints(whiteButton,constraints);
    add(whiteButton);
 
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:  SimpleApplet11.html
 
   /********************************************************
    ** GridBagConstraints for Gray button (Begin Second Row)
    *********************************************************/
    constraints.anchor = GridBagConstraints.CENTER;
    constraints.fill = GridBagConstraints.BOTH;                       // Fill each grid cell in both directions.
    constraints.gridx = GridBagConstraints.RELATIVE;
    constraints.gridy = GridBagConstraints.RELATIVE;
    constraints.gridwidth = GridBagConstraints.REMAINDER; // End the second row with this button.
    constraints.gridheight = 1;
    constraints.ipadx = 0;
    constraints.ipady = 0;
    constraints.insets = new Insets(2,2,2,2);                              // Space 2 pixels between buttons.
    constraints.weightx = 0.0;                                                  // Automatically determine cells per button (Default).
    constraints.weighty = 0.0;
 
    grayButton = new Button("Gray");
    button = new backgroundChangeButtons(this,Color.gray);
    grayButton.addActionListener(button);
    gridbag.setConstraints(grayButton,constraints);
    add(grayButton);
 
   /********************************************************
    ** GridBagConstraints for Black button (Begin Third Row)
    *********************************************************/
    constraints.anchor = GridBagConstraints.CENTER;
    constraints.fill = GridBagConstraints.BOTH;                   // Fill each grid cell in both directions.
    constraints.gridx = GridBagConstraints.RELATIVE;
    constraints.gridy = GridBagConstraints.RELATIVE;
    constraints.gridwidth = GridBagConstraints.RELATIVE; // Set relative to rest of grid (Default).
    constraints.gridheight = 1;
    constraints.ipadx = 0;
    constraints.ipady = 0;
    constraints.insets = new Insets(2,2,2,2);     // Space 2 pixels between buttons.
    constraints.weightx = 0.0;                         // Automatically determine cells per button (Default).
    constraints.weighty = 0.0;
 
    blackButton = new Button("Black");
    button = new backgroundChangeButtons(this,Color.black);
    blackButton.addActionListener(button);
    gridbag.setConstraints(blackButton,constraints);
    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:  SimpleApplet11.html
 
   /********************************************************
    ** GridBagConstraints for Yellow button (End Third Row)
    *********************************************************/
    constraints.anchor = GridBagConstraints.CENTER;
    constraints.fill = GridBagConstraints.BOTH;                       // Fill each grid cell in both directions.
    constraints.gridx = GridBagConstraints.RELATIVE;
    constraints.gridy = GridBagConstraints.RELATIVE;
    constraints.gridwidth = GridBagConstraints.REMAINDER; // End the third row with this button.
    constraints.gridheight = 1;
    constraints.ipadx = 0;
    constraints.ipady = 0;
    constraints.insets = new Insets(2,2,2,2);  // Space 2 pixels between buttons.
    constraints.weightx = 0.0;                      // Automatically determine cells per button (Default).
    constraints.weighty = 0.0;
 
    yellowButton = new Button("Yellow");
    button = new backgroundChangeButtons(this,Color.yellow);
    yellowButton.addActionListener(button);
    gridbag.setConstraints(yellowButton,constraints);
    add(yellowButton);
 
   /***********************************************************************
    ** GridBagConstraints for Magenta button (Begin Fourth and Fifth Rows)
    ***********************************************************************/
    constraints.anchor = GridBagConstraints.CENTER;
    constraints.fill = GridBagConstraints.BOTH;              // Fill each grid cell in both directions.
    constraints.gridx = GridBagConstraints.RELATIVE;
    constraints.gridy = GridBagConstraints.RELATIVE;
    constraints.gridwidth = 1;                                          // Begin fourth row with this button and set
    constraints.gridheight = 2;                                         // button height to span cell 1 in rows 4 and 5.
    constraints.ipadx = 0;
    constraints.ipady = 0;
    constraints.insets = new Insets(2,2,2,2);                    // Space 2 pixels between buttons.
    constraints.weightx = 0.0;                                        // Automatically determine cells per button (Default).
    constraints.weighty = 1.0;                                        // Since this button spans rows 4 and 5, this weighty
                                                                                  // value gets assigned to row 5.
 
    magentaButton = new Button("Magenta");
    button = new backgroundChangeButtons(this,Color.magenta);
    magentaButton.addActionListener(button);
    gridbag.setConstraints(magentaButton,constraints);
    add(magentaButton);
 
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:  SimpleApplet11.html
 
   /*******************************************************
    ** GridBagConstraints for Cyan button (End Fourth Row)
    *******************************************************/
    constraints.anchor = GridBagConstraints.CENTER;
    constraints.fill = GridBagConstraints.BOTH;                       // Fill each grid cell in both directions.
    constraints.gridx = GridBagConstraints.RELATIVE;
    constraints.gridy = GridBagConstraints.RELATIVE;
    constraints.gridwidth = GridBagConstraints.REMAINDER; // End the fourth row with this button.
    constraints.gridheight = 1;                                                   // Reset to span 1 row (Default).
    constraints.ipadx = 0;
    constraints.ipady = 0;
    constraints.insets = new Insets(2,2,2,2);   // Space 2 pixels between buttons.
    constraints.weightx = 0.0;                       // Automatically determine cells per button (Default).
    constraints.weighty = 0.0;                       // Reset to Default.
 
    cyanButton = new Button("Cyan");
    button = new backgroundChangeButtons(this,Color.cyan);
    cyanButton.addActionListener(button);
    gridbag.setConstraints(cyanButton,constraints);
    add(cyanButton);
 
   /******************************************************
    ** GridBagConstraints for Pink button (End Fifth Row)
    ******************************************************/
    constraints.anchor = GridBagConstraints.CENTER;
    constraints.fill = GridBagConstraints.BOTH;                        // Fill each grid cell in both directions.
    constraints.gridx = GridBagConstraints.RELATIVE;
    constraints.gridy = GridBagConstraints.RELATIVE;
    constraints.gridwidth = GridBagConstraints.REMAINDER;  // End the fourth row with this button.
    constraints.gridheight = 1;                                                    // Reset to span 1 row (Default).
    constraints.ipadx = 0;
    constraints.ipady = 0;
    constraints.insets = new Insets(2,2,2,2);   // Space 2 pixels between buttons.
    constraints.weightx = 0.0;                       // Automatically determine cells per button (Default).
    constraints.weighty = 0.0;                       // Reset to Default.
 
    pinkButton = new Button("Pink");
    button = new backgroundChangeButtons(this,Color.pink);
    pinkButton.addActionListener(button);
    gridbag.setConstraints(pinkButton,constraints);
    add(pinkButton);
 
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:  SimpleApplet11.html
 
   /*******************************************************
    ** GridBagConstraints for colorCanvas (Begin Sixth Row)
    *******************************************************/
    constraints.anchor = GridBagConstraints.CENTER;
    constraints.fill = GridBagConstraints.BOTH;              // Fill grid cell in both directions.
    constraints.gridx = GridBagConstraints.RELATIVE;
    constraints.gridy = GridBagConstraints.RELATIVE;
    constraints.gridwidth = 2;                                          // Begin row 6 with this colorCanvas and set
    constraints.gridheight = 1;                                         // the width to span columns 1 and 2 in row 6.
    constraints.ipadx = 0;
    constraints.ipady = 0;
    constraints.insets = new Insets(2,2,2,2);                     // Space 2 pixels between buttons.
    constraints.weightx = 0.0;
    constraints.weighty = 0.0;
 
    colorCanvas = new Canvas();                                   // Initialize Canvas for painting colors
    colorCanvas.setBackground(Color.black);                  // with a black background color.
    gridbag.setConstraints(colorCanvas,constraints);
    add(colorCanvas);

   /***************************************************************
    ** GridBagConstraints for RGBcolorChange (Third Cell Sixth Row)
    ***************************************************************/
    constraints.anchor = GridBagConstraints.CENTER;
    constraints.fill = GridBagConstraints.BOTH;               // Fill grid cell in both directions.
    constraints.gridx = GridBagConstraints.RELATIVE;
    constraints.gridy = GridBagConstraints.RELATIVE;
    constraints.gridwidth = 1;                                           // Reset to span 1 column (Default).
    constraints.gridheight = 1;
    constraints.ipadx = 0;
    constraints.ipady = 0;
    constraints.insets = new Insets(2,2,2,2);                       // Space 2 pixels between buttons.
    constraints.weightx = 0.0;
    constraints.weighty = 0.0;
 
    RGBcolorChange = new colorChangess(                    // Initialize a Panel (defined in class below)
                                    this,"Red","Green","Blue");         // for RGB changes.
    gridbag.setConstraints(RGBcolorChange,constraints);
    add(RGBcolorChange);
 
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:  SimpleApplet11.html
 
   /********************************************************
    ** GridBagConstraints for HSBcolorChange (End Sixth Row)
    ********************************************************/
    constraints.anchor = GridBagConstraints.CENTER;
    constraints.fill = GridBagConstraints.BOTH;                         // Fill each grid cell in both directions.
    constraints.gridx = GridBagConstraints.RELATIVE;
    constraints.gridy = GridBagConstraints.RELATIVE;
    constraints.gridheight = GridBagConstraints.REMAINDER;  // End the sixth row with this Panel.
    constraints.gridheight = 1;
    constraints.ipadx = 0;
    constraints.ipady = 0;
    constraints.insets = new Insets(2,2,2,2);                                // Space 2 pixels between buttons.
    constraints.weightx = 0.0;
    constraints.weighty = 0.0;
    HSBcolorChange = new colorChangess(                              // Initialize another Panel for HSB
                                    this,"Hue","Saturation","Brightness");    // changes.
    gridbag.setConstraints(HSBcolorChange,constraints);
    add(HSBcolorChange);
    }

  public Insets getInsets()                                 // Set all (top, left, bottom, right)
   {return new Insets(50,10,50,10);}                  // outer boundary insets of GridBagLayout:
                                                                      // 50 pixels on top and bottom for
                                                                      // user instructions text, and 10
                                                                      // pixels at left and at right.
 
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:  SimpleApplet11.html
 
  /** CREATE THE CURRENT FRAME **/
 
  public void paint(Graphics g)
   {g.setFont(font);
    String toptext = "Click on a button to change the background color.";
    String bottext = "Change values and press [ENTER].";
 
    int topxstart = (size().width - fontm.stringWidth(toptext))/2;
    int botxstart = (size().width - fontm.stringWidth(bottext))/2;
 
    int fontA = fontm.getAscent();                   // Contrary to the usual
    int fontD = fontm.getDescent();                 // description, the Ascent
    int fontL = fontm.getLeading();                  // of a font contains white
                                                                    // space at the top.
    int fontAwhite = fontD - fontL;                  // The Ascent white space.
    int fontAtext = fontA - fontAwhite;            // The Ascent text space.
    int fonttext = fontAtext + fontD;                // The actual text space.
 
    int topyspacing = (50 - fonttext)/2;            // Compute vertical spacing.
    int botyspacing = topyspacing;
 
    int topystart = topyspacing + fonttext;
    int botystart = (size().height -50) + botyspacing + fonttext;
 
    if (getBackground() == Color.blue)           // Make sure window text
     g.setColor(Color.black);                          // is visible on every
    else if (getBackground() == Color.gray)    // choice of background
     g.setColor(Color.white);                          // color.
    else g.setColor(Color.blue);

    g.drawString(toptext,topxstart,topystart);
    g.drawString(bottext,botxstart,botystart);
    }
 
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:  SimpleApplet11.html
 
  /** THE REPAINT COLOR CANVAS METHOD **/                   // This method is called from
                                                                                                           // the colorChangess class below,
  void repaint_colorCanvas(colorChangess currentChange)                   // in response to an action
                                                                                                           // Event or a LOST_FOCUS Event.
   {int newval1 = Integer.parseInt(currentChange.newstr1.getText());    // Either a TextField has
    int newval2 = Integer.parseInt(currentChange.newstr2.getText());     // changed or a LOST_FOCUS
    int newval3 = Integer.parseInt(currentChange.newstr3.getText());     // has occurred. Data from
                                                                                                           // the affected Panel (RGB
                                                                                                           // or HSB) is taken here.
    Color newRGBcolor = null;                                                             // Declare the new (refreshed)
                                                                                                           // color of the Canvas.
    if (currentChange == RGBcolorChange)
     {newRGBcolor = new Color(newval1,newval2,newval3);               // New Canvas color.
      float HSB[] = Color.RGBtoHSB(                                                  // Convert RGB values to
                               newval1, newval2, newval3,(new float[3]));          // standard HSB values.
      HSB[0] *= 360;                                                                           // Rescale H value to 360
      HSB[1] *= 100;                                                                           // color wheel, and S & B
      HSB[2] *= 100;                                                                           // values each to a percent.
 
      HSBcolorChange.newstr1.setText(String.valueOf((int) HSB[0]));   // Reset HSB TextFields.
      HSBcolorChange.newstr2.setText(String.valueOf((int) HSB[1]));
      HSBcolorChange.newstr3.setText(String.valueOf((int) HSB[2]));
      }
    else if (currentChange == HSBcolorChange)
     {newRGBcolor = Color.getHSBColor(                                           // Change rescaled HSB
                                                (float) newval1/360,                             // values back to standard
                                                (float) newval2/100,                             // HSB values to convert to
                                                (float) newval3/100);                            // RGB and create new Canvas
                                                                                                            // color.
      RGBcolorChange.newstr1.setText(String.valueOf(newRGBcolor.getRed())); // Reset RGB TextFields.
      RGBcolorChange.newstr2.setText(String.valueOf(newRGBcolor.getGreen()));
      RGBcolorChange.newstr3.setText(String.valueOf(newRGBcolor.getBlue()));
      }
    colorCanvas.setBackground(newRGBcolor);                                    // Set Canvas to new
    colorCanvas.repaint();                                                                      // color and 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:  SimpleApplet11.html
 
/*********************************************
 **            END OF CLASS SimpleApplet11                **
 *********************************************
 **BEGIN 2ND CLASS: backgroundChangeButtons    **
 *********************************************/
 
  class backgroundChangeButtons implements ActionListener   // This is the second class in this file.
   {Color newBackgroundColor;                                              // You can have as many classes as you
    SimpleApplet11 callingApplet;                                             // want in a file, but only one can be
                                                                                               // public, in this case SimpleApplet11.
    backgroundChangeButtons(SimpleApplet11 methodArg,     // Generic constructor method for creating
                                             Color newColor)                       // the backgroung color change objects.
     {callingApplet = methodArg;                                              // Note the arcane way in which the
      newBackgroundColor = newColor;                                   // method and class are setup to
      }                                                                                      // communicate with SimpleApplet11.
 
  public void actionPerformed(ActionEvent evt)                       // Uses version 1.1 of the AWT.
    {if (evt.getSource() instanceof Button)
      callingApplet.setBackground(newBackgroundColor);
      callingApplet.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:  SimpleApplet11.html
 
/*********************************************
 **  END 2ND CLASS: backgroundChangeButtons  **
 *********************************************
 **         BEGIN 3RD CLASS: colorChangess          **
 *********************************************/
 
class colorChangess extends Panel
                                implements FocusListener,      // Need for version 1.1 of the AWT.
                                                  ActionListener
 {TextField newstr1, newstr2, newstr3;
  SimpleApplet11 callingApplet;
 
  colorChangess(SimpleApplet11 methodArg,        // Generic constructor method for creating both
                        String label1,                                 // RGB and HSB Panels and their corresponding
                        String label2,                                 // Labels (see init() method above).
                        String label3)                                // Note the arcane way in which the colorChangess
                                                                            // method and class are setup to communicate
   {callingApplet = methodArg;                              // with SimpleApplet11.

     setLayout(new GridLayout(3,1,10,10));           // Initialize the Panel (RGB or HSB) using a
                                                                            // Gridlayout with 3 rows and 1 column for each.
    newstr1 = new TextField("0");                           // Initialize editable TextFields.
    newstr2 = new TextField("0");
    newstr3 = new TextField("0");
 
    add(new Label(label1, Label.RIGHT));             // Add Labels and TextFields to panel.
    newstr1.addFocusListener(this);
    newstr1.addActionListener(this);
    add(newstr1);

    add(new Label(label2, Label.RIGHT));
    newstr2.addFocusListener(this);
    newstr2.addActionListener(this);
    add(newstr2);

    add(new Label(label3, Label.RIGHT));
    newstr3.addFocusListener(this);
    newstr3.addActionListener(this);
    add(newstr3);
    }
 
  public Insets getInsets()                                        // Set top and bottom outer boundary
   {return new Insets(10,0,10,0);}                           // insets of Gridlayout to 10 pixels.

  public void focusGained(FocusEvent evt){}          // Uses version 1.1 of the AWT.
 
  public void focusLost(FocusEvent evt)                  // Uses version 1.1 of the AWT.
   {callingApplet.repaint_colorCanvas(this);}
 
  public void actionPerformed(ActionEvent evt)       // Uses version 1.1 of the AWT.
   {if (evt.getSource() instanceof TextField)
     {callingApplet.repaint_colorCanvas(this);}
    }
  }
 
/*********************************************
 **           END 3RD CLASS: colorChangess           **
 *********************************************/
 
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:  SimpleApplet11.html