/** Combines SimpleApplet8 and SimpleApplet9, and uses
** version 1.1 of the AWT, together with multiple (3)
** classes.
**
** Modified to SimpleApplet10 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: SimpleApplet10.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 SimpleApplet10 extends Applet
{backgroundChangeButton button;
// Declare instance variables:
Canvas colorCanvas;
// backgroundChangeButton (a 2nd
colorChanges RGBcolorChange, HSBcolorChange;
// class), a Canvas, and two
// colorChanges instances (a 3rd class).
Button redButton,greenButton,blueButton,
// Declare the buttons, and define
whiteButton,grayButton,blackButton;
// the font for the window text.
Font font = new Font("TimesRoman",Font.BOLD,20);
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: SimpleApplet10.html |
public void init()
{setBackground(Color.white);
setLayout(new GridLayout(3,3,10,10));
// Initialize a GridLayout with 3 rows,
// 3 columns, and 10 pixel spacing.
redButton = new Button("Red Background");
// Create the labeled buttons, and
button = new backgroundChangeButton(this,Color.red);
// the corresponding backgroundChangeButton
redButton.addActionListener(button);
// color objects to carry the color change.
add(redButton);
// Register listeners and add the Buttons
greenButton = new Button("Green Background");
// to the first two rows of the GridLayout.
button = new backgroundChangeButton(this,Color.green);
greenButton.addActionListener(button);
add(greenButton);
blueButton = new Button("Blue Background");
button = new backgroundChangeButton(this,Color.blue);
blueButton.addActionListener(button);
add(blueButton);
whiteButton = new Button("White Background");
button = new backgroundChangeButton(this,Color.white);
whiteButton.addActionListener(button);
add(whiteButton);
grayButton = new Button("Gray Background");
button = new backgroundChangeButton(this,Color.gray);
grayButton.addActionListener(button);
add(grayButton);
blackButton = new Button("Black Background");
button = new backgroundChangeButton(this,Color.black);
blackButton.addActionListener(button);
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: SimpleApplet10.html |
colorCanvas = new Canvas();
// Initialize Canvas for painting colors
colorCanvas.setBackground(Color.black);
// with a black background color (this
// is the (3,1) entry of the GridLayout).
RGBcolorChange = new colorChanges(
// Initialize a Panel (defined in class below)
this,"Red","Green","Blue"); // for RGB changes (this is the
(3,2) entry
// of the GridLayout).
HSBcolorChange = new colorChanges(
// Initialize another Panel for HSB
this,"Hue","Saturation","Brightness"); // changes (this is the (3,3)
entry
// of the GridLayout).
add(colorCanvas);
// Add the third row to the
add(RGBcolorChange);
// GridLayout.
add(HSBcolorChange);
}
public Insets getInsets()
// Set all (top, left, bottom, right)
{return new Insets(50,10,50,10);}
// outer boundary insets of Gridlayout:
// 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: SimpleApplet10.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: SimpleApplet10.html |
/** THE REPAINT COLOR CANVAS METHOD **/
// This method is called from
// the colorChanges class below,
void repaint_colorCanvas(colorChanges 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: SimpleApplet10.html |
/*********************************************
**
END OF CLASS SimpleApplet10
**
*********************************************
**BEGIN 2ND CLASS: backgroundChangeButton**
*********************************************/
class backgroundChangeButton implements ActionListener
// This is the second class in this file.
{Color newBackgroundColor;
// You can have as many classes as you
SimpleApplet10 callingApplet;
// want in a file, but only one can be
// public, in this case SimpleApplet10.
backgroundChangeButton(SimpleApplet10 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 SimpleApplet10.
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: SimpleApplet10.html |
/*********************************************
** END 2ND CLASS: backgroundChangeButton **
*********************************************
** BEGIN 3RD
CLASS: colorChanges **
*********************************************/
class colorChanges extends Panel
implements FocusListener, // Need for version
1.1 of the AWT.
ActionListener
{TextField newstr1, newstr2, newstr3;
SimpleApplet10 callingApplet;
colorChanges(SimpleApplet10 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 colorChanges
// method and class are setup to communicate
{callingApplet = methodArg;
// with SimpleApplet10.
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: colorChanges
**
*********************************************/
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: SimpleApplet10.html |