Personal tools
You are here: Home Information Systems Java Platform Introduction 17. Hello World Applet

17. Hello World Applet

Document Actions
  • Send this
  • Print this
  • Content View
  • Bookmarks

Hello World Applet

<-- Previous

Table of Contents

Next -->

Applets draw heavily on the functionality of class inheritance and OOP. The first thing we have to understand about Applets is that the Applet classes that we build are all subclasses of the Applet class, and for this reason when building them we have to use the keyword extends to indicate that we are building a subclass:

public class MyApplet extends Applet {
   public void paint(Graphics g) {
   
   }
}

As all Applets are subclasses of Applet they inherit all the methods of Applet and its superclasses - see the image below.

'Hello World!' Applet

To build Applet classes, we must import the functionality that we need as the Applet class is not part of the default java.lang package. Here is the code for your first Applet, the traditional Hello World! application:

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

/**
 * Paints the string Hello World! to the browser page.
 */
public class HelloWorldApplet extends Applet {
    public void paint(Graphics g) {
        g.drawString("Hello world!", 50, 25);
    }
}

The drawString(String str, int x, int y) method of the Graphics object draws its string argument at the position given by int x, int y on the Applet's coordinate system:

Not too complex so far. Compile HelloWorldApplet, and then build the following <APPLET> tag in an HTML page:

<HTML>
<HEAD>
<TITLE>Hello World Applet<TITLE>
</HEAD>
<BODY>
<APPLET code="HelloWorldApplet.class" width=180 height=50></APPLET>
</BODY>
</HTML>

Create a sub-folder applets in the ~/bin/ folder, save the file as HelloWorldApplet.html, and place the HelloWorldApplet.class file into the same directory as the HelloWorldApplet.html file. Now launch the file in your browser to see the Applet in action.

You don't have to run the Applet in a browser - Java comes equipped with an appletviewer that will load the web page for you. At the command prompt type:

appletviewer HelloWorldApplet.html

and press the 'Enter' key - this is what you should see:

As you saw, the string Hello World! has been placed at a position 50 pixels from the left of the pane (X coordinate) and 25 pixels from the top of the pane (Y coordinate). In computer coordinate systems, by convention the X dimension runs from left to right, and the Y dimension from top to bottom.

<-- Previous

Table of Contents

Next -->

Copyright 2007-2008, by the Contributing Authors. Cite/attribute Resource. 17. Hello World Applet. (2008, July 16). Retrieved May 24, 2013, from UWC Free Courseware Web site: http://freecourseware.uwc.ac.za/freecourseware/information-systems/java-platform-introduction/hello-world-applet. This work is licensed under a Creative Commons License : Attribution-ShareAlike 3.0. Creative Commons License : Attribution-ShareAlike 3.0