home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Java 1.2 How-To
/
JavaHowTo.iso
/
javafile
/
ch06
/
ColorPicker.java
< prev
next >
Wrap
Text File
|
1998-12-14
|
4KB
|
193 lines
import java.applet.Applet;
import java.awt.*;
import java.awt.event.*;
/*
* the Applet class
*/
public class ColorPicker extends Applet implements AdjustmentListener
{
/*
* the panel that holds the sliders that
* control the RGB values
*/
Panel controls = new Panel();
/*
* the panel that holds the sample color
*/
Panel sample = new Panel();
public void adjustmentValueChanged(AdjustmentEvent evt)
{
Object object1 = evt.getSource();
if (object1 == (sbRed))
{
tfRed.setText(String.valueOf(sbRed.getValue()));
changecolor();
}
if (object1 == (sbGreen))
{
tfGreen.setText(String.valueOf(sbGreen.getValue()));
changecolor();
}
if (object1 == (sbBlue))
{
tfBlue.setText(String.valueOf(sbBlue.getValue()));
changecolor();
}
}
/*
*the red scrollbar
*/
Scrollbar sbRed;
/*
* the green scrollbar
*/
Scrollbar sbGreen;
/*
* the blue scrollbar
*/
Scrollbar sbBlue;
/*
* the red component TextField
*/
TextField tfRed;
/*
* the green component TextField
*/
TextField tfGreen;
/*
* the blue component TextField
*/
TextField tfBlue;
int min = 0;
int max = 255;
/*
* initilaizes the applet
*/
public void init () {
this.setLayout(new BorderLayout());
controls.setLayout (new GridLayout(3,3,5,5));
this.add ("South",controls);
this.add ("Center",sample);
tfRed = new TextField (5);
tfGreen = new TextField (5);
tfBlue = new TextField (5);
controls.add (new Label ("Red",1));
controls.add (new Label ("Green",1));
controls.add (new Label ("Blue",1));
controls.add (tfRed);
controls.add (tfGreen);
controls.add (tfBlue);
sbRed = new Scrollbar (Scrollbar.HORIZONTAL,0, 1, min, max);
//set the values for the scrollbar
// value, page size, min, max
controls.add (sbRed);
sbRed.addAdjustmentListener(this);
sbGreen = new Scrollbar (Scrollbar.HORIZONTAL,0, 1, min, max);
//set the values for the scrollbar
// value, page size, min, max
controls.add (sbGreen);
sbGreen.addAdjustmentListener(this);
sbBlue = new Scrollbar (Scrollbar.HORIZONTAL,0, 1, min, max);
//set the values for the scrollbar
// value, page size, min, max
controls.add (sbBlue);
sbBlue.addAdjustmentListener(this);
// sets the text fields to the slider value
tfRed.setText(String.valueOf(sbRed.getValue()));
tfGreen.setText(String.valueOf(sbGreen.getValue()));
tfBlue.setText(String.valueOf(sbBlue.getValue()));
changecolor();
}
/** Gets the current value in the text field.
* That's guaranteed to be the same as the value
* in the scroller (subject to rounding, of course).
* @param textField - the textField
*/
double getValue(TextField textField) {
double f;
try {
f = Double.valueOf(textField.getText())
σdoubleValue();
} catch (java.lang.NumberFormatException e) {
f = 0.0;
}
return f;
}
/*
* changes the color of the sample to the
* color defined by the RGB values set by
* the user
*/
public void changecolor () {
int i;
sample.setBackground(new Color((int)getValue(tfRed),
(int)getValue(tfGreen), (int)getValue(tfBlue)));
repaint();
}
public void update (Graphics g) {
paintAll(g);
}
/*
* application entry point
* not used when run as an applet
* @param args - command line arguments
*/
public static void main (String args[]) {
Frame f = new Frame ("ColorPicker");
ColorPicker colorPicker = new ColorPicker ();
colorPicker.init ();
f.addWindowListener(new WindowCloser());
f.setSize (300, 200);
f.add ("Center", colorPicker);
f.show ();
}
}
class WindowCloser extends WindowAdapter
{
public void windowClosing(WindowEvent e)
{
Window win = e.getWindow();
win.setVisible(false);
win.dispose();
System.exit(0);
}
}