Writing a Simple Java Applet

The other model of a Java program is the applet that runs inside the appletviewer program or a Java-capable Web browser. Specifically, a Java applet is a class in the Java Abstract Windowing Toolkit (AWT). In an applet, you do not have to provide a main method. Instead, you provide a paint method where you place code to draw in an area of a window. You can use the applet model to implement GUIs and other graphical programs.

For a "Hello, World!" applet, I'll do the following:

♦ Instead of displaying the message in a default font, pick a specific font to display the message.

♦ Use the information about the font sizes to center the message within the area where the applet is required to display its output.

♦ Draw the text in red instead of the default black color.

Listing 26-1 shows the Java code that implements the "Hello, World!" applet.

Listing 26-1: The HelloWorld Java Applet

//---------------------------------------------------------------

// Displays "Hello, World!" in Helvetica font and in red color.

//---------------------------------------------------------------

import java.applet.*; import java.awt.*;

//---------------------------------------------------------------

// Applet class to display "Hello, World!"

public class HelloWorld extends Applet {

private String hellomsg = "Hello, World!"; private static final long serialVersionUID = 1L;

//---------------------------------------------------------------

// Method that paints the output public void paint(Graphics gc) {

// Draw a rectangle around the applet's bounding box // so we can see the box.

gc.drawRect(0, 0, getSize().width-1, getSize().height-1);

// Create the font to be used for the message.

Font helv = new Font("Helvetica", Font.BOLD, 24);

// Select the font into the Graphics object. gc.setFont(helv);

// Get the font metrics (details of the font size). FontMetrics fm = gc.getFontMetrics(); int mwidth = fm.stringWidth(hellomsg); int ascent = fm.getAscent(); int descent = fm.getDescent();

Listing 26-1 (continued)

// Compute starting (x.y) position to center string

// The size() method returns size of the applet's

int ystart = getSize().height/2 + ascent/2 - descent/2;

gc.setColor(Color.red);

gc.drawString(hellomsg, xstart, ystart);

By browsing through this code, you can learn a lot about how to display graphics output in Java. Here are the key points to note:

♦ The import statement lists external classes that this program uses. The name that follows the import statement can be the name of a class or can be a name with a wildcard (*), which tells the Java compiler to import all the classes in a package. This example uses the Applet class as well as a number of graphics classes that are in the java.awt package.

♦ The HelloWorld applet is defined as an extension of the Applet class. That's what the statement public class HelloWorld extends java.applet.Applet means.

♦ An applet's paint method contains the code that draws the output. The paint method receives a Graphics object as an argument. You have to call methods of the Graphics object to display output.

♦ The getSize method returns the size of the applet's drawing area.

♦ To use a font, you have to first create a Font object and then call the setFont method of the Graphics object.

♦ To draw text in a specific color, invoke the setColor method of the Graphics object with an appropriate Color object as argument.

♦ If you know C++, you'll notice that Java's method invocation is similar to the way you call the member function of a C++ object. Indeed, there are many similarities between C++ and Java.

Save the listing in a file named HelloWorld.java. Then, compile it with the command:

javac HelloWorld.java

This step creates the applet class file: HelloWorld.class. To test the applet, you have to create an HTML document and embed the applet in that document, as shown in the following example:

<title>Hello, World! from Java</title>

<h3>"Hello, World!" from Java</h3>

A Java applet is given an area where it displays its output. In this example, the applet draws a border around its assigned area and then displays the "Hello, World!" message centered in that box. <br>

<applet code=HelloWorld width=200 height=60> If you see this message, then you do not have a Java-capable browser. </applet>

Here is the applet!

As this HTML source shows, you have to use the <applet> tag to insert an applet in an HTML document.

You can use two tools to test the applet. The first one is appletviewer, which comes with JDK. To view the HelloWorld applet, you have to run the appletviewer program and provide the name of the HTML document that includes the applet. Suppose that the HTML document is in the file named hello.html. Then, you'd run appletviewer with the following command:

appletviewer hello.html Figure 26-1 shows how the appletviewer displays the applet.

Was this article helpful?

0 0
Make Money Writing

Make Money Writing

This Report Will Show You How To Make Money By Providing Writing Services To Other Internet Marketers. Learn how to make money by writing the right way. Grab your copy of this report now and learn. Why writing is a great way to earn money. How to compete with cheap writers, even if you charge a lot more money.

Get My Free Ebook


Post a comment