![]() |
![]() |
Java Multithreading | |
Creating a Thread | |
Thread Life Cycle | |
Thread Exceptions | |
Thread Synchronization |
Intro. to Graphics class | |
Drawing - Lines , Rectangles | |
Drawing - Circles and Ellipse | |
Drawing - Arcs | |
Drawing - Polygons | |
Drawing - Bar Charts |
Java Programs | |
Java Keywords Dictionary | |
Java Interview Questions |
Java applet allows us to modify background as well as foreground color of applet.
There are two methods to set the color for background and foreground
For applying the background color java applet has setBackground() method
void setBackground (Color ColorObject);
This method accepts Color object as parameter. We can either pass the color object or we can use Color contants As shown in below chart.
Creating color object
Color ColorObject = new Color (int RED,int GREEN,int BLUE);
The color values are between 0 to 255. Where 0 is black and 255 is more light color
Example
Color myColor = new Color (10,10,255); // light blue color setBackground (myColor);
Example Using color Constant
setBackground (Color.GREEN); // apply the green color
For applying colors to text, foreground color is used.
For applying the Foreground color, java applet has setForeground() method
void setForeground (Color ColorObject);
This method is similar to setBackground() method but only difference is that, it set ups the foreground color.
NOTE : Applet has default Foreground Color = 'Black', Background Color = 'Light gray'. |
Color Name | Syntax | Output |
---|---|---|
Black | Color.black or Color.BLACK |
|
Blue | Color.blue or Color.BLUE |
|
Cyan | Color.cyan or Color.CYAN |
|
Gray | Color.gray or Color.GRAY |
|
Dark Gray | Color.darkgray or Color.DARK_GRAY |
|
Light Gray | Color.lightgray or Color.LIGHT_GRAY |
|
Green | Color.green or Color.GREEN |
|
Magenta | Color.magenta or Color.MAGENTA |
|
Orange | Color.orange or Color.orange |
|
Pink | Color.pink or Color.PINK |
|
Red | Color.red or Color.RED |
|
White | Color.white or Color.WHITE |
|
Yellow | Color.yellow or Color.YELLOW |
Program to set background and foreground color using applet.
import java.awt.*; import java.applet.*; /* <applet code="AppletColors.class" width=200 height=200> </applet> */ public class AppletColors extends Applet { String msg; public void init() { msg = "AndroidBerry"; } public void paint(Graphics g) { Color c = new Color(255,255,0); g.drawString(msg, 15, 140); setForeground(c); setBackground(Color.blue); } }