Java AWT Tutorials

adplus-dvertising
Set Background Color in java
Previous Home Next

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

void setBackgroundColor (Color new color ): This method is used to set background color.

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.setBackgroundColor(Color new color). Here new color specifies the new drawing color for background.

Example

//program for set Background color
import java.awt.*;
import java.applet.*;
/*
<applet code="SetBackgroundColor" width=300 height=300>
</applet>
*/
public class SetBackgroundColor extends Applet
{
public void paint(Graphics g)
{
setBackground(Color.yellow);
g.setColor(Color.red);

g.setColor(Color.blue);
g.drawRect(0,0,40,40);
g.fillRect(50,50,100,100);
}
}

Output of Example:

Download Source Code

Previous Home Next