Core java tutorial for beginners
A tutorial blog which explains different core concepts related to Java along with programming examples

February 24, 2016 Categories: Applets. No Comments on Applets Overview

In this article we will learn what are applets in Java, what are the differences between a Java program and an applet and how to create applets.


Definition of Applet

An applet is a small Java program that is a part of a web page and is downloaded automatically from a server when the client requests that web page.


Applet Fundamentals

Since applet is a part of a webpage, we will learn basic things related to web pages. A web page is a collection of information (ex: Google home page, Yahoo home page etc). Web pages are generally created using HTML.


A web page contains at least four basic HTML tags:

  1. <html> : Specifies that it is the root tag.
  2. <head> : Specifies the head section of the web page. It contains meta information, page title, scripts, external style sheet links etc.
  3. <body> : Contains the content that is displayed to a user who is visiting the web page.
  4. <title> : Specifies the title of the web page. It is specified inside <head> tags.


As an example let’s look at the HTML code of a web page that displays “Hello World” in large letters. The code is as follows:



The file will be saved with .html extension. When you open the file with a web browser, the output will be as follows:



An applet can be displayed in a web page using the <applet> tag as shown below:

<applet code=”ClassName” height=”200″ width=”400″></applet>


An applet doesn’t contain a main() method like the Java console programs. An applet is used to display graphics or to display a GUI (ex: login form, registration form) to the users.


Creating an Applet

An applet can be created by extending either Applet class or JApplet class. First let’s see how to create an applet using the Applet class. The Applet class is available in java.applet package. An applet which displays “Hello World” is shown below:


Remember to include the <applet> tag in comments. This is useful for running the applet. The value of code attribute must match with the class name.


In the above applet program, the class MyApplet extends the Applet class and it contains a method named paint() which accepts a parameter of the type Graphics. The Graphics class belongs to java.awt package and is used to display text or graphics on our applet.


The paint() method is used to re-display the output when the applet is resized or minimized or maximized. The drawString() method belongs to Graphics class and it displays “Hello World!” on the applet display area at the point whose coordinates are (20, 20).


Save the file as and compile it to generate MyApplet.class file.


Running an Applet

An applet can be executed in two ways:

  1. Using appletviewer command-line tool or
  2. Using a browser


Using appletviewer tool:

Java provides a command line tool named appletviewer for quick debugging of applets. Use the following syntax for running an applet using appletviewer:

appletviewer filename


As the file name of our applet is, the command for executing the applet is:



Output of the above command will be as shown below:



Using a browser:

Create a web page named hello.html with the following HTML code:


Unfortunately from Java 7 onwards, local applets (unsigned) cannot be viewed using a browser and Google Chrome browser no longer supports applets in web pages.


It is recommended to use appletviewer tool to debug and execute applets.


Differences between a Java Application and Applets



Related Links:

Note: Do you have a question on this article or have a suggestion to make this article better? You can ask or suggest us by filling in the below form. After commenting, your comment will be held for moderation and will be published in 24-48 hrs.

Leave a Reply

Your email address will not be published. Required fields are marked *

Scroll Up