Hello World

Putting an applet in a web page requires the use of the <applet> tag. To use this tag you need to know how to use the parameters that indicate where the Java class file is and any other details that it will require when it starts up within the browser. Applets start in a different way to applications. Applications start with the “magically named” method main which is invoked when the application is started from the command line. Applets by contrast are controlled by the action of loading the web page that they were embedded in and have a different sequence of methods invoked at start up time.


In the same way that the first application was HelloWorld, here is an example of a basic HelloApplet.

public class HelloApplet extends javax.swing.JApplet{
	public void paint(java.awt.Graphics g){

Note that the HelloApplet class extends the javax.swing.JApplet class, which provides the graphical capabilities in a similar way that a command line graphical program might extend the javax.swing.JFrame class. An applet such as this is compiled in the same way as an application, i.e.

javac HelloApplet.java

and the resultant output will be a file called


In order to view this applet you need to create a basic HTML file that embeds the applet in the web page.

It can be as little as the single lines

<applet code=HelloApplet.class width=200 height=100> </applet>

Note how the reference to the applet includes the .class extension, unlike when you run an application where you do not include a reference to the class extension. Note how there is nothing between each parameter to the class, i.e. There is no comma between width=200 and height=100.

There are two ways of view applet, either using the appletviewer tool that comes with the JDK or by opening the html file using a Java enabled browser. One of the disadvantages of appletviewer is that it does not implement all the security precautions of the applet plugin. I have wasted a great deal of time testing a program with the appletviewer only to find it did not work in a browser because I was using methods that accessed files, so don't rely entirely on the applet viewer. However for basic testing and learning the appletviewer is quick and convenient and available from the command line. So to test HelloApplet you can type the following command line

appletviewer helloapplet.htm

The parameters to the drawString methods are the x,y co-ordinates, and in the example given it positions the output string at the top of the applet window. If you were writing the equivalent application you could pass the x,y co-ordinates from the command line and extract them from the String array that is the parameter to the main method. However there is no command line with a page embedded within a web browser. You can pass the equivalent of command line parameters to an applet by embedding them into the html and capturing them when the applet starts.

Passing parameters to an applet requires additional <param ....> tags between the starting <applet> tag and closing </applet> tag. For example, if you wanted to pass the x,y co-ordinates to the helloapplet you could have parameters passed with the following code.

<applet code=HelloApplet.class width=100 height=100>
<param name="greeting" value="bonjour">

Note that the name of the parameter can be just about anything that makes sense to you and the value will be passed as string to the applet. Examples of parameters being passed to applets are where an applet acts as a “news ticker” and the parameter is passed as part of the html. Of course the html can be generated from a server side interaction.

Last modified: Thursday, 24 July 2014, 2:54 PM