Applets
Introduction#
Applets have been part of Java since its official release and have been used to teach Java and programming for a number of years.
Recent years have seen an active push to move away from Applets and other browser plugins, with some browsers blocking them or actively not supporting them.
In 2016, Oracle announced their plans to deprecate the plugin, Moving to a Plugin-Free Web
Newer and better APIs are now available
Remarks#
An applet is a Java application that normally runs inside a web browser. The basic idea is to interact with the user without the need to interact with the server and transfer information. This concept was very successful around the year 2000 when internet communication was slow and expensive.
An applet offers five methods to control their life cycle.
method name | description |
---|---|
init() |
is called once when the applet is loaded |
destroy() |
is called once when the applet gets removed from memory |
start() |
is called whenever the applet gets visible |
stop() |
is called whenever the applet get overlapped by other windows |
paint() |
is called when needed or manually triggered by calling repaint() |
Minimal Applet
A very simple applet draws a rectangle and prints a string something on the screen.
public class MyApplet extends JApplet{
private String str = "StackOverflow";
@Override
public void init() {
setBackground(Color.gray);
}
@Override
public void destroy() {}
@Override
public void start() {}
@Override
public void stop() {}
@Override
public void paint(Graphics g) {
g.setColor(Color.yellow);
g.fillRect(1,1,300,150);
g.setColor(Color.red);
g.setFont(new Font("TimesRoman", Font.PLAIN, 48));
g.drawString(str, 10, 80);
}
}
The main class of an applet extends from javax.swing.JApplet
.
Before Java 1.2 and the introduction of the swing API applets had extended from java.applet.Applet
.
Applets don’t require a main method. The entry point is controlled by the life cycle. To use them, they need to be embedded in a HTML document. This is also the point where their size is defined.
<html>
<head></head>
<body>
<applet code="MyApplet.class" width="400" height="200"></applet>
</body>
</html>
Creating a GUI
Applets could easily be used to create a GUI. They act like a Container
and have an add()
method that takes any awt
or swing
component.
public class MyGUIApplet extends JApplet{
private JPanel panel;
private JButton button;
private JComboBox<String> cmbBox;
private JTextField textField;
@Override
public void init(){
panel = new JPanel();
button = new JButton("ClickMe!");
button.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent ae) {
if(((String)cmbBox.getSelectedItem()).equals("greet")) {
JOptionPane.showMessageDialog(null,"Hello " + textField.getText());
} else {
JOptionPane.showMessageDialog(null,textField.getText() + " stinks!");
}
}
});
cmbBox = new JComboBox<>(new String[]{"greet", "offend"});
textField = new JTextField("John Doe");
panel.add(cmbBox);
panel.add(textField);
panel.add(button);
add(panel);
}
}
Open links from within the applet
You can use the method getAppletContext()
to get an AppletContext
object that allows you to request the browser to open a link. For this you use the method showDocument()
. Its second parameter tells the browser to use a new window _blank
or the one that shows the applet _self
.
public class MyLinkApplet extends JApplet{
@Override
public void init(){
JButton button = new JButton("ClickMe!");
button.addActionListener(new ActionListener(){
@Override
public void actionPerformed(ActionEvent ae) {
AppletContext a = getAppletContext();
try {
URL url = new URL("https://stackoverflow.com/");
a.showDocument(url,"_blank");
} catch (Exception e) { /* omitted for brevity */ }
}
});
add(button);
}
}
Loading images, audio and other resources
Java applets are able to load different resources. But since they are running in the web browser of the client you need to make sure that these resources are accessible. Applets are not able to access client resources as the local file system.
If you want to load resources from the same URL the Applet is stored you can use the method getCodeBase()
to retrieve the base URL. To load resources, applets offer the methods getImage()
and getAudioClip()
to load images or audio files.
Load and show an image
public class MyImgApplet extends JApplet{
private Image img;
@Override
public void init(){
try {
img = getImage(new URL("https://cdn.sstatic.net/stackexchange/img/logos/so/so-logo.png"));
} catch (MalformedURLException e) { /* omitted for brevity */ }
}
@Override
public void paint(Graphics g) {
g.drawImage(img, 0, 0, this);
}
}
Load and play an audio file
public class MyAudioApplet extends JApplet{
private AudioClip audioClip;
@Override
public void init(){
try {
audioClip = getAudioClip(new URL("URL/TO/AN/AUDIO/FILE.WAV"));
} catch (MalformedURLException e) { /* omitted for brevity */ }
}
@Override
public void start() {
audioClip.play();
}
@Override
public void stop(){
audioClip.stop();
}
}
Load and display a text file
public class MyTextApplet extends JApplet{
@Override
public void init(){
JTextArea textArea = new JTextArea();
JScrollPane sp = new JScrollPane(textArea);
add(sp);
// load text
try {
URL url = new URL("https://www.textfiles.com/fun/quotes.txt");
InputStream in = url.openStream();
BufferedReader bf = new BufferedReader(new InputStreamReader(in));
String line = "";
while((line = bf.readLine()) != null) {
textArea.append(line + "\n");
}
} catch(Exception e) { /* omitted for brevity */ }
}
}