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:

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.

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.

Top courses in Java

Core Java Made Easy (Covers the latest Java 14)
Bharath Thippireddy
4.5 (1,847)
What's New in Java 9 - Modules and More!
Tim Buchalka's Learn Programming Academy, Frank J. Mitropoulos
4.5 (538)
Java Collections from basics to Advanced
Basics Strong
4.4 (143)
Bestseller
Java Programming Masterclass for Software Developers
Tim Buchalka, Tim Buchalka's Learn Programming Academy, Goran Lochert
4.6 (118,851)
Bestseller
The Complete Android & Java Developer Course - Build 21 Apps
Paulo Dichone | Android, Java, Flutter Developer and Teacher, Fahd Sheraz
4.2 (5,931)

More Java Courses

Java students also learn

Empower your team. Lead the industry.

Get a subscription to a library of online courses and digital learning tools for your organization with Udemy for Business.

Request a demo