/** * @(#)RGBscrollbars.java 1.0 21/11/02 * * Uses scrollbars (contained in a panel with null layout) to set RGB color * of the main applet background. RGB components are given separately, and also * as a combined hex value (useful for web programmers). * * Andy Murray 21/11/02 * */ import java.awt.*; import java.awt.event.*; import java.applet.*; public class RGBscrollbars extends Applet implements AdjustmentListener { Panel sbPanel; //panel (for scrollbars only) Scrollbar [] sb; //scrollbars themselves Label [] RGBLabel; //labels for color values Label hexLabel; //for hex color value int r, g, b, //color values panelWidth, border; public void init() { panelWidth=320; border = 10; sbPanel=new Panel(); sbPanel.setSize(panelWidth,100); sbPanel.setLayout(null); //this is a key line to keep scrollbars inflated sb = new Scrollbar [3]; RGBLabel = new Label [3]; hexLabel = new Label("Hex value = #000000"); hexLabel.setBackground(Color.white); for (int i=0;i<3;i++) { sb[i] = new Scrollbar(Scrollbar.HORIZONTAL,0,20,0,275); sb[i].addAdjustmentListener(this); sb[i].setSize(panelWidth-2*border,20); switch (i) { case 0 : { sb[i].setBackground(Color.red); RGBLabel[i]=new Label("R = 0 "); break; } case 1 : { sb[i].setBackground(Color.green); RGBLabel[i]=new Label("G = 0 "); break; } case 2 : { sb[i].setBackground(Color.blue); RGBLabel[i]=new Label("B = 0 "); } } RGBLabel[i].setBackground(Color.white); sb[i].setLocation(border,border+i*30); sbPanel.add(sb[i]); this.add(RGBLabel[i]); } sbPanel.setBackground(Color.lightGray); add(sbPanel); //the applet still has flow layout add(hexLabel); this.setBackground(Color.black); //initially this.setSize(400,300); } public void adjustmentValueChanged(AdjustmentEvent ae) { Adjustable adjusted = ae.getAdjustable(); int val = ae.getValue(); if (adjusted.equals(sb[0])) {r=val;RGBLabel[0].setText("R = "+r);} if (adjusted.equals(sb[1])) {g=val;RGBLabel[1].setText("G = "+g);} if (adjusted.equals(sb[2])) {b=val;RGBLabel[2].setText("B = "+b);} this.setBackground(new Color(r,g,b)); hexLabel.setText("Hex value = #"+getHex(this.getBackground())); } String getHex(Color c) { return Integer.toHexString(c.getRGB()).substring(2); } }