×

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

Method Overriding in Java

Overriding  Overriding is also known as run time polymorphism, and it is about the same method with the same signatures in different classes. Overriding can be applied only methods. Method Overriding Method Overriding...

8 minutes read.

How to Create an Immutable Class in Java

How to Create an Immutable Class in Java In Java, immutable means something that cannot be change. A class is called an immutable class if its content cannot be changed, once...

3 minutes read.

Java Buffer Reader

When a variable cannot change its value during run time is called a Static way of programming. In this programming, a variable is directly assigned to a value. Program: Import java.io.*; class  A {  ...

4 minutes read.

How to Convert char to String in Java

How to Convert char to String in Java There are two methods to convert char to String: Using String.valueOf(char) method Using Charcter.toString(char) method Using String.valueOf(char) method valueOf(char) is the static method of String class that...

2 minutes read.

Coin change problem in dynamic programming

In this tutorial, we will understand a popular problem called the coin changeproblem through dynamic programming. This problem checks the logical and critical thinking ability of the person. Dynamic Programming...

5 minutes read.

Sliding Window Problem in Java

A sliding window is used in computer science and data science to process large datasets. It involves breaking the dataset into smaller chunks or windows and then processing it in...

6 minutes read.

Java Garbage Collection Interview Questions

One of the key areas of Java is garbage collection. Garbage collection enables apps to manage memory automatically. Interviewers frequently ask inquiries about garbage collection. Q1: What is the purpose of...

6 minutes read.

ConcurrentSkipListSet in Java

ConcurrentSkipListSet is a class that is a part of collection framework in Java.It has been available since Java version 1.6. ConcurrentSkipListSet  is a scalable concurrent NavigableSet implementation based on a ConcurrentSkipListMap.The...

16 minutes read.

Bully Algorithm code in Java

Election algorithms include the bully algorithm, mainly used to select a coordinate. To find a coordinator in a distributed system that can carry out the tasks required by other processes,...

4 minutes read.

Java Beans

It is a Java class, It follows conventions they are: It must have a no-arg constructorIt must be serializable.It must provide the methods to get and set the properties Uses Of Java...

3 minutes read.

StringBuilder in Java

StringBuilder in Java Java StringBuilder class is introduced since JDK 1.5. The StringBuilder class is mainly used to create modifiable or mutable strings. Note that the StringBuilder class is not synchronized....

7 minutes read.

MessageDigest in Java

The compression of mathematical numbers is accomplished using hash functions. In almost every data security application, hash functions are employed. The hashing, often called has values, returns a value called...

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

Compare Two Times in Java

Definition The compareTo() function of the LocalTime class is used to compare times. The class's compareTo() method compares two LocalTime objects. Here we have two objects that have to be compared: the...

4 minutes read.

Java Thread Creation

Java provides the two ways to create a Thread: Implementing the Runnable interface.Extending the Thread class. Implementing Runnable interface The easiest way of creating a thread is to make a class that implements...

5 minutes read.

Generics vs Wildcard in Java

In generic programming, the question mark (?) is often referred to as the wildcard. It stands for a mysterious type. The wildcard can be used for many different contexts, such as...

4 minutes read.

Best Java IDE

Applications for desktop, workplace, smartphone, and the internet can be created using Java, one of the most popular programming languages. Java will undoubtedly be a popular programming language for so...

5 minutes read.

Swastika Pattern in Java

This part teaches us how to create the Swastika Pattern in Java utilizing user-defined columns and rows as well as stars or other special characters.A Java pattern application that is...

2 minutes read.

Thread Scheduler in java

Scheduling: it is defined as the execution of multiple threads on a single CPU in some order is called scheduling. Preemptive-priority scheduling: This algorithm schedules threads based on their priority relative to other...

11 minutes read.

Resultset in java

Resultset: A result set is an interface that is present in the package java.sql and the resultset is used to store the data that are returned from the database table after...

5 minutes read.