×

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

Constructor Chaining and Constructor Overloading in Java

Constructor Chaining Constructor chaining and constructor overloading are two confusing terms. Let's first understand constructor chaining.Constructor chaining is the process of calling one constructor from another constructor using the same object....

2 minutes read.

Matrix Multiplication in Java

In Java, using the binary operator (*) we can perform matrix multiplication. A Matrix is a group of arrays. In the multiplication of matrices, the elements of each row are...

3 minutes read.

Adapter class in Java

By using the adapter classes, we can implement Listener interfaces. With the help of adapter classes, we can save code as it provides all implementation methods of listener interfaces Advantages of...

3 minutes read.

Java Integer parseUnsignedInt() method

The parseUnsignedInt() method of Java Integer class parses the string argument as an unsigned decimal integer. The second parameter parses the string argument as an unsigned integer in the radix specified...

2 minutes read.

MVC in Java

A well-known design pattern is Model-View-Controller. The discipline of web development. We can organize our code in this manner. The document stipulates that a program or application must include a...

4 minutes read.

Even Odd Program in Java

Even Odd Program in Java The number that are completely divisible by 2 are even and the number that leaves remainder are odd numbers. For example, the numbers 2, 0, 4,...

5 minutes read.

How to check Date Null in Java?

In this section, we will be acknowledged about Date Null in Java. The date null in Java is an entity that is used when there is no specified value for...

3 minutes read.

Java Boolean logicalAnd() Method

The logicalAnd() method of Java Boolean class returns the result of implementing logicalAND operation on the specified Boolean operands. Syntax:public static boolean logicalAnd (boolean a, boolean b) Parameters:The parameters ‘a’ and ‘b’...

2 minutes read.

Three Partition Problems in Java

In talks with leading IT organizations like Google, Amazon, TCS, Accenture, etc., this extremely intriguing subject is constantly brought up. The goal of the problem-solving exercise is to evaluate the...

8 minutes read.

Abstract classes in Java

Abstract classes in Java Java uses the 'abstract’ keyword to make a class abstract. Such classes are known as abstract classes, and the process is known as abstraction. In programming language, abstract...

4 minutes read.

How to Convert String to float in Java

How to Convert String to Float in java It is used if you want to perform mathematical operations on the string that contains float number. You can convert String to float...

3 minutes read.

String Programs in Java

String Programs in Java: In Java, a String is an immutable object that represents a sequence of characters. For example, “Tutorial” is a string that consists of 8 characters: ‘T’,...

10 minutes read.

Minimum Lights to Activate Java Snippet Class

Minimum Lights to Activate Problem in Java In prison, there is a hallway that is N units long. Given an N-dimensional array A. If the light at the ith position is...

3 minutes read.

Java Generate UUID

What java Generate UUID? A 128-bit long value that is universally unique serves as the basis for the terms UUID (Universally Unique Identifier) and GUID (Globally Unique Identifier). Hexadecimal (octet) digits...

5 minutes read.

Buffer reader to read string in Java

The Buffered Reader class of Java is used to read the stream of characters from the input stream. Program to read string using Buffer reader import java.io.*; class  Demo {   public static void main(String...

3 minutes read.

Segment Tree in Java

Binary trees can address a variety of issues; however, the Segment Tree is more efficient in terms of time complexity. The segment tree in Java is represented using an array. Native...

4 minutes read.

What is Core Java?

The fundamental Java, which includes the fundamental idea of the Java programming language, is referred to as "Core Java." The definition of "Core" is the core idea of something. Core...

3 minutes read.

Snake and Ladder Problem in Java

Find the smallest number of dice throws necessary to reach the destination or last cell from the source or first cell on a snake and ladder board. Essentially, the player...

6 minutes read.

Java extend multiple classes

In Java, what does extend mean? One of several Java inheritance keywords is extended, meaning we pass all or most of the Parent class's characteristics through to the Child class. The...

3 minutes read.

JDK | Java Development Kit

Java Development Kit (JDK) The Java Development Kit is a software development environment used to create Java applications. The JDK includes JRE (Java Runtime Environment), an interpreter (java), a compiler (javac),...

3 minutes read.