Task: Write an applet that will display "Hello World! (updated x times)" - Where x is the number of times that it has been repainted.
Solution: For this task, we wish to create an Applet. It should paint a string on the applet! and so the 
paint()
method must be used. The steps that we must take are:
Subclass 
Applet - i.e. derive our applet from 
java.applet.Applet.
override the 
paint() member function of 
Applet with our own method.
Create a .html file to run the applet in.
The code solution is as follows: (listing for HelloWorldCount.java).
Here we have derived from the 
Applet
class (using extends 
Applet) and then overwritten the 
paint() method with our own code.
1
2
3 // HelloWorld Applet with added painting counter.
4 import java.awt.Graphics;
5 import java.applet.Applet;
6
7 public class HelloWorldCount extends Applet
8 {
9 private int count = 0;
10
11 public void paint(Graphics g)
12 {
13 count++;
14 g.drawString("Hello World! (updated " + count + " times)", 20, 20);
15 }
16 }
17
18 See it working here
How does this work? The import statements are used to include the class library files that we need to implement the
applet. This includes the 
Graphics class (to allow us to draw strings) and the applet library
with the generic 
Applet class.
We create a subclass 
HelloWorldCount of the 
Applet class
using the extends keyword.
We only added one method, the 
paint() method to the applet. This method is called by the browser (that passes the

Graphics object g) whenever the applet needs to be updated. This

Graphics object
g that is passed is used to display "Hello World! (updated x times)", using its 
drawString() method. This method displays
the string at the (x,y) location (20,20) relative to the top left hand corner of the applet.
We also need the 
count variable (state) in the class to allow us to remember how many times 
paint() was called
by the virtual machine. Each time the 
paint() method is called the count is incremented by 1 using the 
count++
call. This can then be displayed in the string by simply using the + operator which concatenates the int variable with the string.
Compile this piece of code by typing: javac HelloWorldCount.java
NB: while DOS is not case sensitive, parameters are - You must use the exact same case!
This will create a Java bytecode file in the same directory called: HelloWorld.class
The last step that we must perform is to create a .html file that this applet can be
displayed within. The code is quite straighforward: (listing for HelloWorldCount.html, again place in the
same directory)
1 2 3 <title> Test Applet Page </title> 4 <hr> 5 <applet code=HelloWorldCount.class width=200 height=200> 6 </applet> 7 <hr> 8 9
To view this example use the appletviewer, or your favorite Java enabled web browser. To use the appletviewer type:appletviewer HelloWorldCount.html
So to compile and execute your applet your Command Prompt should look like Figure 7.1, “HelloWorldCount Command Prompt”:
Your Applet should look like (See Figure 7.2, “HelloWorldCount Applet”): -
So why is the number 21 given in this capture of the applet. I have minimised and then maximised the applet a few times, but the major cause of the increments was that I dragged the corner of the applet to resize it - you can also drag another window over your applet and then re-expose it. Try this yourself.
Possible problems that can occur when stepping through this section are:
If you get "command not found" errors when you type javac or appletviewer,
then your PATH environment variable is not correctly configured to include the
x:\jdk1.x.x\bin directory - where x.x is your current Java version (mine is currently Java JDK 6)
(See section x.x for information on setting the PATH environment variable).
If you get "class not found" errors from the javac compiler then your CLASSPATH is probably not correctly configured. For this example it should include the general Java SDK libraries and the current directory, usually denoted as '.' (See section x.x for information on setting the CLASSPATH).
© 2006
Dr. Derek Molloy
(DCU).