What is an Applet in Java with Examples - onlyxcodes

Saturday 18 March 2023

What is an Applet in Java with Examples

A short Java program called an applet is mostly used for online computing. The Applet Viewer or any web browser that supports Java can be used to transfer it from one machine to another over the internet.


Like application software, an applet can do a variety of tasks for us. It can carry out mathematical processes, show visuals, produce animation and games, play sounds, and take human input, among other things.


In this post, we'll explain what Java applets are, their benefits and drawbacks, how they operate, and their life cycle, and give a few examples.


what is an applet in java with examples

Table Content

1. What is Java Applet?

2. How do Java Applets Work?

3. Advantages of Java Applets

4. Disadvantages of Java Applets

5. Uses of Applet In Java

6. Example of Java Applet

7. How to Run Java Applet?

8. Applet Life Cycle or Skeleton In Java

9. Types of Applet In Java

10. Difference Between Local Applet and Remote Applet

11. Difference Between Java Applet and Java Application


1. What is Java Applet?

A Java applet is a little application created in the Java programming language and optimized for web browser use. Java applets are loaded and run inside a web browser, as opposed to conventional software programs that are downloaded and installed on a computer. The appletviewer command and HTML file both run Java applets.


Note: Some browsers required plugins to execute the applet program.


2. How do Java Applets Work?

The Java programming language is used to create Java applets, which are then compiled into bytecode. The bytecode is subsequently put into a file with the .class extension. After then, an HTML page containing this file is given to a web browser.


The web browser asks the server to download the .class file containing the bytecode when it loads the HTML page with the Java applet. The JVM that is incorporated in the web browser loads and runs the bytecode after the .class file has been downloaded. The Java applet then functions like any other program inside the web browser.


3. Advantages of Java Applets

Platform independent: Platform independence means that Java applets can run on any computer that has a web browser with a JVM installed.


Dynamic: Java applets can be updated or modified without the user having to download and install a new version because they are dynamically generated.


Interactive: Java applets can respond to user input and offer a rich user experience since they are interactive.


4. Disadvantages of Java Applets

Data cannot be streamed directly into applets from the browser.


Events that are triggered outside of the applet zone cannot be subscribed to by applets. Applets, for instance, are unable to tell whether a user followed a bookmark. Similarly, applets are unable to recognize when a user types in a URL that needs to be.


5. Uses of Applet In Java 

Java applets may be run by many different systems' browsers and are used to add interactive capabilities to online applications.


These are tiny, portable Java programs that can execute instantly when HTML pages are accessed and are embedded in HTML pages. 


6. Example of Java Applet

Here is an example of a straightforward Java applet that, when loaded, shows a message:


 import java.applet.*;
import java.awt.*;

public class Myapplet extends Applet 
{
   public void paint(Graphics g) 
   {
      g.drawString("United Kingdom", 50, 20);
   }
}

//<APPLET code="Myapplet.class" width="200" height="150"></APPLET>

Compile javac Myapplet.java


Run: appletviewer Myapplet.java


Output:


The web browser's window is created by this applet, which shows the word "United Kingdom" in it.


the web browser's window is created by this applet, which shows the word united kingdom in it.

Explanation:


java.awt and java.applet are two packages that every Applet program must import.


The AWT classes are imported by the java.awt.* package. Through the AWT, applets communicate (directly or indirectly) with the user.


Support for a window-based, graphical user interface is provided by the AWT. The class Applet is found in the applet package, which is imported by java.applet.*.


The Applet class must be a subclass of every applet you make.


The program's class needs to be declared as public since code from outside the program will be able to access it.


A paint() method must be declared in every Applet application. The AWT class defines this method, which the applet must override.


Every time an applet has to redisplay its output, the paint() method is invoked.


The operation of an applet does not start at the main() method, which is another crucial point to keep in mind about applet programs. An applet application does not have a main() method.


7. How to run Java Applet?

In Java, there are two methods for running an applet.


1) Using HTML File.


2) Using appletViewer command.


1) Using HTML File.


The same process you used to compile your programs at the command line is used to compile an applet program.


Running the applet on a web browser that supports Java.


Make a quick HTML file called Myapplet.html in the same directory so that it can be used to run an Applet in a web browser. Include the following code inside the file's body tag.


The Applet class is loaded by the applet tag.


<APPLET code="Myapplet.class" width="200" height="150"></APPLET>

Myapplet.java


import java.applet.*;
import java.awt.*;

public class Myapplet extends Applet 
{
   public void paint(Graphics g) 
   {
      g.drawString("United Kingdom", 50, 20);
   }
}

Myapplet.html


<html>
	<head>
		<title>welcome</title>
	</head>
	<body>
		<APPLET code="Myapplet.class" width="200" height="150"></APPLET>
	</body>
</html>

2) Using appletViewer command.


Create an applet with the applet tag in the comment and compile it to run the applet using the appletviewer tool.


Run Myapplet.java by appletviewer after that. No longer is an HTML file necessary.


Myapplet.java


import java.applet.*;
import java.awt.*;

public class Myapplet extends Applet 
{
   public void paint(Graphics g) 
   {
      g.drawString("United Kingdom", 50, 20);
   }
}

//<APPLET code="Myapplet.class" width="200" height="150"></APPLET>

Compile: javac Myapplet.java


Run: appletviewer Myapplet.java


Output:


appletviewer command shows simple message in a window

8. Applet Life Cycle or Skeleton In Java

Four methods make up the Java applet life cycle, and they are called in the following order:


1. init()


When an applet is first loaded into memory, the init() method is invoked. The applet is initialized and any setup procedures are carried out using this method. For the duration of an applet's life, it is only ever called once.


2. start()


The start() method is used to launch the applet and is invoked after the init() method. Every time a user accesses the website staging the applet, this procedure is invoked.


3. stop()


When a user navigates away from the web page where the applet is located, the stop() method is invoked. The applet is terminated using this method, freeing up any resources it might be utilizing.


4. destroy()


When the applet is no longer required and is going to be deleted from memory, the destroy() method is called. This method is used to free up whatever resources the applet might have been utilizing.


The last method is called paint(). 


5. paint(Graphics g)


One mechanism for the applet's life cycle is provided by the java.awt.component class.


The Applet is painted using the paint() method. It offers objects of the Graphics class that can be used to draw shapes like ovals, rectangles, arcs, and more.


Applet Life Cycle Flow


The flow of the applet life cycle can be summed up as follows:


When the applet is first loaded into memory, the init() method is called.


When the applet is launched, the start() method is called.


When a user navigates away from the web page where the applet is located, the stop() method is invoked.


When the applet is no longer required and is going to be deleted from memory, the destroy() method is called.


Example of an Applet Skeleton:


import java.awt.*; 
import java.applet.*; 

public class Testapplet extends Applet 
{ 
	public void init() 
	{ 
		//initialization 
	} 
	public void start () 
	{ 
		//start or resume execution 
	} 
	public void stop() 
	{ 
		//suspend execution 
	} 
	public void destroy() 
	{ 
		//perform shutdown activity 
	} 
	public void paint (Graphics g) 
	{ 
		//display the content of window 
	} 
}

Example:


import java.applet.*; 
import java.awt.*; 

public class Testapplet extends Applet 
{ 
	int height, width; 
	
	public void init() 
	{ 
		height = getSize().height; 
		width = getSize().width; 
		
		setName("TestApplet"); 
	} 
	public void paint(Graphics g) 
	{  
		g.drawOval(20,20,200,120);
	} 
}

//<APPLET code="Testapplet.class" width="250" height="250"></APPLET>

Compile: javac Testapplet.java


Run: appletviewer Testapplet.java


Output:


draw circle by applet

9. Types of Applet In Java 

Java Applets can be divided into two categories:


1. Local Applet


2. Remote Applet


1. Local Applet


A Local Applet is an applet that has been created locally and is kept on a local system. A web page doesn't need to utilize the Internet while it's looking for a local applet, and the local system doesn't need an Internet connection either.



2. Remote Applet


An applet hosted on a remote computer linked to the Internet and created by a third party is referred to as a remote applet. We can download the remote applet onto our system via the Internet if our system is linked to the Internet.



10. Difference Between Local Applet and Remote Applet

1.  We have a local applet on our computer.


We don't keep Remote Applet on our machine.


2.  Accessing the applet does not require an internet connection.


To access the applet, you need an internet connection.


3.  It is not necessary to know the applet's URL for a local applet.


It requires the applet's URL for a remote applet.


11. Difference Between Java Applet and Java Application

The Java programming language may be used to create two different kinds of applications: Java Applets and Java Applications. The key variations between them are as follows:


Deployment:


Web browsers are intended to run Java Applets. They are normally carried out when a user accesses an HTML page and are typically incorporated into HTML pages. 


On the other hand, Java Apps are standalone programs that are downloaded and run on a user's machine.


Security:


The Java sandbox, a constrained environment in which Java Applets operate, places restrictions on their access to the user's system. 


Depending on their permissions, Java Applications can access the user's system resources more easily.


User Interface:


Button, text field, and menu elements from the browser's user interface are accessible to Java Applets.


On the other hand, Java applications have complete control over their user interface and can add customized components as required.


Performance:


Because of the expense associated with operating inside a web browser, Java Applets may have performance problems.


On the other hand, Java Apps operate locally on the user's machine and can fully utilize the resources.


Other:


Java Applications are standalone programs that can access the user's system more freely than Java Applets, which are intended to be run inside a web browser and have limited access to the user's system.

No comments:

Post a Comment

Post Bottom Ad