Java applets

Applet is a piece of Java code embedded within web page and runs in web browser window. All applets must extent the java.applet. Applet class. Applet class provides interface between Java and web browser.

Any applet should override some or all initialization methods offered by Applet class:
- init() - used for one-time execution, this method acts as a constructor of this object
- start() - used to perform more complex operations (user interaction, start timer...)
- stop() - suspend the applets execution, release the resources
- destroy() - release the resources

My first applet

Let’s make an applet which moves some text left and right. It consists of single class called Mooving.java:

import java.applet.Applet;
import java.awt.Font;
import java.awt.Graphics;
import java.util.Timer;
import java.util.TimerTask;


public class Mooving extends Applet {

private static final long serialVersionUID = 1L;
private int speed = 10;
private String label;
private Timer timer;

int x = 0;
int dx = 1;
int y = 0;

@Override
public void init() {
String textSpeed = getParameter("speed");
speed = (textSpeed == null) ? 30 :
(1000 / Integer.parseInt(textSpeed));
label = getParameter("lbl");
if (label == null)
label = "Empty";
Font font = new java.awt.Font("Arial", Font.PLAIN, 18);
setFont(font);
}

public void start() {
timer = new Timer();
timer.schedule(new TimerTask() {
public void run() {
repaint();
}
}
, speed, speed);
}

@Override
public void paint(Graphics g) {

y = g.getFont().getSize();
g.drawString(label, x, y);

if (x == 100) {
dx = -1;
}
if (x == 0) {
dx = 1;
}

x = x + dx;

}

public void stop() {
timer.cancel();
timer = null;
}
public String getAppletInfo() {
return "Title: Moving text\n"
+ "Author: Matjaz Cerkvenik\n";
}

}

Find more examples here

Deploying single-class applets into html

If you applet consists of only one class, there is no need to build jar of it. Deploy only already compiled classes - search for them in build/classes directory (not .java files). You can deploy java class into html using applet tag (in this example .html and .class files should be in the same directory):

<html>
<head>
<title>Moving text</title>
</head>
<body>
<applet codebase="." code="Mooving.class" width=400 height=50>
Your browser does not support applets!
<param name=lbl value="Hello Applets World!">
<param name=speed value="30">
</applet>
</body>
</html>

If applet runs OK you should not see text ‘Your browser does not support applets’.

If you are running java application from jar, use the following code:

<applet code="MyApp" archive="MyApp.jar" height="200" width="300"></applet>

REMARK: the use of <applet> tag is discouraged since the tag is marked as deprecated. You have to do it in other way (see below).

Another way to embed java class into html is with object tag:

<html>
<head>
<title>Moving text</title>
</head>
<body>
<object classid="123456" width="400" height="50">
<param name="code" value="Mooving.class">
<param name="codebase" value=".">
<param name="type" value="application/x-java-applet;version=1.4">
<param name="lbl" value="Hello Applets World!">
<param name="speed" value="30">
</object>
</body>
</html>

Click HERE to see working example. In both cases there are two parameters 'lbl' and 'speed' that are passed to java class with getParameter() method. Set the custom values for these two parameters without recompiling the class.

To support all browsers use this:

<!--[if !IE]>-->
<object classid="java:MyA.class"
codetype="application/x-java-applet"
archive="MyA.jar"
height="200" width="300">
<param name="aName" value="whatever"/>
<param name="anotherName" value="blabla"/>
<!--<![endif]-->
<object classid="clsid:8AD9C840-044E-11D1-B3E9-00805F499D93" height="200" width="300">
<param name="code" value="MyA"/>
<param name="archive" value="MyA.jar"/>
<param name="aName" value="whatever"/>
<param name="anotherName" value="blabla"/>
</object>
<!--[if !IE]>-->
</object>
<!--<![endif]-->