//ColorChooser2.java //Demonstrates colors and scrollbars //uses improved scroller //Andy Harris, 05/00 import java.awt.*; import java.applet.*; import java.awt.event.*; import Scroller; public class ColorChooser2 extends Applet implements AdjustmentListener{ Panel pnlRGB = new Panel(); Scroller scrRed = new Scroller(Scroller.RGB, "R"); Scroller scrGreen = new Scroller(Scroller.RGB, "G"); Scroller scrBlue = new Scroller(Scroller.RGB, "B"); Panel pnlHSB = new Panel(); Scroller scrHue = new Scroller(Scroller.HSB, "H"); Scroller scrSat = new Scroller(Scroller.HSB, "S"); Scroller scrBright = new Scroller(Scroller.HSB, "B"); Label lblColor = new Label(); public void init(){ pnlRGB.setLayout(new GridLayout(1,0)); pnlRGB.add(scrRed); pnlRGB.add(scrGreen); pnlRGB.add(scrBlue); pnlHSB.setLayout(new GridLayout(1,0)); pnlHSB.add(scrHue); pnlHSB.add(scrSat); pnlHSB.add(scrBright); setLayout(new BorderLayout()); add(pnlRGB, BorderLayout.WEST); add(lblColor, BorderLayout.CENTER); add(pnlHSB, BorderLayout.EAST); //add listeners scrRed.addAdjustmentListener(this); scrGreen.addAdjustmentListener(this); scrBlue.addAdjustmentListener(this); scrHue.addAdjustmentListener(this); scrSat.addAdjustmentListener(this); scrBright.addAdjustmentListener(this); } // end init public void adjustmentValueChanged(AdjustmentEvent e){ Color theColor; Scrollbar scr = (Scrollbar) e.getSource(); if (scr.getMaximum() == 255){ //an RGB scroller was hit //figure out the color theColor= new Color (scrRed.getIntValue(), scrGreen.getIntValue(), scrBlue.getIntValue()); //get a series of HSB values float HSBVals[] = Color.RGBtoHSB (theColor.getRed(), theColor.getGreen(), theColor.getBlue(), null); int hue = (int)(HSBVals[0] * 100); int sat = (int)(HSBVals[1] * 100); int bright = (int)(HSBVals[2] * 100); //copy these values to HSB scrollers scrHue.setValue(hue); scrSat.setValue(sat); scrBright.setValue(bright); } else { //it was an HSB scroller //get the color theColor = Color.getHSBColor (scrHue.getFloatValue(), scrSat.getFloatValue(), scrBright.getFloatValue()); //Reset the RGB scrollers to current color scrRed.setValue(theColor.getRed()); scrGreen.setValue(theColor.getGreen()); scrBlue.setValue(theColor.getBlue()); } // end if lblColor.setBackground(theColor); } // end adjValChanged } // end class def