A Sample Applet





A Sample Applet

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'll take a look at 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.

Load your word processor and create a new file called SalutonApplet.java. Enter the text of Listing 17.1 into the file and save it when you're done.

Listing 17.1. The Full Text of SalutonApplet.java

 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: }


This applet does not need to use the start(), stop(), or destroy() methods, so they are not included in the program. Compile the program with the javac compiler tool, if you're an JDK user, or another tool.

Drawing in An Applet Window

Text is displayed in an applet window by using the drawString() method of the Graphics2D class, which draws text in a graphical user interface component.

The drawString() method is similar in function to the System.out.println() method that displays information to the system's standard output device.

Before you can use the drawString() method, you must have a Graphics or Graphics2D object that represents the applet window.

The paint() method of all applets includes a Graphics object as its only argument, which can be cast to a Graphics2D object:


Graphics2D screen2D = (Graphics2D)screen;


When you have created a Graphics2D object like this, you can call its drawString() method to display text on the area represented by the object.

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 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. Figure shows how the (x,y) coordinate system works in conjunction with the statement on Line 12 of SalutonApplet.java.

1. Drawing a string to an (x,y) position.


Testing the SalutonApplet Program

Although you have compiled the SalutonApplet program into a class file, you cannot run it using a Java interpreter such as java. If you do, you'll get an error message looking like this:


Exception in thread "main" java.lang.NoSuchMethodError: main


The error occurs because a Java interpreter runs applications by calling its main() method. Applets don't include this method. Instead, to run an applet, you need to create a web page that loads the applet. To create a web page, open up a new file on your word processor and call it SalutonApplet.html. Enter Listing 17.2 and then save the file.

Listing 17.2. The Full Text of SalutonApplet.html

 1: <html>

 2: <head>

 3: <title>Saluton Mondo!</title>

 4: </head>

 5: <body bgcolor="#000000" text="#FF00FF">

 6: <center>

 7: This a Java applet:<br>

 8: <applet code="SalutonApplet.class" height=150 width=300>

 9: You need a Java-enabled browser to see this.

10: </applet>

11: </body>

12: </html>


All applets you write can be tested with the appletviewer tool that comes with the Java Development Kit. You can see the output of the SalutonApplet applet by typing the following:


appletviewer SalutonApplet.html


One thing to note about appletviewer is that it only runs the applets that are included in a web page, and does not handle any of the other elements such as text and images.

Applets can also be loaded by web browsers, if they are equipped with the Java Plug-in. To attempt this at a command line, type the following command:


SalutonApplet.html


You can also choose File, Open from the browser's menu to find and open the page.

Figure shows a screen capture of SalutonApplet loaded in Mozilla Firefox.

Figure. The SalutonApplet program running on a web page.


If you can't get this applet to run in Firefox or another web browser, the most likely reason is that the browser needs the Java Plug-in.


     Python   SQL   Java   php   Perl 
     game development   web development   internet   *nix   graphics   hardware 
     telecommunications   C++ 
     Flash   Active Directory   Windows