ColorMixer.java
/**
* Class which implements an applet which displays the color selected
* using three scrollbars. It illustrates the use of "listeners"
* Written by Kim Bruce, 10/3/99
*/
import java.awt.*;
import java.applet.Applet;
import objectdraw.*;
import java.awt.event.*;
public class ColorMixer extends WindowController implements ActionListener {
private static final int MIN_COLOR_VALUE = 0;
private static final int MAX_COLOR_VALUE = 255;
// labels for color controlled by each of three scrollbars
Label redLabel = new Label("Red", Label.RIGHT);
Label blueLabel = new Label(" Blue", Label.RIGHT);
Label greenLabel = new Label("Green", Label.RIGHT);
// Labels to display current value of each scrollbar
TextField redValueField = new TextField("0", 3);
TextField greenValueField = new TextField("0", 3);
TextField blueValueField = new TextField("0", 3);
// Panels on screen
Panel selectorPanel;
FilledRect colorRect; // Rectangle displaying chosen color
// Set up Scrollbars and labels on panels
public void begin() {
// Set up panel to hold three text fields and their labels
selectorPanel = new Panel();
// We want the labels and text fields to be next to each other, so use a GridLayout
// This gives us:
// 3 rows, one for each color
// 2 columns, one for the label, one for the text field
// 10 pixels between the label and text field
// 5 pixels beween rows
selectorPanel.setLayout (new GridLayout (3, 2, 10, 5));
// Set up the labels and text fields
selectorPanel.add (redLabel);
selectorPanel.add (redValueField);
selectorPanel.add (blueLabel);
selectorPanel.add (blueValueField);
selectorPanel.add (greenLabel);
selectorPanel.add (greenValueField);
// Add listeners to the fields so we know when the user changes the value.
redValueField.addActionListener (this);
blueValueField.addActionListener (this);
greenValueField.addActionListener (this);
add(selectorPanel,"South"); // Add sliderPanel to south of canvas
colorRect = new FilledRect(0,0,400,300,canvas); // create color display
}
// Get the value out of a text field and convert it to an integer.
// If the value is below the minimum allowed color value, set it
// to the minimum and update the text field. If it is above the
// maximum, set it to the maximum and update the text field.
//
// Parameters:
// field - the field whose value should be converted to an integer
//
// Notes:
// The program will crash if the field contains something other
// than an integer.
private int getColorMix (TextField field) {
String stringValue = field.getText ();
int mix = Integer.parseInt (stringValue);
if (mix < MIN_COLOR_VALUE) {
mix = MIN_COLOR_VALUE;
field.setText ("" + MIN_COLOR_VALUE);
}
else if (mix > MAX_COLOR_VALUE) {
mix = MAX_COLOR_VALUE;
field.setText ("" + MAX_COLOR_VALUE);
}
return mix;
}
/** This method is called whenever the user hits carriage return
* in a text field. It repaints the colorRect with the color
* obtained from the text field values.
*/
public void actionPerformed(ActionEvent evt) {
// get component color values
int redValue = getColorMix (redValueField);
int greenValue = getColorMix (greenValueField);
int blueValue = getColorMix (blueValueField);
// Create the new color and make it the color for colorRect
Color newColor = new Color(redValue,greenValue,blueValue);
colorRect.setColor(newColor);
}
}