import java.applet.Applet; import java.awt.*; import java.awt.event.*; /** * Class ColorSelector Applet * * Class for determining the Red Green Blue (RGB) values for selected colors * * @see #init * * @version 1.0 * @author Michael D. baker Jr. */ public class ColorSelector extends Applet { private DisplayArea rgbDisplay; private ControlArea rgbControls; private RGBadjustmentHandler rgbHandler; /** * Method init() * * initialization method for the applet * * @param none * * @return none * * @since 1.1 */ public void init() { rgbDisplay = new DisplayArea(); rgbControls = new ControlArea(); rgbHandler = new RGBadjustmentHandler( rgbDisplay, rgbControls ); rgbControls.addAdjustmentListener( rgbHandler ); setLayout( new BorderLayout() ); add( rgbDisplay, BorderLayout.CENTER); add( rgbControls, BorderLayout.EAST ); rgbDisplay.setColor( rgbControls.getColor() ); } } /** * Class DisplayArea * * Class for displaying colors in a large area * * @version 1.0 * @author Michael D. baker Jr. * * @see #DisplayArea * @see #setColor */ class DisplayArea extends Canvas { /** * Method DisplayArea() * * Creates a large display area object for displaying colors * * @param none * * @return none * * @since 1.1 */ public DisplayArea() { super(); } /** * Method setColor() * * Set the color of the display area canvas * * @param Color myColor target color to use * * @return none * * @since 1.1 */ public void setColor( Color myColor ) { setBackground( myColor ); } } /** * Class ControlArea * * Class for providing Red Green Blue (RGB) selection controls and displaying * the current selection numerically * * @version 1.0 * @author Michael D. baker Jr. * * @see #ControlArea * @see #addAdjustmentListener * @see #getColor * @see #displayColor */ class ControlArea extends Panel { final private int RED = 0; final private int GREEN = 1; final private int BLUE = 2; final private String rgbLabels[] = { "RED", "GREEN", "BLUE" }; private Scrollbar control[]; private Label title[]; private Label value[]; private Panel controlPanel[]; private Panel controlDisplay[]; /** * Method ControlArea() * * Creates a ControlArea object for adjusting rgb colors with sliders * and presenting the numerical values as text * * @param none * * @return none * * @since 1.1 */ public ControlArea() { int maxTitleWidth = 0; Dimension titleDim; Dimension scrollDim; controlPanel = new Panel[3]; controlDisplay = new Panel[3]; control = new Scrollbar[3]; title = new Label[3]; value = new Label[3]; setLayout( new GridLayout( 1, 3 ) ); for( int i=0; i<3; i++ ) { control[i] = new Scrollbar( Scrollbar.VERTICAL, 0, 1, 0, 256 ); title[i] = new Label( rgbLabels[i] ); value[i] = new Label( "0" ); controlPanel[i] = new Panel(); controlDisplay[i] = new Panel(); titleDim = title[i].getSize(); if( titleDim.width > maxTitleWidth ) maxTitleWidth = titleDim.width; controlDisplay[i].setLayout( new GridLayout( 2,1 ) ); controlDisplay[i].add( title[i] ); controlDisplay[i].add( value[i] ); controlPanel[i].setLayout( new BorderLayout() ); controlPanel[i].add( control[i], BorderLayout.CENTER ); controlPanel[i].add( controlDisplay[i], BorderLayout.SOUTH ); add( controlPanel[i] ); } titleDim = title[0].getSize(); titleDim.width = maxTitleWidth; scrollDim = control[0].getSize(); scrollDim.width = 5; for( int i=0; i<3; i++ ) { title[i].setSize( titleDim ); title[i].setAlignment( Label.CENTER ); value[i].setSize( titleDim ); value[i].setAlignment( Label.CENTER ); control[i].setSize( scrollDim ); } } /** * Method addAdjustmentListener() * * register a target object with this control object to be notified whenever * the user adjusts any of the slider controls * * @param AdjustmentListener l object to notify when controls adjusted * * @return none * * @since 1.1 */ public void addAdjustmentListener( AdjustmentListener l ) { for( int i=0; i<3; i++ ) control[i].addAdjustmentListener( l ); } /** * Method getColor() * * get the current color settings of this control object * * @param none * * @return Color * * @since 1.1 */ public Color getColor() { Color current = new Color( control[RED ].getValue(), control[GREEN].getValue(), control[BLUE ].getValue() ); return( current ); } /** * Method displayColor() * * update the numerical display for the color controls * * @param none * * @return none * * @since 1.1 */ public void displayColor() { for( int i=0; i<3; i++ ) { value[i].setText( String.valueOf( control[i].getValue() ) ); } } } /** * Class RGBadjustmentHander * * Class to watch for changes to controls and updating displays * * @version 1.0 * @author Michael D. baker Jr. * * @see #RGBadjustmentHandler * @see #adjustmentValueChanged */ class RGBadjustmentHandler implements AdjustmentListener { private DisplayArea d; private ControlArea c; /** * Method RGBadjustmentHander() * * Create a controller class to handle events and update appropriate * objects. Graphical areas will be updated with the color. Control * areas will be monitored for adjustment to any controls and numerical * displays updated. * * @param DisplayArea object handle for graphical area to update * @param ControlArea object handle for control area to watch & update * * @return none * * @since 1.1 */ public RGBadjustmentHandler( DisplayArea display, ControlArea controls ) { d = display; c = controls; } /** * Method adjustmentValueChanged() * * method called whenever one of the controls is adjusted. * * @param AdjustmentEvent event object containing adjustment information * * @return none * * @since 1.1 */ public void adjustmentValueChanged( AdjustmentEvent e ) { d.setColor( c.getColor() ); c.displayColor(); } }