// Illustrates the use of the Color class
import java.applet.*;
import java.awt.*;
import java.awt.event.*;
public class Colors extends Applet implements ActionListener
{
ColorControl[] control = new ColorControl[3];
public Colors()
{
setLayout( new GridLayout(6,1) );
control[0] = new ColorControl( "Red" );
control[1] = new ColorControl( "Green" );
control[2] = new ColorControl( "Blue" );
for (int i = 0; i < 3; i++)
{
add( control[i] );
}
}
public void actionPerformed( ActionEvent theEvent )
{
Color c = new Color( control[0].getValue(),
control[1].getValue(),
control[2].getValue() );
setBackground( c );
repaint();
}
public static void main(String arg[])
{
Colors theApp = new Colors( );
AppletFrame af = new AppletFrame(theApp, "The Colors Applet");
af.add(theApp, "Center");
af.setSize( 200, 300 );
af.setVisible(true);
theApp.init( );
theApp.start( );
}
}
class ColorControl extends Applet implements ActionListener
{
final static int STEP = 16;
private Label name;
private int value = 255;
private TextField input = new TextField( "255" );
private Button upButton = new Button( " + " );
private Button dnButton = new Button( " - " );
public int getValue() { return value; }
public ColorControl( String s )
{
setLayout( new GridLayout(1,4) );
name = new Label( s );
add( name );
add( input );
add( upButton );
add( dnButton );
input.addActionListener(this);
upButton.addActionListener(this);
dnButton.addActionListener(this);
}
public void actionPerformed(ActionEvent theEvent)
{
value = Integer.parseInt(input.getText());
if (theEvent.getSource( ) == upButton) value+=(value + STEP > 255 ? 0 : STEP);
if (theEvent.getSource( ) == dnButton) value-=(value - STEP < 0 ? 0 : STEP);
input.setText( "" + value );
((ActionListener) getParent( )).actionPerformed(theEvent);
}
}
class AppletFrame extends Frame implements WindowListener
{
Applet theApplet;
public AppletFrame(Applet anyApplet, String title)
{
super(title);
theApplet = anyApplet;
addWindowListener(this);
}
public void windowClosed (WindowEvent theEvent) { }
public void windowClosing (WindowEvent theEvent) { quitApplication( ); }
public void windowDeiconified(WindowEvent theEvent) { }
public void windowIconified (WindowEvent theEvent) { }
public void windowOpened (WindowEvent theEvent) { }
public void windowActivated (WindowEvent theEvent) { }
public void windowDeactivated(WindowEvent theEvent) { }
public void quitApplication( )
{
setVisible(false);
theApplet.setVisible(false);
theApplet.stop( );
theApplet.destroy( );
dispose( );
System.exit(0);
}
}