Udemy logo

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.

Page Last Updated: October 2013

Top courses in Java

Java Tutorial for Beginners
Navin Reddy
4.5 (541)
Java 8 New Features In Simple Way
DURGASOFT DURGA
4.7 (13,808)
Java Interview Help
Bharath Thippireddy
4.6 (1,189)
Java Programming for Complete Beginners
in28Minutes Official
4.5 (38,967)
Oracle Java Certification - Pass the Associate 1Z0-808 Exam.
Tim Buchalka's Learn Programming Academy, Goran Lochert
4.5 (5,389)
Bestseller
Java SE 11 Developer 1Z0-819 OCP Course - Part 1
Tim Buchalka, Tim Buchalka's Learn Programming Academy
4.4 (3,650)
Bestseller
Learn Selenium with Java, Cucumber + Live Project
Pavan Kumar
4.6 (4,293)
Bestseller
Modern Java - Learn Java 8 features by coding it
Pragmatic Code School
4.5 (9,554)

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 Business.

Request a demo