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
*/

COLOR PALATTE WITH MATRIX OF BUTTONS


package ish;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
public class ColorChooserTest
{
public static void main(String[] args)
{
ColorChooserFrame frame=new ColorChooserFrame();
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.show();
}
}
class ColorChooserFrame extends JFrame
{
public ColorChooserFrame()
{
setTitle("ColorChooserTest");
setSize(WIDTH,HEIGHT);
ColorChooserPanel panel=new ColorChooserPanel();
Container contentPane=getContentPane();
contentPane.add(panel);
}
public static final int WIDTH=300;
public static final int HEIGHT=200;
}
class ColorChooserPanel extends JPanel
{
public ColorChooserPanel()
{
JButton modalButton=new JButton("Modal");
modalButton.addActionListener(new ModalListener());
add(modalButton);
JButton modalessButton=new JButton("Modaless");
modalessButton.addActionListener(new ModelessListener());
add(modalessButton);
JButton immediateButton=new JButton("Immediate");
immediateButton.addActionListener(new ImmediateListener());
add(immediateButton);
}
private class ModalListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
Color defaultColor=getBackground();
Color Selected=JColorChooser.showDialog(ColorChooserPanel.this,"set background",
defaultColor);
setBackground(Selected);
}
}
private class ModelessListener implements ActionListener
{
public ModelessListener()
{
chooser=new JColorChooser();
dialog=JColorChooser.createDialog(ColorChooserPanel.this,"Backgroundcolor",false,chooser,new ActionListener()
{
public void actionPerformed(ActionEvent event)
{
setBackground(chooser.getColor());
}
},null);

}
public void actionPerformed(ActionEvent event)
{
chooser.setColor(getBackground());
dialog.show();
}
private JDialog dialog;
private JColorChooser chooser;
}
private class ImmediateListener implements ActionListener
{
public ImmediateListener()
{
chooser=new JColorChooser();
chooser.getSelectionModel().addChangeListener(new ChangeListener()
{
public void stateChanged(ChangeEvent event)
{
setBackground(chooser.getColor());
}
});
dialog=new JDialog((Frame)null,false);
dialog.getContentPane().add(chooser);
dialog.pack();
}
public void actionPerformed(ActionEvent event)
{
chooser.setColor(getBackground());
dialog.show();
}
private JDialog dialog;
private JColorChooser chooser;
}
}


COLOR PALETTE





import java.awt.*;

import java.awt.event.*;

import java.applet.*;

public class ColorPalette extends Applet implements ActionListener, ItemListener

{

Button btnRed, btnGreen, btnBlue;

String str = "";

CheckboxGroup cbgColor;

CheckboxGroup cbgImage;

Checkbox optFore, optBack;

Checkbox optMb, optWsb;

Image imgMb, imgWsb;

TextArea txtaComments = new TextArea("", 5, 30);

public void init() {

setLayout(new GridLayout(4, 3));

cbgColor = new CheckboxGroup();

cbgImage = new CheckboxGroup();

Label lblColor = new Label("Select the Area :") ;

Label lblImage = new Label("Select the Image :") ;

optFore = new Checkbox("Foreground", cbgColor,true);

optBack = new Checkbox("Background", cbgColor,false);

optMb = new Checkbox("REC-Main Block", cbgImage,true);

optWsb = new Checkbox("REC-Workshop Block",cbgImage, false);

btnRed = new Button("Red");

btnGreen = new Button("Green");

btnBlue = new Button("Blue");

//BB / REC - 17

imgMb = getImage(getDocumentBase(),getParameter("fidal"));

imgWsb = getImage(getDocumentBase(),getParameter("castro"));

add(btnRed);

add(btnGreen);

add(btnBlue);

add(lblColor);

add(optFore);

add(optBack);

add(lblImage);

add(optMb);

add(optWsb);

add(txtaComments);

optFore.addItemListener(this);

optBack.addItemListener(this);

optMb.addItemListener(this);

optWsb.addItemListener(this);

btnRed.addActionListener(this);

btnGreen.addActionListener(this);

btnBlue.addActionListener(this);

}

public void actionPerformed(ActionEvent ae) {

str = cbgColor.getSelectedCheckbox().getLabel() ;

if(ae.getSource() == btnRed &&

str.equals("Background")) {

txtaComments.setBackground(Color.red);

}

if(ae.getSource() == btnRed &&

str.equals("Foreground")) {

txtaComments.setForeground(Color.red);

}

if(ae.getSource() == btnGreen &&

str.equals("Background")) {

txtaComments.setBackground(Color.green);

}

if(ae.getSource() == btnGreen &&

str.equals("Foreground")) {

txtaComments.setForeground(Color.green);

}

if(ae.getSource() == btnBlue &&

str.equals("Background")) {

txtaComments.setBackground(Color.blue);

}

//BB / REC - 18

if(ae.getSource() == btnBlue &&

str.equals("Foreground")) {

txtaComments.setForeground(Color.blue);

}

}

public void itemStateChanged(ItemEvent ie) {

repaint();

}

public void paint(Graphics g) {

if(optMb.getState() == true)

g.drawImage(imgMb, 200, 400, this) ;

if(optWsb.getState() == true)

g.drawImage(imgWsb, 200, 400, this) ;

}

}