/** Changes SimpleApplet11 to include a ScollPane
 ** (version 1.1 of the AWT) with an image, and uses
 ** multiple classes.
 **
 ** Modified to SimpleApplet12 by John Pais, July 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:  SimpleApplet12.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 SimpleApplet12 extends Applet          // Declare instance variables:
 {backgroundChangeButtonss button;                    // backgroundChangeButtonss (a 2nd class),

  scrollableCanvas canvas;                                     // a scrollableCanvas (a 3rd class) that
  String imageFile = "borgcube.jpg";                       // displays an image, and two colorChangesss
  colorChangesss RGBcolorChange, HSBcolorChange;    // instances (a 4th class).

  Color newRGBcolor;
  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:  SimpleApplet12.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]
    ***************************************************/
 
   /******************************************************************************************
    ** GridBagConstraints for Red, Green, Blue, Yellow, and Magenta 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 backgroundChangeButtonss(this,Color.red); // the corresponding backgroundChangeButtonss
    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 backgroundChangeButtonss(this,Color.green);
    greenButton.addActionListener(button);
    gridbag.setConstraints(greenButton,constraints);
    add(greenButton);
 
    blueButton = new Button("Blue");
    button = new backgroundChangeButtonss(this,Color.blue);
    blueButton.addActionListener(button);
    gridbag.setConstraints(blueButton,constraints);
    add(blueButton);
 
    yellowButton = new Button("Yellow");
    button = new backgroundChangeButtonss(this,Color.yellow);
    yellowButton.addActionListener(button);
    gridbag.setConstraints(yellowButton,constraints);
    add(yellowButton);
 
    magentaButton = new Button("Magenta");
    button = new backgroundChangeButtonss(this,Color.magenta);
    magentaButton.addActionListener(button);
    gridbag.setConstraints(magentaButton,constraints);
    add(magentaButton);
 
   /******************************************************
    ** GridBagConstraints for Cyan 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;
 
    cyanButton = new Button("Cyan");
    button = new backgroundChangeButtonss(this,Color.cyan);
    cyanButton.addActionListener(button);
    gridbag.setConstraints(cyanButton,constraints);
    add(cyanButton);
 
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:  SimpleApplet12.html
 
   /*******************************************************************
    ** GridBagConstraints for ScrollPane (Begin Second and Third 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 = GridBagConstraints.REMAINDER; // End the second row with this ScrollPane.
    constraints.gridheight = 2;                                                    // Set height to span rows 2 and 3.
    constraints.ipadx = 0;
    constraints.ipady = 0;
    constraints.insets = new Insets(10,10,10,10);         // Space 10 pixels.
    constraints.weightx = 0.0;
    constraints.weighty = 1.0;                                      // Since this ScrollPane spans rows 2 and 3, this
                                                                                 // weighty value gets assigned to row 3.
 
    ScrollPane scrollPane = new ScrollPane(ScrollPane.SCROLLBARS_ALWAYS);
    Image img = getImage(getCodeBase(),imageFile);
    canvas = new scrollableCanvas(img);
    canvas.setBackground(new Color(127,0,0));
    scrollPane.add(canvas);                                    // Add scrollableCanvas to ScrollPane and
    gridbag.setConstraints(scrollPane,constraints);  // then add ScrollPane to GridBagLayout.
    add(scrollPane);                                               // Event Listeners for ScrollPane are
                                                                            // implemented automatically in 1.1 AWT.
   /***********************************************************
    ** GridBagConstraints for RGBcolorChange (Begin Forth Row)
    ***********************************************************/
    constraints.anchor = GridBagConstraints.CENTER;
    constraints.fill = GridBagConstraints.HORIZONTAL;    // Fill grid cell in both directions.
    constraints.gridx = GridBagConstraints.RELATIVE;
    constraints.gridy = GridBagConstraints.RELATIVE;
    constraints.gridwidth = 3;                                               // Begin row 4 with this colorCanvas and set
    constraints.gridheight = 1;                                               // the width to span columns 1-3 in row 4.
    constraints.ipadx = 0;
    constraints.ipady = 0;
    constraints.insets = new Insets(10,10,10,10);                  // Space 10 pixels.
    constraints.weightx = 0.0;
    constraints.weighty = 0.0;
 
    RGBcolorChange = new colorChangesss(                       // Initialize a Panel (defined in class below)
                                     this,"Red","Green","Blue");              // for RGB changes.
    gridbag.setConstraints(RGBcolorChange,constraints);
    add(RGBcolorChange);
 
   /********************************************************
    ** GridBagConstraints for HSBcolorChange (End Forth Row)
    ********************************************************/
    constraints.anchor = GridBagConstraints.CENTER;
    constraints.fill = GridBagConstraints.HORIZONTAL;         // 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 Panel.
    constraints.gridheight = 1;
    constraints.ipadx = 0;
    constraints.ipady = 0;
    constraints.insets = new Insets(10,10,10,10);                       // Space 10 pixels.
    constraints.weightx = 0.0;
    constraints.weighty = 0.0;
    HSBcolorChange = new colorChangesss(                            // 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:  SimpleApplet12.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 = "View image boundary, 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:  SimpleApplet12.html
 
  /** THE REPAINT COLOR CANVAS METHOD **/        // This method is called from
                                                                                                // the colorChangesss class below,
  void repaint_colorCanvas(colorChangesss 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()));
      }

    canvas.setBackground(newRGBcolor);          // Set Canvas to new
    canvas.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:  SimpleApplet12.html
 
/***********************************************
 **       END OF CLASS SimpleApplet12                    **
 ***********************************************
 ** BEGIN 2ND CLASS: backgroundChangeButtonss **
 ***********************************************/
 
  class backgroundChangeButtonss implements ActionListener // This is the second class in this file.
   {Color newBackgroundColor;                                              // You can have as many classes as you
    SimpleApplet12 callingApplet;                                             // want in a file, but only one can be
                                                                                               // public, in this case SimpleApplet12.
    backgroundChangeButtonss(SimpleApplet12 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 SimpleApplet12.
 
  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:  SimpleApplet12.html
 
/*********************************************
 ** END 2ND CLASS: backgroundChangeButtonss **
 *********************************************
 **    BEGIN 3RD CLASS: scrollableCanvas            **
 *********************************************/

class scrollableCanvas extends Canvas
 {Image image;
  Dimension preferredSize = new Dimension(600, 500);
  Dimension minimumSize = new Dimension(10, 10);
 
  scrollableCanvas(Image img)                     // Minimal constructor without SimpleApplet12
   {image = img;}                                        // applet argument. So, limited communication
                                                                  // with SimpleApplet12.
  public Dimension getMinimumSize()
   {return minimumSize;}
 
  public Dimension getPreferredSize()
   {return preferredSize;}
 
  public void paint(Graphics g)
   {int imageWidth = image.getWidth(this);
    int imageHeight = image.getHeight(this);
    int vertspacing = (imageHeight-120)/2;               // Spacing for Eastern and
    int horizspacing = (imageWidth-165)/2;              // Southern boundary text.
 
    g.drawImage(image, 0, 0, getBackground(), this);
    g.setColor(Color.yellow);
 
    if (imageWidth*imageHeight < 1000)                  // Don't paint Eastern and
     {}                                                                     // Southern boundary text
    else                                                                    // until image is at least
     {for (int i = 0; i < 3; i += 1)                                // partially loaded.
       {g.drawString("Eastern",imageWidth+5,10 + i*(40 + vertspacing));
         g.drawString("Boundary",imageWidth+5,25 + i*(40 + vertspacing));
         g.drawString("of Image",imageWidth+5,40 + i*(40 + vertspacing));
 
         g.drawString("Southern",i*(55 + horizspacing),15 + imageHeight);
         g.drawString("Boundary",i*(55 + horizspacing),30 + imageHeight);
         g.drawString("of Image",i*(55 + horizspacing),45 + imageHeight);
         }
      g.drawString("Scroll to the East and South.",10,20);
      }
  }
}
 
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:  SimpleApplet12.html
 
/**************************************
 ** END 3RD CLASS: scrollableCanvas  **
 **************************************
 ** BEGIN 4TH CLASS: colorChangesss  **
 **************************************/
 
class colorChangesss extends Panel
                                 implements FocusListener,   // Need for version 1.1 of the AWT.
                                                  ActionListener
 {TextField newstr1, newstr2, newstr3;
  SimpleApplet12 callingApplet;

  colorChangesss(SimpleApplet12 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 colorChangesss
                                                                           // method and class are setup to communicate
   {callingApplet = methodArg;                             // with SimpleApplet12.
     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 4TH CLASS: colorChangesss       **

 ***********************************************/
 
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:  SimpleApplet12.html