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 |
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 |
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 |
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 |
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 |
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 |