//Scroller //A scrollbar that returns 0 to 255 //Or a float zero to 1 //used in ColorChooser.java //Andy Harris, 05/00 import java.awt.*; import java.awt.event.*; /** The Scroller class is a custom scrollbar Intended to make color selection panels easier to creat It includes methods for RGB and HSB - style color selection */ public class Scroller extends Panel implements AdjustmentListener{ /** Used to specify an RGB - style scrollbar */ public static final int RGB = 0; /** Used to specify an RGB - style scrollbar */ public static final int HSB = 1; private int privateType = RGB; Scrollbar scr = new Scrollbar(Scrollbar.VERTICAL, 0, 1, 0, 256); Label lblValue = new Label("0"); Label lblCaption = new Label(); AdjustmentListener listener; /** type is the type of scrollbar to generate. Use Scroller.RGB to get a 0-255 value, and Scroller.HSB to get a 0-1 float value */ public Scroller(int type, String caption){ privateType = type; if (privateType == RGB){ scr.setMaximum(256); } else { scr.setMaximum(101); } // end if setLayout(new BorderLayout()); add (scr, BorderLayout.CENTER); add (lblCaption, BorderLayout.NORTH); add (lblValue, BorderLayout.SOUTH); setFont(new Font("Serif", Font.PLAIN, 10)); lblCaption.setText(caption); scr.addAdjustmentListener(this); } // end constructor public void addAdjustmentListener(AdjustmentListener l){ listener = l; } // end addAdjustmentListener public void adjustmentValueChanged(AdjustmentEvent e){ lblValue.setText(String.valueOf(e.getValue())); listener.adjustmentValueChanged(e); } //end adjustmentValueChanged /** returns back an int between 0 and 255 useful for RGB color creation */ public int getRGBValue(){ return scr.getValue(); } // end getIntValue /** returns back a float between 0 and 1 useful for HSB color creation */ public float getHSBValue(){ return (float)((float)scr.getValue() / 100); } // end getFloatValue /** set the value of the scrollbar. MUST be an int value */ public void setValue(int val){ scr.setValue(val); lblValue.setText(String.valueOf(val)); } // end setValue /** set the value of the scrollbar. If a float was sent (like an HSB value) multiply the value by 100, then set the value */ public void setValue(float fval){ int val = (int)(fval * 100); scr.setValue(val); lblValue.setText(String.valueOf(val)); } // end setValue } // end class def