Thursday, 27 February 2014

INTRODUCING THE AWT:Working With Color

Java supports color in a portable, device-independent fashion.


Working with Color

Java supports color in a portable, device-independent fashion. The AWT color system allows you to specify any color you want. It then finds the best match for that color, given the limits of the display hardware currently executing your program or applet. Thus, your code does not need to be concerned with the differences in the way color is supported by various hardware devices. Color is encapsulated by the Color class.


color constructors


To create your own colors,using one of the color constructors.the most commly used forms are shown here:

  • Color (int red, int green, int blue)
  • Color (int rgbValue) //int rgbValue = (0xff000000 | (0xc0<<16) | (0x00 <<8) | 0x00);
  • Color (float red, float green, float blue)

The first constructors take three integers that specify the color as the mix of red,green and blue. these values must be between 0 and 255.

example:
new color(255,100,100); // light red.


The second color constructor takes a single integer that contain the mix of red,greenand blue packed into an integer.the integer is organized with red in bits 16 to 23,green in bits 8 to 15,and blue in bits 0 to 7.

example:
int newRed=(0xff000000 | (0xc0<<16) | (0x00 <<8) | 0x00);
color darkRed = new Color(newRed);

the final constructor,color(float,float,float),takes three float values(between 0.0 to 1.0) that specify the relative mix of red,blue and green.

Color Methods


 The color class defines several methods that help manipulate colors.they are:
  • Using Hue, Saturation, and Brightness:

       The  Hue-Saturation- Brightness(HSB)color model is an alternatives to red-green-blue(rgb) for specifying particular colors.hue is wheel of color, value between 0.0 to 1.0.saturation is light pastels to intense hues, value between 0.0 to 1.0 . brightness: 1 is bright white and 0 is black, value between 0.0 to 1.0.

 Color supplies two methods that let you convert between RGB andHSB. they are:

  •        static int HSBtoRGB (float hue, float saturation, float brightness)
  •       static float[] RGBtoHSB (int red, int green, int blue, float values[])


If values is not null, then this array is given the HSB values and returned. Otherwise, a new array is created and the HSB values are returned in it.


  •  getRed(), getGreen(), and getBlue()
                 you can obtain the red green,and blue components of a color independently using getRed(),getGreen(),and getBlue(), shown here:

syntax:
       int getRed()
       int getGreen()
       int getBlue()


  • getRGB()
                 To obtain a packed,RGB representation of a color,use getRGB().

syntax:
       int getRGB()


Setting the current Graphics Color

        

        By default ,graphics objects are drawn in the current foreground color.you cn change this color by calling the Graphics method setColor():

syntax:
void setColor(Color newColor)
here ,newColor specifies the new drawing color.

         you can obtain current color by calling getColor,Shown here:

syntax:
        Color getColor()


A color Demonstration Applet


import java.awt.*;
import java.applet.*;
/* <applet code="ColorDemo" width=300 height=200> */

public classColorDemo extends Applet
{
//draw line
public void main paint(Graphics g)
{
color c1 = new color(255,100,100);
g.setColor(c1);
g.drawLine(0,0,100,100);
g.drawLine(0,100,100,0);

g.setColor(color.red);
g.drawOval(10,10,50,50);
g.fillOval(70,90,140,100);
}
}

No comments :

Post a Comment