R4R
Right Place For Right Person TM
 

R4R JAVAAWT ExamplesSet color in java

previous

Home

Next

Set color 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
void setColor (Color newcolor ); This method is used to set color of object.
 
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(Color newcolor). Here newcolor specifies the new drawing color which is red for  Line and blue for rectangle.



//program for set color
import java.awt.*;
import java.applet.*;
/*
<applet code="SetColor" width=300 height=300>
</applet>
*/
public class SetColor extends Applet{
public void paint(Graphics g){
g.setColor(Color.red);
g.drawLine(170,170,270,270);
g.setColor(Color.blue);
g.drawRect(0,0,40,40);
g.fillRect(50,50,100,100);
}
}

Download source code

Output of  Example

previous

Home

Next


Tolal:0 Click:
Show All Comments

Post Your Comments

Your Name:

Your Email ID :
Comments :
URL
  =* Enter SUM

Tutorials,Examples,Questions and Answer

R4R
R4R
R4R
R4R
R4R
R4R
R4R
R4R