Java Lesson 15: Applets

2
72

Introduction

A primary goal of Java is the concept of “Write Once, Run Anywhere” which means that when you write a Java program, you can run it on any computer the Java Virtual Machine (JVM) has been ported to. In this lesson we will learn the basics of developing and executing Java applets.

What is a Java Applet?

A Java applet is Java program code that runs from a browser window, often over the Internet, instead of from the Java Virtual Machine (JVM) command prompt interface.

Using the browser environment requires some special class libraries. In particular we will use the AWT class to control our applet’s appearance in a window.

Differences between Java Programs and Java Applets

Applets typically do not use the console I/O routines such as System.out.println() and the Scanner class like the Java programs we have seen so far (although they are sometimes used for debugging purposes). Instead, Applets use a GUI interface and a mouse.

Applets run from the appletviewer utility program or from a browser (e.g., Chrome, IE, Safari, Firefox).

Applets do not have a main() method.

Applets have import statements such as the following for Java applet library classes and methods:

Applet Life Cycle

Java applets have a life cycle in which program execution proceeds from one step to
the next in a series of methods from start to finish. These methods come from the java.applet.Applet class. We can override these methods with our code. Usually only the paint() method is overridden for simple applets. The methods are:

 Applet Method Purpose of Method
init() Called once when applet begins execution.
start() Called when applet comes into focus.
paint() Called after start(). Could be run more than once.
stop() Called when the applet loses focus, e.g., when minimizing the window.
destroy() Called when applet exits.
Applet lifecycle
Figure 1. Applet Lifecycle

Developing and Running an Applet

There are four steps to develop and execute a Java applet: Write the applet, compile the applet, write an HTML script to run the applet, and run the HTML script.

Write the Applet

Write your Java applet using the text editor just like regular Java programs. Enter or copy-and-paste the following applet code into your text editor and save it with the filename Applet15a.java

The first two statements import the necessary Java class libraries. The next line tells the Java compiler that we are extending the Applet class. Remember, applets do not have a main() method; we extend Applet instead. Then we override the paint() method to draw a text string in the window. The paint() method has one parameter of type Graphics, and this parameter is typically named g. The drawString() method expects three parameters: a string, and the x and y coordinates of where to place the first letter of the string. In our program the “Hello Applet!” string is placed 100 pixels from the left side of the window and 200 pixels from the top of the window.

Compile the Applet

After you have written the applet and saved it to your disk, you are ready to compile the code in the usual way:

which creates the file Applet15a.class in the same folder as the .java file.

Write an HTML Script

The Applet15a.class file needs to be called from an HTML script. Create a text file called applet_run.html with the following code.

Note: We will use this same HTML file to launch (run) all the applets in this tutorial lesson by simply changing the code parameter on line #6.

The most important line is the object element, which instructs the browser to load and run the Java class file in a window that is 300 pixels wide by 300 pixels tall.

If you would like to learn more about HTML, then please see the WikiProgramming HTML Lessons.

Run the HTML Script

There are two ways to run the HTML script:

1. Most operating systems can run the HTML script directly from the command line. The disadvantage of this way is that the window will usually be the size of the full screen instead of the size specified in the HTML file.

2. Use the appletviewer program provided with the JDK. This is the way we will use.

Enter this command to run the HTML script:

apppletviewer creates a window and our applet runs within it. Below are screenshots of compiling and running Applet15a.

Compile and run of Applet15a.java. Use appletviewer to launch the HTML file.

The applet's window is created and the text "Hello Applet!" is displayed in it.

As an exercise, change the x and y coordinates in the Java program so the text string displays in a different position, such as 5 and 5, or 280 and 100. Try to guess where the string will be before you run it. Remember to recompile the program (with the javac command) before running appletviewer, otherwise appletviewer will use the original Applet15a.class file.

Overriding the Applet Methods and the Applet Life Cycle

In our first simple example we only used (overrode) the paint() method. This is quite common for small applets and for testing purposes. You do not need to add the other methods if you do not use them.

The next example, Applet15b.java, implements all the methods just to show the applet life cycle, and the order each method is called. First, enter this code and save it in the file Applet15b.java

Notice the init(), start(), paint(), stop(), and destroy() methods are overridden. To each method we have added a System.out.println() to display the name of each method as they are automatically called by the applet.

The screenshot below shows the steps required to run the applet. First we compile the applet to create a .class file. Second, we edit applet_run.html to change line #6 so it calls Applet15b instead of Applet15a:

And third, we run the applet_run.html script. The output below shows the methods called in the order listed in Figure 1.

Compile and execution of Applet15b.java, which displays the name of each method in the applet lifecycle as they are called.

Now let’s run the applet again and minimize and maximize the window. The life cycle method stop() is called when the window is minimized. Then when the window is maximized start() and paint() are called a second time. Finally, stop() and destroy() are called when the window is closed.

Execution of Applet15b.java, which displays the name of each lifecycle method called, including when the window is minimized and maximized.

Displaying Numeric Types

The drawString() method only displays Strings; it does not automatically handle numeric types like the System.out.println() method does. To display a numeric type (e.g., integer, double) with drawstring(), you must first convert it to a String with the toString() method. Also note you cannot pass primitive types such as int and double. The passed variables must be one of the Wrapper types: Integer, Double, Byte, Long, Short, or Float. For example:

The applet Applet15c.java uses toString(). Remember to change applet_run.html to call Applet15c.class when you run this example.

Compile and execution of Applet15c.java, which converts the number eight into a String type and displays it in an applet window.

Window created by Applet15c.java which converts the number eight into a String and displays it in the applet window.

Drawing Several Strings

As you might guess, it is possible to draw more than one String in an applet’s window. Our next example, Applet15d.java, uses a for loop to repeatedly increment the x and y coordinates of a String’s position.

Window created by Applet15d.java which increments the x and y coordinate values to position a String and displays it in the applet window.

Drawing Shapes and Colors

As it turns out, we can draw several Strings and shapes in one applet window. We can even change colors and draw filled-in shapes. Applet15e.java draws a text String, a line, a filled-in rectangle, and an oval.

Window created by Applet15e.java which draws various shapes of various colors in the applet window.

The Graphics class provides many methods to draw other shapes, colors, and text fonts. See the Oracle Java Graphics webpage for more information.

Summary

In this lesson we will learn the basics of developing and executing Java applets, which are Java programs that runs from a browser window instead of from the command line.

Next Lesson

Another way to develop GUI-based Java programs is with the Java Abstract Window Toolkit (AWT), which is the subject of our next tutorial lesson.

 

2 COMMENTS

  1. […] or to control how the drawing looks on the screen. We briefly touched on some of these in the Applets lesson, and will expand our coverage here. The methods […]

Leave a Reply