This hour’s first project is an applet that displays the string “Saluton mondo!”, the traditional Esperanto greeting that is becoming more traditional by the hour. You get a feel for how applets are structured by re-creating the Saluton
application from Hour 2, “Writing Your First Program,” as a program that can run on a web page.
Create a new empty Java file called SalutonApplet
, enter the text from Listing 17.1 into the file, and save the file.
1: import java.awt.*;
2:
3: public class SalutonApplet extends javax.swing.JApplet {
4: String greeting;
5:
6: public void init() {
7: greeting = "Saluton mondo!";
8: }
9:
10: public void paint(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()
methods, so they are not included in the program. You run the applet after learning more about how it was coded.
Text is displayed in an applet window by using the drawString()
method of the Graphics2D
class, which draws text in a user interface component. The drawString()
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 variables 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.
SalutonApplet
ProgramJava applets can’t be run like applications because they lack a main()
method.
Instead, to run an applet, you must add markup to a web page that contains the applet. To create an example web page for SalutonApplet
, create a new web page in NetBeans by following these steps:
1. Choose File, New File. The New File dialog opens.
2. Choose Other from the Categories pane and HTML File from File Types, then click Next. The New HTML File dialog opens.
3. Give the file the name SalutonApplet
and click Finish.
NetBeans opens the source code editor with some default HTML markup. Delete all the markup that’s been provided for you, enter Listing 17.2 into the editor, and save the file.
1: <html>
2: <head>
3: <title>Saluton Mondo!</title>
4: </head>
5: <body bgcolor="#000000" text="#FF00FF">
6: <p>This is a Java applet.</p>
7: <applet
8: code="SalutonApplet.class"
9: codebase="..\..\build\classes"
10: height="150"
11: width="300"
12: >
13: <p>You need a Java-enabled browser to see this.</p>
14: </applet>
15: </body>
16: </html>
The <applet>
tag is defined in lines 7–14, but line 13 will be ignored in any browser equipped to run Java.
After saving the file, you can view it in a web browser: In the Project pane to the left of the source code editor, right-click the filename SalutonApplet.html
, and then choose View
. The web page opens in your computer’s default web browser, as shown in Figure 17.1.
NetBeans can test applets without the necessity to create a web page. With the applet’s class file open in the source editor, choose the menu command Run, Run File. The applet is loaded by the appletviewer tool, which is part of the Java Development Kit (JDK).
When you run the applet in a browser, you might be asked whether it’s OK to run the program. Many web browsers must be configured to enable Java programs before they run Java applets.
Java applets are run in current browsers by the Java Plug-in, an interpreter from Oracle that supports the most up-to-date version of the language.
A plug-in is a program that works in conjunction with a web browser to expand its functionality. Plug-ins handle a type of data that the browser normally could not handle. Apple offers a plug-in to display QuickTime movies, Macromedia distributes a plug-in to run Flash animation files, and many other kinds of special content are supported in this manner.
The plug-in can be downloaded from Sun’s Java site at www.java.com.
3.12.147.77