Powered by Blogger.

About Java: Colors in Java Selector

Description:

The java.awt.Color class is used to create new colors, and predefines a few color constants (see below). Java allows the creation of up to 24 million different colors.

Because our eyes have three kinds of neurons that respond primarily to red, green, and blue, it's possible for computer monitors (and TVs) to create all colors using the RGB system. The RGB system Java uses combines red, green, and blue in amounts represented by a number from 0 to 255. For example, red is (255, 0, 0) -- 255 units of red, no green, and no blue. White is (255, 255, 255) and black is (0,0,0).

Color Constants:

Java originally defined a few color constant names in lowercase, which violated the naming rule of using uppercase for constants. These are best to use since they are available in all versions of Java: Color.black, Color.darkGray, Color.gray, Color.lightGray, Color.white, Color.magenta, Color.red, Color.pink, Color.orange, Color.yellow, Color.green, Color.cyan, Color.blue

Java 1.4 added the proper uppercase names for constants: Color.BLACK, Color.DARK_GRAY, Color.GRAY, Color.LIGHT_GRAY, Color.WHITE, Color.MAGENTA, Color.RED, Color.PINK, Color.ORANGE, Color.YELLOW, Color.GREEN, Color.CYAN, Color.BLUE

Constructor:

Color c = new Color(int red, int green, int blue)

This creates a new color object that is a combination of red, green, and blue. The values of each color must be in the range 0-255.

Don't do anything to a Color:

A Color object is created only to be passed as a parameter. You will never call any Color methods.

Example:

Color c = new Color(255, 255, 240);
this.setBackground(c);

Advanced:

Additional constructors use single packed ints or floating point values. In addition, there is support for the HSB (Hue, Saturation, Brightness) color model and transparency. There is a Color Chooser in Java 2 that lets the user choose a color.   

Colors in Java:

In Java, we can control the colors used for the foreground using setForeground() method and the background using setBackground() method of AWT components. The setForeground() and setBackground() methods take an arguments that is an instance of of java.awt.Color class. We can use the constant colors referred to as Color.red, Color.blue, Color.green, Color.yellow, and so on. We can also construct a specific Color object by specifying the color by a combination of three byte-sized integers (0-255), one for each primary color: red, green, and blue.

Colors in Java Program:

 import java.awt.*;
 import java.awt.event.*;

 public class TestColors implements WindowListener, ActionListener{
private Frame f;
private Button b;

public TestColors(){
f = new Frame(”Frame Title”);
b = new Button(”Change Color”);
b.setActionCommand(”button press”);
}

public void launchFrame(){
b.addActionListener(this);
b.setForeground(Color.red);
b.setBackground(Color.yellow);
f.add(b);
f.addWindowListener(this);
f.setSize(300, 300);
f.setBackground(Color.green);
f.setLayout(new FlowLayout());
f.setVisible(true);
}

public void actionPer formed(ActionEvent e){
int x, y, z;
if(e.getActionCommand()==”button press”){
x = (int) (Math.random()*100);
y = (int) (Math.random()*100);
z = (int) (Math.random()*100);
Color c = new Color(x, y, z);
f.setBackground(c);
}
}

public void windowClosing(WindowEvent e){
System.exit(0);
}

public void windowOpened(WindowEvent e){}
public void windowIconified (WindowEvent e){}
public void windowDeiconified (WindowEvent e){}
public void windowClosed(WindowEvent e){}
public void windowActivated(WindowEvent e){}
public void windowDeactivated(WindowEvent e){}

public static void main(String[]args){
TestColors tc = new TestColors();
tc.launchFrame();
}
}

1 comment: