Applets are small Java programs that are used in internet computing. They can be transported over the internet from one computer to another and run using the Applet Viewer or any Web Browser that supports java. Applet is a special type of program that is embedded in the webpage to generate the dynamic content. An applet, like any application program, can do many things for us. It can perform arithmetic operations, display graphics, play sounds, accept user input, create animation, and play interactive games.
Applets are Java programs that can be embedded in a Web page. The Applet that is embedded within the web page makes the Web page active or interactive or dynamic. A Java applet when run, can produce graphics, sounds and moving images. Java applets therefore have begun to make a significant impact on the World wide web.
We can embed applets into web pages in two ways. One, we can write our own applets and embed them into Web pages. Second, we can download an applet from a remote computer system and then embed it into a web page.
An applet developed locally and stored in a local system is known as a local applet. When a Web page is trying to find a local applet, it does not need to use the Internet and therefore the local system does not require the Internet connection. It simply searches the directories in the local system and locates and loads the specified applet.
A remote applet is that which is developed by someone else and stored on a remote computer. If our system is connected to the internet, we can download the remote applet onto our system. In order to locate and load a remote applet, we must know the applet’s address on the web.
Applets were a popular way to add dynamic content to web pages in the early days of the internet. However, with the evolution of web technologies and security concerns, the use of Java applets has declined, and modern web development tends to favour other technologies like HTML5, JavaScript, and CSS for creating interactive web applications. Due to security concerns, modern browsers may not support Java applets, and we may need to use alternative technologies for web development.
Difference between Applets and Applications:
Both the applets and the applications are Java programs but there is a significant difference between them. Applets are not full-featured application programs. They are usually written to accomplish a small task or a component of a task. Since they are usually designed for use on the internet, they impose certain limitations and restrictions in their design.
Applets are designed for web-based environments with restricted access to resources, while applications are standalone programs that can be executed on a user’s machine with broader access to system resources.
Java applications and Java applets are both types of Java programs, but they are designed for different environments and have distinct characteristics. Here are the main differences between Java applets and applications:
- The execution of the Java application begins with the main() method. On the other hand, the applet initializes through the init() method. It does not use the main() method.
- Unlike stand-alone applications, applets cannot be run independently. Applets are designed to be run within a web browser
- Applets cannot read from or write to the files in the local computer.
- Applets cannot communicate with other servers on the network.
- Applets cannot run any program from the local computer.
- Applets are restricted from using libraries from other languages such as C or C++
Applet Architecture:
We know that java provides console-based programming language environment and window-based programming environment. An applet is a window-based programming environment. So, applet architecture is different than console-based program.
An applet is a GUI-based program. As such, its architecture is different from the console-based programs. Java applets are essentially java window programs that can run within a web page.
The applet architecture in Java includes the following key components:
Applet Class: Applets are Java classes that extend the java.applet.Applet
class. The Applet
class provides the basic infrastructure for running an applet, including methods for initialization, starting, stopping, and destroying the applet. Common methods include init()
, start()
, stop()
, and destroy()
.
init(): This method is called when an applet is first loaded. It is used for initializing the applet and setting up any necessary resources.
start(): Called each time the applet is started or resumed. Invoked after the init() method. The start() method is called whenever the user revisits a page containing the applet. It is used to start any threads or animations that the applet may have.
stop(): Called when the user leaves the page containing the applet. It is used to stop or suspend any ongoing activities, such as animations.
destroy(): This method is called when the applet is no longer needed. It allows the applet to release resources and clean up.
paint(): Called whenever the applet needs to be redrawn.
import java.applet.Applet;
import java.awt.Graphics;
public class MyFirstApplet extends Applet {
public void init() {
// Initialization code
}
public void start() {
// Code to start the applet
}
public void paint(Graphics g) {
// Code to paint on the applet
}
public void stop() {
// Code to stop the applet
}
public void destroy() {
// Cleanup code before the applet is destroyed
}
}
HTML Document:
To embed an applet in an HTML page, we use the <applet>
tag. The <applet>
tag includes attributes like code
, width
, height etc.,
specifying the name of the applet class and applet’s width, height.
<applet code=”MyApplet.class” width=”300″ height=”200″> </applet>
Applet Viewer or Web Browser:
The applet is executed either in the Java Applet Viewer or within a web browser that supports Java applets. The Java Applet Viewer allows you to run and test applets without the need for a web browser.
Simple Applet Display Methods:
Applet display methods are the methods that are used to display the applet’s content in a web browser or an applet viewer. Applets are displayed in a window and they use the AWT to perform input and output functions. AWT stands for the Abstract Window Toolkit. Since all applets run in a window, it is necessary to include support for that window by importing java.awt package.
To output a string to an applet, use drawString( ), which is a member of the Graphics class. Typically, it is called from within either update() or paint( ). It has the following general form:
void drawString(String message, int x, int y)
Here, messageis the string to be output beginning at x, y. In a Java window, the upper-left corner is location 0, 0.
To set the background color of an applet’s window, we use setBackground( ). To set the foreground color, we use setForeground(). These methods are defined by Component, and have the general forms:
void setBackground(Color newColor)
void setForeground(Color newColor)
Here, newColor specifies the new color. The class Color defines the following values that can be used to specify colors:
Color.black Color.magenta Color.blue Color.orange Color.cyan Color.pink Color.darkGray Color. red Color.gray Color.white Color. green Color.yellow Color.lightGray
For example, this sets the background color to blue and the text color to yellow:
setBackground(Color.blue);
setForeground(Color.yellow);
Passing Parameters to Applets:
We can pass parameters to an applet using the <param>
tag. Parameters specify extra information that can be passed to an applet from the HTML page.
The <param> tag contains two attributes: name and value which are used to specify the name of the parameter and the value of the parameter respectively. For example, the param tags for passing name and age parameters as shown below:
<param name= “name” value= “Aakash” />
<param name= “age” value= “18″ />
To retrieve a parameter, use the getParameter( ) method, defined by Applet. Its general syntax is shown below:
String getParameter(String paramName)
Here, paramName is the name of the parameter.
The <PARAM>tags must be included between the <APPLET> and</ APPLET> tags. The init () method in the applet retrieves user-defined values of the parameters defined in the <PARAM>tags by using the get Parameter() method. This method accepts one string argument that holds the name of the parameter and returns a string which contains the value of that parameter. Since it returns String object, any data type other than String must be converted into its corresponding data type before it can be used.
Your blog always puts a smile on my face and makes me feel better about the world Thank you for being a source of light and positivity
Thank you so much…