Simple Applet Display Methods
As we’ve
mentioned, applets are displayed in a window, and AWT-based applets use the AWT
to perform input and output. Although we will examine the methods, procedures,
and techniques related to the AWT in subsequent chapters, a few are described
here, because we will use them to write sample applets. (Remember, Swing-based
applets are described later in this book.)
As
described in Chapter 13, to output a string to an applet, use drawString( ), which is a member of the
Graphics class. Typically, it is
called from within either update( )
or paint( ). It has the following
general form:
void
drawString(String message, int x, int y)
Here, message is the string to be output
beginning at x,y. In a Java window,
the upper-left corner is location 0,0. The drawString(
) method will not recognize newline characters. If you want to start a line
of text on another line, you must do so manually, specifying the precise X,Y
location where you want the line to begin. (As you will see in later chapters,
there are techniques that make this process easy.)
To set
the background color of an applet’s window, use setBackground( ). To set the foreground color (the color in which
text is shown, for example), use setForeground(
). These methods are defined by Component,
and they have the following general forms:
void
setBackground(Color newColor) void
setForeground(Color newColor)
Here, newColor specifies the new color. The
class Color defines the constants
shown here that can be used to specify colors:
Color.black
Color.blue
Color.cyan
Color.darkGray
Color.gray
Color.green
Color.lightGray
Color.magenta
Color.orange
Color.pink
Color.red
Color.white
Color.yellow
Uppercase
versions of the constants are also defined.
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. Of course, 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);
}
}
Sample
output is shown here:
The methods stop( ) and destroy( ) are not overridden, because they are not needed by this simple applet.
Related Topics
Privacy Policy, Terms and Conditions, DMCA Policy and Compliant
Copyright © 2018-2023 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.