/* A useful applet that demonstrates the use of sliders in a very basic way. However, the applet can be used as a color swatch to determine the RGB values for various colors. There are three sliders (one each for RGB) and three labels (passive GUI widgets. The placement of all widgets is not specified. Later we'll learn how to design better GUI layouts. */ import java.awt.*; import java.applet.Applet; import java.awt.event.*; public class swatch extends Applet implements AdjustmentListener { Scrollbar redSlider, greenSlider, blueSlider; // the three sliders Label redLabel, greenLabel, blueLabel; // labels for them int redValue, greenValue, blueValue; // current RGB values Color theColor=Color.black; Color textColor=Color.white; public void init() { // create GUI widgets for red (label and scrollbar) redLabel = new Label("Red"); add(redLabel); redSlider = new Scrollbar(Scrollbar.HORIZONTAL, 0, 1, 0, 256); add(redSlider); redSlider.addAdjustmentListener(this); // create GUI widgets for green greenLabel = new Label("Green"); add(greenLabel); greenSlider = new Scrollbar(Scrollbar.HORIZONTAL, 0, 1, 0, 256); add(greenSlider); greenSlider.addAdjustmentListener(this); // create GUI widgets for blue blueLabel = new Label("Blue"); add(blueLabel); blueSlider = new Scrollbar(Scrollbar.HORIZONTAL, 0, 1, 0, 256); add(blueSlider); blueSlider.addAdjustmentListener(this); } // end of init public void paint(Graphics g) { // set the swatch to theColor setBackground(theColor); // print out the color values using text of complementary color textColor = new Color(255-redValue, 255-greenValue, 255-blueValue); g.setColor(textColor); g.drawString("Red = " + redValue, 50, 50); g.drawString("Green = " + greenValue, 50, 80); g.drawString("Blue = " + blueValue, 50, 100); } // end of paint public void adjustmentValueChanged (AdjustmentEvent e) { redValue = redSlider.getValue(); greenValue = greenSlider.getValue(); blueValue = blueSlider.getValue(); // create the new color theColor = new Color(redValue, greenValue, blueValue); repaint(); } // end of adjustmentValueChanged } // end of applet swatch