privateColorcolorAt(int y){returnColor.getHSBColor(1-(float) y /(height -1),1,1);}}getGreen() returns the green component in the range 0-255 in the default sRGB space.
importjava.awt.Color;publicclassMain{publicstaticvoidmain(String[] a){Color myBlack =newColor(0,0,0);// Color blackColor myWhite =newColor(255,255,255);// Color whiteSystem.out.println(myBlack.equals(myWhite));}}
importjava.awt.*;importjava.awt.event.*;publicclassRGBChooserComponentextendsPanelimplementsAdjustmentListener{privateScrollbar redScroll, greenScroll, blueScroll;// Scroll bars.privateLabel redLabel, greenLabel, blueLabel;// For displaying RGB values.privateCanvas colorCanvas;// Color patch for displaying the color.publicRGBChooserComponent(){// Constructor./*Now letโs add scrollbars with values from 0 to 255. */
redScroll =newScrollbar(Scrollbar.HORIZONTAL,0,10,0,265);
greenScroll =newScrollbar(Scrollbar.HORIZONTAL,0,10,0,265);
blueScroll =newScrollbar(Scrollbar.HORIZONTAL,0,10,0,265);/* Create Labels showing current RGB and HSB values. */
redLabel =newLabel(" R = 0");
greenLabel =newLabel(" G = 0");
blueLabel =newLabel(" B = 0");/* We are setting backgrounds for Scrollbars and Labels, so they get the default
gray background of the applet. */
redScroll.setBackground(Color.lightGray);
greenScroll.setBackground(Color.lightGray);
blueScroll.setBackground(Color.lightGray);
redLabel.setBackground(Color.white);
greenLabel.setBackground(Color.white);
blueLabel.setBackground(Color.white);/* Establish a panel that would listen for changes to the Scrollbars' values */
redScroll.addAdjustmentListener(this);
greenScroll.addAdjustmentListener(this);
blueScroll.addAdjustmentListener(this);/* Add a canva, the background color of which will always match the currently selected color. */
colorCanvas =newCanvas();
colorCanvas.setBackground(Color.black);/* Create the applet layout, which consists of a row of
three equal-sized regions holding the Scrollbars,
the Labels, and the color patch. The background color
of the applet is gray, which will show around the edges
and between components. */setLayout(newGridLayout(1,3,3,3));setBackground(Color.gray);Panel scrolls =newPanel();Panel labels =newPanel();add(scrolls);add(labels);add(colorCanvas);/* Add the Scrollbars and the Labels to their respective panels. */
scrolls.setLayout(newGridLayout(3,1,2,2));
scrolls.add(redScroll);
scrolls.add(greenScroll);
scrolls.add(blueScroll);
labels.setLayout(newGridLayout(3,1,2,2));
labels.add(redLabel);
labels.add(greenLabel);
labels.add(blueLabel);}// end init();publicColorgetColor(){// Get the color currently displayed by the component.int r = redScroll.getValue();int g = greenScroll.getValue();int b = blueScroll.getValue();returnnewColor(r, g, b);}publicvoidsetColor(Color c){// Set the component to display the given color.// (Ignore this if c is null.)if(c !=null){int r = c.getRed();// Get the color levels from the color.int g = c.getGreen();int b = c.getBlue();
redLabel.setText(" R = "+ r);// Set the labels.
greenLabel.setText(" G = "+ g);
blueLabel.setText(" B = "+ b);
redScroll.setValue(r);// Set the scrollbars.
greenScroll.setValue(g);
blueScroll.setValue(b);
colorCanvas.setBackground(newColor(r,g,b));// Set the canvas.
colorCanvas.repaint();}}publicDimensiongetMinimumSize(){// Specify the minimum reasonable size of this component.returnnewDimension(150,40);}publicDimensiongetPreferredSize(){// Specify the preferred size of this component.returnnewDimension(280,80);}publicvoidadjustmentValueChanged(AdjustmentEvent evt){// This is called when the user has changed the values on// one of the scrollbars. All the scrollbars are checked,// the labels are set to display the correct values,// and the color patch is reset to correspond to the new color.int r = redScroll.getValue();int g = greenScroll.getValue();int b = blueScroll.getValue();
redLabel.setText(" R = "+ r);
greenLabel.setText(" G = "+ g);
blueLabel.setText(" B = "+ b);
colorCanvas.setBackground(newColor(r,g,b));
colorCanvas.repaint();// Redraw the canvas in its new color.}// end adjustmentValueChangedpublicInsetsgetInsets(){// The system calls this method to find out how much space to// leave between the edges of the applet and the components that// it contains. I want a 2-pixel border at each edge.returnnewInsets(2,2,2,2);}}// end class RGBColorChooser
GO TO FULL VERSION