×

Applet Program in Java

Applet Program in Java

An applet is a program that can be embedded in a web page. Applets programs are run by a web browser. It mainly works on the client-side. In an HTML page, an applet can be embedded using the applet or object tag. To furnish dynamic content on a website, Java applets are used. The applet program in Java demonstrates how to use applets in the program. A Java applet does not contain the main method. Let’s discuss applet in brief.

Applet’s Life Cycle

Following methods are involved in the life cycle of an applet.

  • init(): The init() method does the initialization of variables. When the applet is loaded, the method is called for doing the initialization work. Since, the applet is loaded only once, therefore, the init() method is also called only once.
  • start(): The start() method is called after the init() method has  done its job. The start() method is responsible for restarting an applet that has been stopped. Every time an applet’s HTML content is rendered on the screen, the start() method is called. For example, when a user moves from an applet’s HTML window to some different window and then comes back to the applet’s HTML window again, the start() method is invoked.
  • paint(): Whenever the output is displayed by the applet, the paint() method is called. For example, when the applet’s window is first minimized, then restored, then the paint() method gets called that redraws the output. The paint() method is also invoked when the applet starts its execution. This is done to render the output on the screen.
  • stop(): When the user moves from the applet’s HTML window to some different window,  the stop() method is called. It suspends the threads when the applet invisible. When the user comes back then those suspended thread starts by invoking the start() method and the applet’s output is again visible.
  • destroy(): For complete removal of applets from memory, the destroy() method is called. Therefore, any resources occupied by the applets should be freed up at this point in time. Thus, before calling the destroy() method, it is necessary to call the stop() method for the suspension of threads.

Java Applet Program

The following program shows how to create various geometrical shapes using applets.

FileName: AppletExample.java

 // importing the class Graphics
import java.awt.Graphics;
// importing the class Applet
import java.applet.Applet; 
public class AppletExample extends Applet
{ 
// overriding the method paint()
@Override 
public void paint(Graphics g)
{ 
    // Current graphics color is set to Yellow
    g.setColor(Color.yellow); 
    // drawing a string Hello World at position (50, 50)
    g.drawString("Hello World", 50, 50); 
    // drawing a rectangle whose width is 38 and height is 32
    // at the position (75, 150)
    g.drawRect(75, 150, 38, 32);
    // drawing a straight line between points (10, 20) and (21, 350)
    g.drawLine(10, 20, 21, 350); 
    // filling the rectangle of width 38 and height 32.
    g.fillRect(75, 150, 38, 32); 
    // Current graphics color is now set to Red 
    g.setColor(Color.red); 
    // drawing an oval shape of width 35 and height 39
    // at the position (73, 150)
    g.drawOval(73, 150, 35, 39);
    // Filling the oval shaped of width 35 and height 39
    g.fillOval(73, 150, 35, 39);
    // drawing an arc of width 39 and height 32
    // at poistion (93, 157) whose starting angle is 32
    // and arc angle is 260
    g.drawArc(93, 157, 39, 30, 32, 260); 
    // filling the arc of width 39 and height 32
    // at poistion (93, 157) whose starting angle is 32
    // and arc angle is 260
    g.fillArc(93, 157, 39, 30, 32, 260); 
} 
}  

For Execution:

To execute the above written applet program in a web browser, an HTML file is required. Inside the HTML file, add the following code snippet.

 <applet code = " AppletExample.class" width = 250 height = 60> </applet>

The width and height supplied in the applet tag that determines the display area. \

Explanation: The public access specifier for the class AppletExample is due to the fact that the outer environment needs to access the above program and any other access specifier reduces the visibility. Whatever needs to be painted is written inside the paint() method. When the AWT(Abstract Window Toolkit) invokes the paint() method, the Graphics parameter is configured, for drawing, with an appropriate state.


Related Topics

Internal Working of ArrayList in Java

Java's version of a resizable array is called ArrayList. The dynamic growth of an array list ensures that there is always room for new elements. ArrayList's backing data structure is...

8 minutes read.

Exception Handling in Java

Before looking at how exceptions are handled in java, it is necessary to see what an exception is. What is Exception? Whenever a program is written, errors are encountered. Some of these...

6 minutes read.

Moran Numbers in Java

In this article, we will be acknowledged about the moran numbers in Java, how they are formed, what are the approaches to achieve the moran numbers. Moran Number A moran numbers are...

3 minutes read.

Classes and Objects in Java Example Programs

Classes and Objects in Java Example Programs Java is an Object-Oriented programming language, i.e., everything in Java is associated with objects and objects are associated with classes. The classes and objects...

5 minutes read.

Java Volatile Keyword

The compiler, runtime, or processors may use any kind of optimization if there aren't any required synchronizations. Although most of the time these improvements are advantageous, they occasionally can result...

6 minutes read.

CRC Program in Java

The acronym CRC stands for Cyclic Redundancy Check. It is invented by W. Wesley Peterson in 1961. It is an error detection technique that detects errors in digital networks (also...

5 minutes read.

Java 9 Interface Private Method

Java 9 provides us the facility to include private methods inside the java interface. In java 8 and earlier versions, we are supposed to use only constant variables and abstract...

3 minutes read.

Java Try-Catch Block

Java Try Block The handling of the exceptions in a block of code is done with the help of java try block. It throws the code that is enclosed in a...

3 minutes read.

What’s new in Java 12

On March 19th, 2019, the Java 12th edition was released. After releasing this edition, they have decided to release every new edition every six months. This version is the advanced...

5 minutes read.

Java Network Programming (Socket Programming in Java)

JAVA NETWORK Network programming is used to execute programs across multiple machines that are connected by a network. The java.net package contains a collection of classes and interfaces that provide this...

3 minutes read.

Java Virtual Machine (JVM)

JVM is a virtual runtime environment to execute Java byte codes. The JVM doesn’t understand the keywords we used to write code. That is why it is converted into bytecode. It controls the...

3 minutes read.

Reverse a String using Collections in Java

Generally, a String is a grouping of characters. Yet, in Java, a String is an item that addresses a succession of characters. The Java.lang.String class is utilized to make a...

5 minutes read.

Different Ways to Take Input from User in Java

Any information provided to a system for use is known as user input. Any responsive software or application must include user input. In Java, there are 4 different ways to...

5 minutes read.

Java Append Data to File

When writing data to a file using the classes within the java.io package, its file will often be overwritten, meaning that any existing data will be removed and new data...

4 minutes read.

Check if the given array is mirror inverse in Java

The primary objective is to check whether the given array is mirror inverse or not. The values and position of the specified array are switched, and a duplicate array is created....

3 minutes read.

Creating a Custom Generic Class in Java

To indicate parameter types when creating generic classes, we utilize the <> symbol. The syntax used to generate objects of a generic class is as follows. // To create an instance...

4 minutes read.

Array and String based questions in Java

1. What is an Array in Java? A collection of identical data types is referred to as an array. There can be no separate data kinds. It supports the storage of...

4 minutes read.

How to Convert boolean to String in Java

How to Convert boolean to String in Java There are two methods to convert boolean to String. Using valueOf(boolean) method Using toString(boolean) method For both the above methods, if the passes Boolean...

2 minutes read.

Count of Range Sum Problem in Java

In this article, we will discuss the basic approach or native approach used to count range sum problem in java. To solve this problem, we will check for numbers if they...

6 minutes read.

String Coding Interview Questions in Java

What is String in Java?In Java, a String is a Class that is defined in the java.lang package. It isn't a basic data type like int or long. Character Strings...

5 minutes read.