Last week, you saw the Hello World example application :
class HelloWorld
{
public static void main(String args[])
{
System.out.println("Hello World!");
}
}
Below is listing for the applet version of the program. Use an editor to
type the program listing as shown below and save it in a file called
HelloWorldApplet.java. import java.awt.Graphics;
public class HelloWorldApplet extends java.applet.Applet
{
public void paint(Graphics g)
{
g.drawString("Hello World!", 5, 10);
}
}
The import statement on the first line enables you to refer to the
java.awt.Graphics class as just Graphics. Without it,
the definition of the paint method would look like:
public void paint(java.awt.Graphics g)
There are three important differences between the application and the applet
versions:
-
The HelloWorldApplet class is a public class.
All applets must be public otherwise the browsers will not be
able to execute them!
-
The extends keyword after the name of the
class. It indicates to Java that the class preceeding the extends
keyword is a subclass of the class following it. Ie, class
HelloWorldApplet is a subclass of class
java.applet.Applet. This is how subclassing is done in Java. If a
class definition doesn't include the extends clause, its superclass is assumed
to be java.lang.Object
-
Instead of the method definition for main(), HelloWorldApplet
overrides the default paint() method. Recall that the order of
execution of methods in applets is different to that of applications. Applets
are first initialised with the init() method, then started and
stopped with the start() and stop() methods. Any
screen activity is done via the paint() method. All the
applets that you create will be subclasses of the java.applet.Applet
class. Hence, they will inherit these main activity methods as
discussed in the previous section.
Since all the HelloWorldApplet applet is doing is writing (painting) the
words "Hello World!" to the screen, we only need to override the
paint() method. Without any instance variables to initialise or
objects to create, there is no need to override the default
init() method. Similarly with the other activity
methods.
The paint() method takes 1 argument, a Graphics object. When the
browser loads and executes the applet, it creates a Graphics object which it
passes to the paint() method whenever the paint()
method is called. Ie, each time the applet is painted or repainted.
paint() contains only one statement, a method call to
drawString which is a method defined in the Graphics class. It
takes 3 arguments. The first argument is the string to be drawn, the second and
third are the x and y coordinates respectively. drawString draws
the string specified in the first argument at the x and y coordinates specified
by the second and third arguments. In this case, it draws the string "Hello
World!" starting from point (5, 10). Note that in Java (and most other
programming languages), the screen coordinates start from the top-left
corner of the drawing area.
 |
The red square is point (5, 10) in Java's screen coordinate system.
This is where the drawString method will start drawing the
"Hello World!" string. |
When you have finished typing the program as shown above, compile it by
typing javac HelloWorldApplet.java. Just like in the previous
example, a HelloWorldApplet.class file will be generated. However, you can't run
the bytecode file with the Java interpreter. If you try, you will get the
following error message:
In class HelloWorldApplet: void main(String argv[]) is undefined
That's
because there is no definition of main() in our program, only a
definition of paint(). Since the first method the Java interpreter
looks for is main(), when it fails to find it, the returns the
above error.
In order to run the applet, it must be embedded in a World Wide Web document,
and then executed with a Java enabled browser or the
appletviewer that comes with the Java Developer's kit. Create
the following web page:
<html> <body> <p> My first applet:</p> <applet
code="HelloWorldApplet.class" width=150 height=100> <b>You need a Java
enabled browser to view this applet</b> </applet> </body>
</html>You can save the web page as any name you like, for example
Hello.html. The important part is getting the reference to the
"HelloWorldApplet.class" correct (remember that Java is case sensitive).
Now, you can test the applet with the appletviewer or a Java enabled browser.
To test it with a browser, open the web page you just created and the browser
will execute the applet. To test it with the appletviewer, type:
appletviewer Hello.html
The appletviewer will perform the same
function as the browser (load, initialise, start the applet; create the Graphics
object for the paint method), but it will only show the applet, nothing else. As
its name suggest, it is purely for the viewing of applets. When the applet loads
and executes, it should look like this:
That's it. You have just created and executed your very first applet.