/** Copyright (c) 1997 Sams.net Publishing. All Rights Reserved.
 ** Teach Yourself Java 1.1, Laura Lemay and Charles L. Perkins.
 ** Adapted from Listing 13.1:  The ColorTest applet, and Listing
 ** 13.2:  The ColorControls class (Uses 1.02 version of the AWT).
 **
 ** Modified to SimpleApplet9 by John Pais, May 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:  SimpleApplet9.html
 
/** USE EXISTING JAVA CLASSES **/

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

/** EXTEND THE JAVA APPLET CLASS **/

public class SimpleApplet9 extends Applet
{Canvas colorCanvas;                                                    // Declare a Canvas instance variable,
  colorChange RGBcolorChange, HSBcolorChange;       // and two colorChange (a second class
                                                                                      // defined below) instance variables.
  public void init()
   {setBackground(Color.white);
    setLayout(new GridLayout(1,3,10,10));                      // Initialize a GridLayout with 1 row,
                                                                                      // 3 columns, and 10 pixel spacing.
    colorCanvas = new Canvas();                                     // Initialize Canvas for painting colors
    colorCanvas.setBackground(Color.black);                  // with a black background color (this
                                                                                      // is the first column of the GridLayout).
 
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:  SimpleApplet9.html
 
    RGBcolorChange = new colorChange(                       // Initialize a Panel (defined in class below)
                                     this,"Red","Green","Blue");         // for RGB changes (this is the second column
                                                                                      // of the GridLayout).
    HSBcolorChange = new colorChange(                               // Initialize another Panel for HSB
                                     this,"Hue","Saturation","Brightness");  // changes (this is the third column
                                                                                              // of the GridLayout).

    add(colorCanvas);                                                             // Add all three columns to the
    add(RGBcolorChange);                                                     // GridLayout.
    add(HSBcolorChange);
    }

  public Insets getInsets()                                                       // Set all (top, left, bottom, right)
   {return new Insets(10,10,10,10);}                                     // outer boundary insets of Gridlayout
                                                                                            // to 10 pixels.
 
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:  SimpleApplet9.html
 
  /** THE REPAINT COLOR CANVAS METHOD **/    // This method is called from
                                                                                            // the colorChange class below,
  void repaint_colorCanvas(colorChange 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.
 
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:  SimpleApplet9.html
 
      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.
    }
   }

class colorChange extends Panel                               // This is the second class in this file. You can
{TextField newstr1, newstr2, newstr3;                      // have as many classes as you want, but only one
  SimpleApplet9 callingApplet;                                  // can be public, in this case SimpleApplet9.

  colorChange(SimpleApplet9 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 colorChange
                                                                                // method and class are setup to communicate
   {callingApplet = methodArg;                                 // with SimpleApplet9.

    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");
 
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:  SimpleApplet9.html
 
    add(new Label(label1, Label.RIGHT));               // Add Labels and TextFields to panel.
    add(newstr1);
    add(new Label(label2, Label.RIGHT));
    add(newstr2);
    add(new Label(label3, Label.RIGHT));
    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 boolean action(Event evt, Object arg)         // Uses version 1.02 of the AWT.
   {if (evt.target instanceof TextField)
     {callingApplet.repaint_colorCanvas(this);
      return true;
      }
    else return false;
    }
 
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:  SimpleApplet9.html
 
  public boolean handleEvent(Event evt)                   // Uses version 1.02 of the AWT.
   {switch (evt.id)
     {case Event.LOST_FOCUS:
        callingApplet.repaint_colorCanvas(this);
        return true;
      default:
       return super.handleEvent(evt);
      }
     }
   }