The Color class is used to create colors in the default RGB color space or colors in any color spaces defined by a ColorSpace. Every color has an implicit alpha value of 1.0 or an explicit one provided in the constructor. The alpha value defines the transparency of a color and can be represented by a float value in the range 0.0 – 1.0 or 0 – 255. An alpha value of 1.0 or 255 means that the color is completely opaque and an alpha value of 0 or 0.0 means that the color is completely transparent. When constructing a Color with an explicit alpha or getting the color/alpha components of a Color, the color components are never pre multiplied by the alpha component.
import java.awt.*;
import java.awt.event.*;
import java.applet.*;
/* <APPLET CODE ="ColorClass.class" WIDTH=300 HEIGHT=200> </APPLET> */
public class ColorClass extends Applet implements ItemListener
{
Choice l;
public void init()
{
l=new Choice();
l.addItem("Black");
l.addItem("Blue");
l.addItem("Cyan");
l.addItem("Dark Gray");
l.addItem("Gray");
l.addItem("Green");
l.addItem("Light Gray");
l.addItem("Magenta");
l.addItem("Orange");
l.addItem("Pink");
l.addItem("Red");
l.addItem("White");
l.addItem("Yellow");
add(l);
l.addItemListener(this);
}
public void itemStateChanged(ItemEvent e)
{
repaint();
}
public void paint(Graphics g)
{
if(l.getSelectedItem()=="Black")
{
setBackground(Color.black);
}
if(l.getSelectedItem()=="Blue")
{
setBackground(Color.blue);
}
if(l.getSelectedItem()=="Cyan")
{
setBackground(Color.cyan);
}
if(l.getSelectedItem()=="Dark Gray")
{
setBackground(Color.darkGray);
}
if(l.getSelectedItem()=="Gray")
{
setBackground(Color.gray);
}
if(l.getSelectedItem()=="Green")
{
setBackground(Color.green);
}
if(l.getSelectedItem()=="Light Gray")
{
setBackground(Color.lightGray);
}
if(l.getSelectedItem()=="Magenta")
{
setBackground(Color.magenta);
}
if(l.getSelectedItem()=="Orange")
{
setBackground(Color.orange);
}
if(l.getSelectedItem()=="Red")
{
setBackground(Color.red);
}
if(l.getSelectedItem()=="Pink")
{
setBackground(Color.pink);
}
if(l.getSelectedItem()=="White")
{
setBackground(Color.white); }
if(l.getSelectedItem()=="Yellow")
{
setBackground(Color.yellow);
}
}
}