/* A RGBColorChooser shows three scroll bars that the user can manipulate to set the red, green, and blue, components of the color. A color patch shows the selected color, and there are three labels that show the numerical values of all the components. Values are in the range 0 to 255. The initial color is black. */ import java.awt.*; import java.awt.event.*; import java.applet.*; public class RGBColorChooser extends Applet implements AdjustmentListener { private Scrollbar redScroll, greenScroll, blueScroll; // Scroll bars. private Label redLabel, greenLabel, blueLabel; // For displaying RGB values. private Canvas colorCanvas; // Color patch for displaying the color. public void init() { /* Create Scrollbars with possible values from 0 to 255. */ redScroll = new Scrollbar(Scrollbar.HORIZONTAL, 0, 10, 0, 265); greenScroll = new Scrollbar(Scrollbar.HORIZONTAL, 0, 10, 0, 265); blueScroll = new Scrollbar(Scrollbar.HORIZONTAL, 0, 10, 0, 265); /* Create Labels showing current RGB and HSB values. */ redLabel = new Label(" R = 0"); greenLabel = new Label(" G = 0"); blueLabel = new Label(" B = 0"); /* Set background colors for Scrollbars and Labels, so they don't inherit the gray background of the applet. */ redScroll.setBackground(Color.lightGray); greenScroll.setBackground(Color.lightGray); blueScroll.setBackground(Color.lightGray); redLabel.setBackground(Color.white); greenLabel.setBackground(Color.white); blueLabel.setBackground(Color.white); /* Set the applet to listen for changes to the Scrollbars' values */ redScroll.addAdjustmentListener(this); greenScroll.addAdjustmentListener(this); blueScroll.addAdjustmentListener(this); /* Create a canvas whose background color will always be set to the currently selected color. */ colorCanvas = new Canvas(); colorCanvas.setBackground(Color.black); /* Create the applet layout, which consists of a row of three equal-sized regions holding the Scrollbars, the Labels, and the color patch. The background color of the applet is gray, which will show around the edges and between components. */ setLayout(new GridLayout(1,3,3,3)); setBackground(Color.gray); Panel scrolls = new Panel(); Panel labels = new Panel(); add(scrolls); add(labels); add(colorCanvas); /* Add the Scrollbars and the Labels to their respective panels. */ scrolls.setLayout(new GridLayout(3,1,2,2)); scrolls.add(redScroll); scrolls.add(greenScroll); scrolls.add(blueScroll); labels.setLayout(new GridLayout(3,1,2,2)); labels.add(redLabel); labels.add(greenLabel); labels.add(blueLabel); } // end init(); public void adjustmentValueChanged(AdjustmentEvent evt) { // This is called when the user has changed the values on // one of the scrollbars. All the scrollbars are checked, // the labels are set to display the correct values, // and the color patch is reset to correspond to the new color. int r = redScroll.getValue(); int g = greenScroll.getValue(); int b = blueScroll.getValue(); redLabel.setText(" R = " + r); greenLabel.setText(" G = " + g); blueLabel.setText(" B = " + b); colorCanvas.setBackground(new Color(r,g,b)); colorCanvas.repaint(); // Redraw the canvas in its new color. } // end adjustmentValueChanged public Insets getInsets() { // The system calls this method to find out how much space to // leave between the edges of the applet and the components that // it contains. I want a 3-pixel border at each edge. return new Insets(3,3,3,3); } } // end class RGBColorChooser