Copyright Derek O'Reilly, Dundalk Institute of Technology (DkIT), Dundalk, Co. Louth, Ireland.
A Java program can be compiled to be either an:
In these notes, we shall concern ourselves primarily with applets.
Applet GUI can be implemented using the Abstract Windowing Toolkit (AWT) or Swing. Swing is newer and provides enhanced functionality compared to the AWT. Swing sits on top of AWT and Java2D. The various AWT GUI components are inherited by Swing. All of the Swing classes have a "J" placed in front of them to distinguish them from their AWT super-class. Swing applets must be implemented as a subclass of JApplet.
For a standard GUI based applet, there is little difference between AWT and Swing. We shall develop our programs in Swing using JApplets. As JApplets and AWT applets and almost identical, the term "applet" is commonly used to refer to JApplet. This is the convention that will be used in these notes.
Example JApplet (Run Example Code)
import java.awt.*; import javax.swing.*; public class DemoJApplet extends JApplet { @Override public void init() { this.setContentPane(new View()); } public class View extends JPanel { @Override protected void paintComponent(Graphics g) { super.paintComponent(g); g.drawString("JApplet demo", 100, 50); } } }
Even if Swing JApplets is being used to provide the GUI functionality, it is quite common for to also use some of the AWT classes in the same applet. For example, in the example above, the Graphics class belongs to the AWT package, so the AWT package must be imported into the programme.
The differences between JApplets and AWT applets include:
We shall deal with these issues as they arise.
Applets differ from stand-alone programs as follows:
There can only be one occurrence of init() in any applet. After the class's constructor, init() is the first code to be called when an applet is loaded into a browser. A class's constructor does not have access to the GUI. Therefore, init() is the first place where GUI methods can be called. The init() method is called exactly once for any applet. It is not called every time the browser returns to the HTML webpage in which the applet resides.
There can only be one occurrence of start() in any applet. The start() method is called after the init() method when an applet is loaded into a browser. In addition, start() is called every time a browser returns to the HTML webpage in which the applet resides.
An example where the start() method can be used is for starting an audio clip every time a user returns to a webpage.
To get access to the JApplet class an applet must import (include) the JPpplet package (library).
As every applet is a subclass of the JApplet class, every applet must extend the JApplet class:
For example:
import java.awt.*; import javax.swing.*; public class DemoJApplet extends JApplet { @Override public void init() { this.setContentPane(new View()); } public class View extends JPanel { @Override protected void paintComponent(Graphics g) { super.paintComponent(g); g.drawString("JApplet demo", 100, 50); } } }
The HTML wrapper code for an applet is:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Demo</title> </head> <body> <applet code = DemoJApplet.class name = Demo width = 640 height = 480> </applet> </body> </html>
Passing Parameters from HTML files
Using the HTML param tag and the Java getParameter() method we can read in applet parameters from a HTML file. The getParameter() method has the syntax:
String getParameter(String parameterName);
If the parameter does not exist in the HTML file, then NULL is returned.
For example:
import javax.swing.JApplet; public class DemoJApplet extends JApplet { private String imageName; private int scaleX; private int scaleY; @Override public void init() { this.imageName = this.getParameter("imageName"); this.scaleX = Integer.getInteger(this.getParameter("scaleX")); this.scaleY = Integer.getInteger(this.getParameter("scale Y")); } }
The HTML wrapper code for an applet with parameters is:
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <meta name="viewport" content="width=device-width, initial-scale=1.0"> <title>Demo</title> </head> <body> <applet code = DemoJApplet.class name = Demo width = 640 height = 480 param name = "imageName" value = "myBitmap.gif" param name = "scaleX" value = "2" param name = "scaleY" value = "1" </applet> </body> </html>
Copyright Derek O' Reilly, Dundalk Institute of Technology (DkIT), Dundalk, Co. Louth, Ireland.