import java.awt.*; import java.awt.event.*; import java.applet.Applet; public class ColorTest extends java.applet.Applet implements ColorChangeListener { ColorControls RGBcontrols, HSBcontrols; Canvas swatch; public static void main(String[] args) { Frame theFrame = new Frame("Color Test"); Applet theApplet = new ColorTest(); theFrame.add(theApplet, "Center"); theFrame.setLocation(50, 50); theFrame.setSize(700, 150); WindowListener w = new WindowAdapter() { public void windowClosing(WindowEvent e) { System.exit(0); } }; theFrame.addWindowListener(w); theApplet.init(); theFrame.setVisible(true); } public void colorChanged(ColorChangeEvent evt) { if (evt.generator == RGBcontrols) { if (evt.changer == ColorChangeEvent.SCROLLBAR) { scrollUpdate(RGBcontrols); } else { textUpdate(RGBcontrols); } } else { if (evt.changer == ColorChangeEvent.SCROLLBAR) { scrollUpdate(HSBcontrols); } else { textUpdate(HSBcontrols); } } } public void init() { setLayout(new GridLayout(1, 3, 5, 15)); // The color swatch swatch = new Canvas(); swatch.setBackground(Color.black); // The subpanels for the controls RGBcontrols = new ColorControls(this, "Red", 255, "Green", 255, "Blue", 255); HSBcontrols = new ColorControls(this, "Hue", 360, "Saturation", 100, "Brightness", 100); // Add it all to the layout add(swatch); add(RGBcontrols); add(HSBcontrols); } public Insets getInsets() { return new Insets(10, 10, 10, 10); } void textUpdate(ColorControls controlPanel) { Color c; // get string values from text fields, convert to ints int value1 = Integer.parseInt(controlPanel.tfield1.getText()); int value2 = Integer.parseInt(controlPanel.tfield2.getText()); int value3 = Integer.parseInt(controlPanel.tfield3.getText()); if (controlPanel == RGBcontrols) { // RGB changed, convert HSB // set RGB Scroll fields RGBcontrols.sb1.setValue(value1); RGBcontrols.sb2.setValue(value2); RGBcontrols.sb3.setValue(value3); c = new Color(value1, value2, value3); // convert RGB values to HSB values float[] HSB = Color.RGBtoHSB(value1, value2, value3, null /*(new float[3])*/); HSB[0] = HSB[0] * 360; HSB[1] = HSB[1] * 100; HSB[2] = HSB[2] * 100; HSBcontrols.tfield1.setText(String.valueOf((int)HSB[0])); HSBcontrols.tfield2.setText(String.valueOf((int)HSB[1])); HSBcontrols.tfield3.setText(String.valueOf((int)HSB[2])); // set HSB Scroll fields HSBcontrols.sb1.setValue((int)HSB[0]); HSBcontrols.sb2.setValue((int)HSB[1]); HSBcontrols.sb3.setValue((int)HSB[2]); } else { // HSB has changed, update RGB // set HSB Scroll fields HSBcontrols.sb1.setValue(value1); HSBcontrols.sb2.setValue(value2); HSBcontrols.sb3.setValue(value3); c = Color.getHSBColor((float)value1 / 360, (float)value2 / 100, (float)value3 / 100); // reset RGB fields RGBcontrols.tfield1.setText(String.valueOf(c.getRed())); RGBcontrols.tfield2.setText(String.valueOf(c.getGreen())); RGBcontrols.tfield3.setText(String.valueOf(c.getBlue())); // set RGB Scroll fields RGBcontrols.sb1.setValue(c.getRed()); RGBcontrols.sb2.setValue(c.getGreen()); RGBcontrols.sb3.setValue(c.getBlue()); } // Update swatch swatch.setBackground(c); swatch.repaint(); } void scrollUpdate(ColorControls controlPanel) { Color c; // Get string slider values int value1 = controlPanel.sb1.getValue(); int value2 = controlPanel.sb2.getValue(); int value3 = controlPanel.sb3.getValue(); if (controlPanel == RGBcontrols) { // RGB has changed, convert HSB RGBcontrols.tfield1.setText(String.valueOf (value1)); RGBcontrols.tfield2.setText(String.valueOf (value2)); RGBcontrols.tfield3.setText(String.valueOf (value3)); c = new Color(value1, value2, value3); // Convert RGB values to HSB values float[] HSB = Color.RGBtoHSB(value1, value2, value3, null /*(new float[3])*/); HSB[0] *= 360; HSB[1] *= 100; HSB[2] *= 100; // Reset HSB fields HSBcontrols.tfield1.setText(String.valueOf((int)HSB[0])); HSBcontrols.tfield2.setText(String.valueOf((int)HSB[1])); HSBcontrols.tfield3.setText(String.valueOf((int)HSB[2])); // Set HSB Scrolls HSBcontrols.sb1.setValue((int)HSB[0]); HSBcontrols.sb2.setValue((int)HSB[1]); HSBcontrols.sb3.setValue((int)HSB[2]); } else { // HSB has changed, update RGB HSBcontrols.tfield1.setText(String.valueOf (value1)); HSBcontrols.tfield2.setText(String.valueOf (value2)); HSBcontrols.tfield3.setText(String.valueOf (value3)); c = Color.getHSBColor((float)value1 / 360, (float)value2 / 100, (float)value3 / 100); // reset RGB fields RGBcontrols.tfield1.setText(String.valueOf(c.getRed())); RGBcontrols.tfield2.setText(String.valueOf(c.getGreen())); RGBcontrols.tfield3.setText(String.valueOf(c.getBlue())); // set RGB Scrolls RGBcontrols.sb1.setValue(c.getRed()); RGBcontrols.sb2.setValue(c.getGreen()); RGBcontrols.sb3.setValue(c.getBlue()); } //update swatch swatch.setBackground(c); swatch.repaint(); } }