Java Applet Tutorial: Learning the Basics

javaapplettutorialJava is relied on heavily in web-based applications for everything from creating games to large-scale enterprise programs. Java programs can be created as standalone applications run locally on a user’s computer or via a Web browser using applets. An applet is basically a Java program that runs within a web browser. It still has full use of the Java API and is still considered a fully functional Java application as a result.

If you have no Java experience, consider checking out Java for Absolute Beginners to learn basic concepts before jumping into creating your own Java applets. Once you have the basics down, learning to create your own applets adds a whole new level of interactivity to existing web pages and you may find yourself in high demand as an experienced Java applet programmer.

What Makes a Java Applet Different from a Java Application?

Since you now know that applets can use the entire Java API during runtime, what actually makes an applet an applet? The most important differences between the two include:

  • An applet is a Java class that extends the java.applet.Applet class.

  • Unlike traditional Java applications, applets do not invoke a main() method.

  • Applets are designed to be embedded within an HTML webpage.

  • The applet code is automatically downloaded to the user’s machine when they view an HTML webpage with an embedded applet.

  • A Java Virtual Machine (JVM) is required to view an applet. In some cases, the JVM is a browser plug-in, but it can also be a separate runtime environment installed locally on the user’s computer.

  • The JVM creates an instance of the applet class and invokes methods defined within the applet during the applet’s lifetime.

  • The security rules for applets are enforced by the web browser and tend to be much stricter than the security rules that apply to standalone Java applications. It is often referred to as a sandbox environment. This is done to prevent malicious code from executing on a user’s computer at runtime.

  • If an applet requires a custom class, it can be downloaded in a single Java Archive (JAR) file. This means the possibilities are endless because you can create custom classes as your skill level progresses.

The Life Cycle of an Applet

There are five primary methods in the Applet class which provide the framework for just about every applet you create.

  • init – This method is used to initialize any components of your applet before the program begins to run. The init method is called after the param tags have been processed by the JVM.

  • start – The start method is automatically called once the browser calls the init method. This method is also called automatically whenever a user returns to the page containing the applet.

  • stop – Stop is automatically called by the browser when the user moves off the page where the applet is embedded. It can be called multiple times within the same applet.

  • destroy – Once the browser shuts down, the destroy method removes any resources that would otherwise be left behind by the applet.

  • paint – Called immediately after start, the paint method is what displays your applet on a webpage. Paint is also called any time the applet needs to repaint itself.

Hello World Applet

As it is customary to do when you learn any new programming language, the first program you should create is the “Hello World” program. This is extremely easy to do in applet form as you can see from this code:

import java.applet.*;

import java.awt.*;

 

public class HelloWorldApplet extends Applet

{

        public void paint (Graphics g)

        {

                    g.drawstring (“Hello World”, 25, 50);

        }

}

 

Taking a closer look at this example, hopefully you notice a few things. If you have experience with Java, you are familiar with importing classes needed for the program. In the case of this applet, you need to import the applet class and awt (the class where the paint method is located). Java Fundamentals I & II explains the concept of importing various class libraries into your applications.

Also notice that in the import statements there is a “*” symbol. This is used as a wildcard to import the entire class library. For this simple Hello World example, you could just as easily type import java.awt.graphics and prevent the applet from loading the entire awt class.

Invoking an Applet

Since your applet is designed to be run in an HTML webpage, you need to learn how to invoke the applet using HTML. If you are not familiar with using HTML, HTML Made Easy is a good place to start.

The code to invoke your Hello World applet in a web browser looks something like this:

<html>

<title>The Hello World Applet</title>

<hr>

<applet code=”HelloWorldApplet.class” width=”320” height=”120”>

Optional message goes here.

</applet>

<hr>

</html>

Notice that there is room after the code that calls the applet to include a text based message to viewers. It’s usually a good idea to include something that explains what the applet is supposed to do. Java has taken a lot of heat lately for being less secure than it should be and many people are hesitant to allow applets to run on a webpage they are viewing. By describing the applet in text form, many users will be more likely to enable Java to view your page as you intended.

Of course, this is only the beginning of learning to become a proficient Java applet developer.  There is much more information about applets available in the Advanced Java Programming course.

Despite security concerns over using Java on web pages, it remains a very popular platform for interactive web design and you will have no shortage of work as an experienced applet developer.

If you have already developed your own standalone Java applications, you can easily convert these to applets and embed them into web pages. This allows a much broader audience to experience your application thanks to the power of Java applets and HTML web browsers.