Friday, April 26, 2013

COLOR PALATTE USING APPLET


import java.awt.*;
import java.awt.event.*;
import java.applet.*;
public class ColorChooserApplet extends Applet implements AdjustmentListener
{
private float[]hsb=new float[3];
private int r=0,g=0,b=0;
private Scrollbar hueScroll,brightnessScroll,saturationScroll,blueScroll,redScroll,greenScroll;
private Label hueLabel,brightnessLabel,saturationLabel,redLabel,greenLabel,blueLabel;
private Canvas colorCanvas;
public void init()
{
Color.RGBtoHSB(0,0,0,hsb);
hueScroll=new Scrollbar(Scrollbar.HORIZONTAL,(int)(255*hsb[0]),10,0,265);
saturationScroll=new Scrollbar(Scrollbar.HORIZONTAL,(int)(255*hsb[1]),10,0,265);
brightnessScroll=new Scrollbar(Scrollbar.HORIZONTAL,(int)(255*hsb[2]),10,0,265);
redScroll=new Scrollbar(Scrollbar.HORIZONTAL,0,10,0,265);
greenScroll=new Scrollbar(Scrollbar.HORIZONTAL,0,10,0,265);
blueScroll=new Scrollbar(Scrollbar.HORIZONTAL,0,10,0,265);
hueLabel=new Label("H="+hsb[0]);
saturationLabel=new Label("S="+hsb[1]);
brightnessLabel=new Label("B="+hsb[2]);
redLabel=new Label("R=0");
greenLabel=new Label("G=0");
blueLabel=new Label("B=0");
hueScroll.setBackground(Color.lightGray);
saturationScroll.setBackground(Color.lightGray);
brightnessScroll.setBackground(Color.lightGray);
redScroll.setBackground(Color.lightGray);
greenScroll.setBackground(Color.lightGray);
blueScroll.setBackground(Color.lightGray);
hueLabel.setBackground(Color.white);
saturationLabel.setBackground(Color.white);
brightnessLabel.setBackground(Color.white);
redLabel.setBackground(Color.white);
greenLabel.setBackground(Color.white);
blueLabel.setBackground(Color.white);
hueScroll.addAdjustmentListener(this);
saturationScroll.addAdjustmentListener(this);
brightnessScroll.addAdjustmentListener(this);
redScroll.addAdjustmentListener(this);
greenScroll.addAdjustmentListener(this);
blueScroll.addAdjustmentListener(this);

colorCanvas=new Canvas();
colorCanvas.setBackground(Color.black);
setLayout(new GridLayout(1,3,3,3));
setBackground(Color.gray);
Panel scrolls=new Panel();
Panel Labels=new Panel();
add(scrolls);
add(Labels);
add(colorCanvas);
scrolls.setLayout(new GridLayout(6,1,2,2));
scrolls.add(redScroll);
scrolls.add(greenScroll);
scrolls.add(blueScroll);
scrolls.add(saturationScroll);
scrolls.add(brightnessScroll);
Labels.setLayout(new GridLayout(6,1,2,2));
Labels.add(redLabel);
Labels.add(greenLabel);
Labels.add(blueLabel);
Labels.add(hueLabel);
Labels.add(saturationLabel);
Labels.add(brightnessLabel);
}
public void adjustmentValueChanged(AdjustmentEvent eve)
{
int r1,g1,b1;
r1=redScroll.getValue();
g1=greenScroll.getValue();
b1=blueScroll.getValue();
if(r!=r1||g!=g1||b!=b1)
{
r=r1;
g=g1;
b=b1;
Color.RGBtoHSB(r,g,b,hsb);
}
else
{
hsb[0]=hueScroll.getValue()/255.0F;
hsb[1]=saturationScroll.getValue()/255.0F;
hsb[2]=brightnessScroll.getValue()/255.0F;
int rgb=Color.HSBtoRGB(hsb[0],hsb[1],hsb[2]);
r=(rgb>>16)&0xFF;
g=(rgb>>8)&0xFF;
b=rgb&0xFF;
}
redLabel.setText("R="+r);
greenLabel.setText("G="+g);
blueLabel.setText("B="+b);
hueLabel.setText("H="+hsb[0]);
saturationLabel.setText("S="+hsb[1]);
blueLabel.setText("B="+hsb[2]);
redScroll.setValue(r);
greenScroll.setValue(g);
blueScroll.setValue(b);
hueScroll.setValue((int)(255*hsb[0]));
saturationScroll.setValue((int)(255*hsb[1]));
brightnessScroll.setValue((int)(255*hsb[2]));
colorCanvas.setBackground(new Color(r,g,b));
colorCanvas.repaint();
}
public Insets getInsets()
{
return new Insets(3,3,3,3);
}
}

embed.html


/* To run:
*javac ColorChooserApplet.java
*appletviewer embed.html
*/

No comments:

Post a Comment