Applet Basics Applet Basics Applet Basics
Applet Basics Applet Basics Applet Basics
• Here, newColor specifies the new color. The class Color defines the constants
shown here that can be used to specify colors:
• Color.black Color.magenta
• Color.blue Color.orange
• Color.cyan Color.pink
• Color.darkGray Color.red
• Color.gray Color.white
• Color.green Color.yellow
• Color.lightGray
• The following example sets the background color to green and the text color to
red:
• setBackground(Color.green);
• setForeground(Color.red);
• A good place to set the foreground and background colors is in the init( ) method.
Ofcourse, you can change these colors as often as necessary during the execution
of your applet.
• You can obtain the current settings for the
background and foreground colors by calling
• getBackground( ) and getForeground( ),
respectively. They are also defined by
Component and are shown here:
• Color getBackground( )
• Color getForeground( )
• Here is a very simple applet that sets the
background color to cyan, the foreground
color to red, and displays a message that
illustrates the order in which the init( ), start(
), and paint( ) methods are called when an
applet starts up:
/* A simple applet that sets the foreground and
background colors and outputs a string. */
import java.awt.*;
import java.applet.*;
/*<applet code="Sample" width=300
height=50>
</applet>*/
public class Sample extends Applet{
String msg;
// set the foreground and background colors.
public void init() {
setBackground(Color.cyan);
setForeground(Color.red);
msg = "Inside init( ) --";
}
// Initialize the string to be displayed.
public void start() {
msg += " Inside start( ) --";
}
// Display msg in applet window.
public void paint(Graphics g) {
msg += " Inside paint( ).";
g.drawString(msg, 10, 30);
}
}