/** * ColorMixer is a simple applet to help * pick a color in Java, showing the various arguments * needed to make a new Color. The Color constructors * are a good example of overloading. You can make a new * instance of Color by using one of several constructor methods. * One takes 3 integers (the Red-Green-Blue from 0 to 255) * another takes 3 float values (percentage from 0.0 to 1.0) * another takes a single hex value, and * yet another takes a single intger value * * @author Fr Chris Thiel, ofmCap * @date 9 September 2006 * @version 1.0 */ import java.awt.*; import java.applet.Applet; import java.awt.event.*; public class ColorMixer extends Applet implements AdjustmentListener { int redValue = 0; // red int greenValue = 0; // green int blueValue = 0; // blue Label redLabel = new Label("Red=0"); Label greenLabel = new Label("Green=000"); Label blueLabel = new Label("Blue=000 "); Scrollbar red=new Scrollbar(Scrollbar.HORIZONTAL,0,20,0,275); Scrollbar green=new Scrollbar(Scrollbar.HORIZONTAL,0,20,0,275); Scrollbar blue=new Scrollbar(Scrollbar.HORIZONTAL,0,20,0,275); public void init() { /** * The next few lines sets up the layout to organize * all elements of the GUI (Graphic User Interface) */ redLabel.setAlignment(Label.RIGHT); greenLabel.setAlignment(Label.RIGHT); blueLabel.setAlignment(Label.RIGHT); setLayout(new BorderLayout()); Panel labelPanel=new Panel(); labelPanel.setLayout(new GridLayout(3,1)); labelPanel.add(redLabel); labelPanel.add(greenLabel); labelPanel.add(blueLabel); Panel sliderPanel=new Panel(); sliderPanel.setLayout(new GridLayout(3,1)); sliderPanel.add(red); sliderPanel.add(green); sliderPanel.add(blue); Panel southPanel= new Panel(); southPanel.setLayout(new BorderLayout()); southPanel.add("West",labelPanel); southPanel.add("Center",sliderPanel); add("South",southPanel); /** * the next bit adds the adjusment listeners so the applet can * respond to the user's action on the scrollbars */ red.addAdjustmentListener(this); green.addAdjustmentListener(this); blue.addAdjustmentListener(this); } /** * adjustmentValueChanged() is the method that handles AdjustmentEvents * which is when the value of a Scrollbar is changed */ public void adjustmentValueChanged(AdjustmentEvent e) { redValue = red.getValue(); greenValue = green.getValue(); blueValue = blue.getValue(); redLabel.setText("Red="+redValue); greenLabel.setText("Green="+greenValue); blueLabel.setText("Blue="+blueValue); repaint(); } /** * /Here is a private method * for converting integer values from 1-255 into percentages... */ private float percent(int x) { float p=(float)(x/255.0); int per=(int)Math.round(100*p); p=per/100.0F; return p; } /** * hex is a method for converting integer values from 0 to 255 * into a hex String */ private String hex(int x) { String s=Integer.toHexString(x); s=s.toUpperCase(); if (s.length()<2) { s="0"+s; } return s; } /** * Paint is where it gets put together */ public void paint( Graphics g) { //Draw the text description: g.setColor(Color.black); g.setFont (new Font("sansserif", Font.BOLD, 16)); g.drawString( "Using 3 int arguments:\t Color c= new Color( "+redValue+", "+greenValue+", "+blueValue+" );", 10,20); g.drawString( "Using 3 float arguments:\t Color c= new Color( "+percent(redValue)+"F, "+percent(greenValue)+"F, "+percent(blueValue)+"F );", 10,40); g.drawString( "Using 1 hex argument:\t Color c= new Color( 0x"+hex(redValue)+hex(greenValue)+hex(blueValue)+" );", 10,60); g.drawString( "Using 1 int argument:\t Color c= new Color( "+(65536*redValue+256*greenValue+blueValue)+" );", 10,80); // Draw a big example of the color: g.setColor(new Color(redValue, greenValue, blueValue)); g.fillRoundRect(20, 100, 520, 300, 100,100); } }