ptg7068951
240 HOUR 17:Creating Interactive Web Programs
LISTING 17.1 The Full Text of SalutonApplet.java
1: importjava.awt.*;
2:
3: public classSalutonApplet extendsjavax.swing.JApplet {
4: String greeting;
5:
6: public voidinit() {
7: greeting= “Saluton mondo!”;
8: }
9:
10: public voidpaint(Graphics screen) {
11: Graphics2D screen2D = (Graphics2D) screen;
12: screen2D.drawString(greeting, 25, 50);
13: }
14: }
The SalutonApplet program stores the string “Saluton mondo!”inside the
init()method in lines 6–8 and displays it in the applet window in line 12.
The applet does not need to use the start(), stop(), or destroy()meth-
ods, so they are not included in the program. You run the applet after
learning more about how it was coded.
Drawing in an Applet Window
Text is displayed in an applet window by using the drawString()method
of the Graphics2Dclass, which draws text in a user interface component.
ThedrawString()method is similar to the System.out.println()method
that you’ve been using to display text in applications.
The following three arguments are sent to drawString():
. The text to display, which can be several different strings and vari-
ables strung together with the +operator
. The x position in an (x,y) coordinate system, where the string should
be displayed
. The y position in an (x,y) coordinate system, where the string should
be displayed
The (x,y) coordinate system in an applet is used with several methods. It
begins with the (0,0) point in the upper-left corner of the applet window.
The x values count up as you move to the right and y values count up as
you move down.