/* * =================================================================== * DemoColor.java : This applet creates a canvas to display colors and * a control panel to adjust the RGB and HSB values needed to create * various shades of color. A slider is also provided for alpha value * adjustment. * * Note. When components with different colors overlap each other, the * transparency of the foreground is the extent to which it allows * the background color of pixels to be visible. This property of a * color is called its alpha value. * * Adapted from : Pantham S., Pure JFC Swing, 1999. * Modified by : Mark Austin March, 2001 * =================================================================== */ import javax.swing.*; import javax.swing.event.*; import java.awt.*; import java.awt.event.*; import javax.swing.border.*; public class DemoColor extends JApplet { DrawingCanvas canvas; // Canvas to display colors JLabel rgbValue; // Label to display rgbValue of color // Sliders to control RGB, HSB and alpha values JSlider slider, sliderR, sliderG, sliderB, sliderH, sliderS, sliderBr, sliderAlpha; public void init() { // 1. Get the content pane Container container = getContentPane(); // 2. Define the color canvas canvas = new DrawingCanvas(); // 3. Create control sliders by using the method // defined in snippet-6. The parameters of the slider // constructors are its minimum and maximum values, current value, // maximum and minimum tick spacings. setSlider(0, 255, 0, 50, 5); sliderR = slider; setSlider(0, 255, 0, 50, 5); sliderG = slider; setSlider(0, 255, 0, 50, 5); sliderB = slider; setSlider(0, 10, 0, 5, 1); sliderH = slider; setSlider(0, 10, 0, 5, 1); sliderS = slider; setSlider(0, 10, 0, 5, 1); sliderBr = slider; setSlider(0, 255, 255, 50, 5); // See the integer constructor sliderAlpha = slider; // with alpha value of Color. // 4. Add the labels and sliders to a panel JPanel panel = new JPanel(); panel.setBorder(new TitledBorder("Control and Display Panel")); panel.setLayout(new GridLayout(6,2,15,0));//6 rows, 2 cols, // 15, 0 horiz&vert spacing panel.add(new JLabel("R-G-B Sliders (0 - 255)")); panel.add(new JLabel("H-S-B Sliders (ex-1)")); panel.add(sliderR); panel.add(sliderH); panel.add(sliderG); panel.add(sliderS); panel.add(sliderB); panel.add(sliderBr); panel.add(new JLabel("Alpha Adjustment (0 - 255): ", JLabel.RIGHT)); panel.add(sliderAlpha); panel.add(new JLabel("RGB Value: ", JLabel.RIGHT)); rgbValue = new JLabel(""); rgbValue.setBackground(Color.white); rgbValue.setForeground(Color.black); rgbValue.setOpaque(true); panel.add(rgbValue); // 5. Add the panel and canvas to the applet container.add(panel, BorderLayout.SOUTH); container.add(canvas); } // 6. Method to create sliders with necessary configuration public void setSlider(int min, // Slider minimum value int max, // Slider maximum value int init, // Slider initial value int mjrTkSp, // Major tick spacing int mnrTkSp) { // Minor tick spacing slider = new JSlider(JSlider.HORIZONTAL, min, max, init); slider.setPaintTicks( true ); slider.setMajorTickSpacing( mjrTkSp ); slider.setMinorTickSpacing( mnrTkSp ); slider.setPaintLabels( true ); slider.addChangeListener(new SliderListener()); } // 7. Definition of the display canvas class DrawingCanvas extends Canvas { Color color; int redValue, greenValue, blueValue; int alphaValue = 255; float[] hsbValues = new float[3]; // 8. Constructor public DrawingCanvas() { // Assign a size to the canvas setSize( 350, 350); // canvas width and height // Assign color to the canvas color = new Color(0, 0, 0); setBackgroundColor(); } public void setBackgroundColor() { color = new Color(redValue, greenValue, blueValue, alphaValue); setBackground(color); } } // 9. Define the slider listener to alter different parameters // of colors. class SliderListener implements ChangeListener { public void stateChanged(ChangeEvent e) { JSlider slider = (JSlider) e.getSource(); if (slider == sliderAlpha) { canvas.alphaValue = slider.getValue(); canvas.setBackgroundColor(); } else if (slider == sliderR) { canvas.redValue = slider.getValue(); displayRGBColor(); } else if (slider == sliderG) { canvas.greenValue = slider.getValue(); displayRGBColor(); } else if (slider == sliderB) { canvas.blueValue = slider.getValue(); displayRGBColor(); } else if (slider == sliderH) { canvas.hsbValues[0] = (float)(slider.getValue()*0.1); displayHSBColor(); } else if (slider == sliderS) { canvas.hsbValues[1] = (float) (slider.getValue()*0.1); displayHSBColor(); } else if (slider == sliderBr) { canvas.hsbValues[2] = (float) (slider.getValue()*0.1); displayHSBColor(); } // Refresh canvas with new parameters canvas.repaint(); } public void displayRGBColor() { canvas.setBackgroundColor(); Color.RGBtoHSB(canvas.redValue, canvas.greenValue, canvas.blueValue, canvas.hsbValues); sliderH.setValue((int)(canvas.hsbValues[0]*10)); sliderS.setValue((int)(canvas.hsbValues[1]*10)); sliderBr.setValue((int)(canvas.hsbValues[2]*10)); rgbValue.setText(Integer.toString( canvas.color.getRGB()&0xffffff, 16)); } public void displayHSBColor() { canvas.color = Color.getHSBColor(canvas.hsbValues[0], canvas.hsbValues[1], canvas.hsbValues[2]); // This color is not sufficient for display; we // need to have proper alpha value. So use this to // to obtain the RGB components. Anyway, they are useful // to adjust the values of RGB sliders. canvas.redValue = canvas.color.getRed(); canvas.greenValue = canvas.color.getGreen(); canvas.blueValue = canvas.color.getBlue(); sliderR.setValue(canvas.redValue); sliderG.setValue(canvas.greenValue); sliderB.setValue(canvas.blueValue); canvas.color = new Color(canvas.redValue, canvas.greenValue, canvas.blueValue, canvas.alphaValue); canvas.setBackground(canvas.color); } } }