Warning: strpos(): needle is not a string or an integer in /home/thebestp/public_html/buildingblocksjava/wp-content/plugins/textlinkads/textlinkads.php on line 175

Warning: strpos(): needle is not a string or an integer in /home/thebestp/public_html/buildingblocksjava/wp-content/plugins/textlinkads/textlinkads.php on line 175

Warning: strpos(): needle is not a string or an integer in /home/thebestp/public_html/buildingblocksjava/wp-content/plugins/textlinkads/textlinkads.php on line 175
Examining the Hello World Applet | Building Blocks Java

Examining the Hello World Applet

The Hello World Applet adds several constructs to what we saw in the Hello World Application. Moving from top to bottom the first thing you notice is the two lines:

import java.applet.Applet;
import java.awt.Graphics;

The import statement in Java is similar to the #include statement in C or C++. It pulls in the classes that are contained in a package elsewhere. A package is merely a collection of related classes. In this case we’re requesting access to the public classes contained in the basic release, java.applet.Applet and java.awt.Graphics. Depending on the phase of the moon, awt stands for “advanced window toolkit” or “applet window toolkit”. You’ll see a lot more of it.

The next change from the application is the Class definition:

public class HelloWorldApplet extends Applet

The extends keyword indicates that this class is a subclass of the Applet class; or, to put it another way, Applet is a superclass of HelloWorldApplet. The Applet class is defined in the java.applet.Applet package which we just imported. Since HelloWorldApplet is a subclass of the Applet class, our HelloWorldApplet automatically inherits all the functionality of the generic Applet class. Anything an Applet can do, the HelloWorldApplet can do too.

The next difference between the applet and the application is far less obvious (except maybe to a longtime C programmer). There’s no main method! Applets don’t need them. The main method is actually in the browser or the AppletViewer, not in the Applet itself. Applets are like plugin code modules for Adobe Photoshop that provide extra functionality, but can’t run without a main program to host them.

Rather than starting at a specific place in the code applets are event driven. An applet waits for one of a series of events such as a key press, the mouse pointer being moved over the applets visible area, or a mouse click and then executes the appropriate event handler. Since this is our first program we only have one event handler, paint.

Most applets need to handle the paint event. This event occurs whenever a part of the applet’s visible area is uncovered and needs to be drawn again.

The paint method is passed a Graphics object which we’ve chosen to call g. The Graphics class is defined in the java.awt.Graphics package which we’ve imported. Within the paint method we call g’s drawString method to draw the string “Hello World!” at the coordinates (50,25). That’s 50 pixels across and twenty-five pixels down from the upper left hand corner of the applet. We’ll talk more about coordinate systems later. This drawing takes place whenever a portion of the screen containing our applet is covered and then uncovered and needs to be refreshed.

Comments are closed.