Androidberry logo Androidberry text logo
AndroidBerry Home
  • Learn Java
  • Java Programs
  • Interview Questions
  • Androidberry facebook
  • Androidberry twitter
  • Androidberry google plus

Java Tutorials


Basic Introduction

Decision Making

Looping

Classes and Objects

Inheritance

Interface

Packages

Exception Handling

Multithreading

Java Multithreading
Creating a Thread
Thread Life Cycle
Thread Exceptions
Thread Synchronization

Java Applets

What is Applet?
Java Applet vs Application
Creating a Applet
Displaying Applet on Webpage
Applet Lifecycle
Applet Tag
Passing Parameters to Applet
Setting Color to applet
Update and Repaint method
Using Fonts in Applet

Graphics Programming

Intro. to Graphics class
Drawing - Lines , Rectangles
Drawing - Circles and Ellipse
Drawing - Arcs
Drawing - Polygons
Drawing - Bar Charts

More on Java

Java Programs
Java Keywords Dictionary
Java Interview Questions

Java Setting colors to an Applet


Java Setting Applet Color

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

1. Setting Background Color

For applying the background color java applet has setBackground() method

Syntax

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

2. Setting Foreground Color

For applying colors to text, foreground color is used.

For applying the Foreground color, java applet has setForeground() method

Syntax

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 Constants

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

Example

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);
    }
}

Output

Java Applet Color Example
<< Previous Next >>

See Also

All Java Programs
Java Keywords
Java Interview Questions

AndroidBerry Support

About us
Contact us
Suggest us
Questions?

Follow us

Androidberry FacebookFacebook
Androidberry TwitterTwitter
Androidberry GooglePlusGoogle+
Back to top
Androidberry Name Logo