Week 10
Week 10
An applet is a Java program that runs in a Web browser. An applet can be a fully functional Java
application because it has the entire Java API at its disposal.
Applets
▪ In general, an applet is a small program or utility with limited features, requiring minimal
resources, and usually designed to run within a larger program.
▪ A Java applet is such a program written in the Java programming language and typically
designed to run from a web browser.
o Write the applet in Java, and save it with a .java file extension, just as when you write a
Java application.
o Compile the applet into bytecode using the javac command, just as when you write a
Java application.
o Write an HTML document that includes a statement to call your compiled Java class.
o Load the HTML document into a Web browser (such as Mozilla Firefox or Microsoft
Internet Explorer), or open the HTML document using the Applet Viewer.
▪ The tag that begins every HTML document is <html>, which is surrounded by angle brackets.
▪ The html within the tag is an HTML keyword that specifies that an HTML document follows the
keyword.
▪ Generally, HTML documents contain other tags with the texts. Of importance at this point is the
<object>-</object> tag pair used to run an applet from within an HTML document.
▪ Three attributes (or arguments) are placed within the <object> tag: code, width, and height
<object code = ″AClass.class″
width = 300
height = 200>
</object>
Three attributes of the given object tag:
o code = followed by the name of the compiled applet you are calling
o width = followed by the width of the applet on the screen
o height = followed by the height of the applet on the screen
NOTE: Instead of the <object> and </object> tag pair, you can use the tag set <applet> and
</applet> in your HTML applet host documents.
Example:
Example:
<html>
<applet
code = ″HelloApplet.class″
width = 400
height = 200
>
</applet>
</html>
Running an Applet
Two ways of running an applet:
o by opening the associated HTML file on a Webbrowser
o the appletviewer
NOTE: The appletviewer is used mainly for testing applets. It is, in effect, a web browser that
recognizes only the tags associated with the execution of an applet.
▪ Creators of Java created an applet class named JApplet that you can import using the
statement,
import javax.swing.JApplet;
JApplet Class
▪ JApplet is a simple extension of java.applet.Applet for use when creating Swing programs
designed to be used in a Web browser.
▪ As a direct subclass of Applet, JApplet is used in much the same way as the Applet.
▪ When using the JApplet class, you need the following import statements:
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
▪ The extends keyword indicates that your applet builds on, or inherits, the traits of the JApplet
class.
A simple applet
import javax.swing.*;
import java.awt.*;
public class HelloApplet extends JApplet {
public void init() {
Container con = getContentPane();
cont.setLayout(new FlowLayout());
JLabel label = new JLabel("Hello world!");
con.add(label);
}
}
Creating an Applet with aninit() Method
▪ The Applet class provides four methods that are invoked by a Web browser when the browser
runs an applet.
o public void init()
o public void start()
o public void stop()
o public void destroy()
▪ If you fail to write one or more these methods, Java creates them for you.