R4R
Right Place For Right Person TM
 

R4R JAVAAWT ExamplesColor Demonstration in java in java

previous

Home

Next

Color Demonstration in java in java

In this example we are going to set color of object.To set the color of object we have to use method setColor();
In this we are using Graphics class and Applet class

There are three method :
Color( int red, int green, int blue);
 This method is used to set color of object by using the mixture of three color(red, blue, green). these value must be between 0 to 255.
Color(int rgb Value); It take a single integer that contain the mixture of three color(red, blue, green) the integer is organized with red in bits 16 to 23, green in bits 8 to 15, blue in bits 0 to7.
Color(float red, float blue, float green); It takes three float value from range 0.0 to1.0
 paint(Graphics g); This method is used to paint the applet.

Here first we are importing two packages. Then after we have write applet tag into comments. In this tag we have pass three arguments code ,height  and width. The code attribute is used to call the applet class. And Height , width is used for Sizing the window.
Then we have create a class and extends this class with Applet Class to accurse the property of applet. We had override paint method of applet class.
In our example use g.setColor(c1). Here c1 specifies the new drawing color of Line .the value of c1=(255,100,100);



//Program for Collerdemonstration
import java.awt.*;
import java.applet.*;
/*
<applet code="ColorDemonstration" width=300 height=300>
</applet>
*/
public class ColorDemonstration extends Applet{
public void paint(Graphics g){
Color c1=new Color(255,100,100);
Color c2=new Color(100,255,100);
Color c3=new Color(100,100,255);
g.setColor(c1);
g.drawLine(170,170,270,270);
g.setColor(c2);
g.drawRect(0,0,40,40);
g.setColor(c3);
g.fillRect(50,50,100,100);
g.drawLine(0,10,100,10);
}
}

Download source code

Output of  Example

previous

Home

Next

New Updates

R4R
R4R
R4R
R4R
R4R
R4R
R4R
R4R