Applet in Java Program

Java applets are interactive web applications that can be run on a variety of platforms using browsers. They’re small, portable Java programs that are embedded in HTML pages and run when the pages are viewed.

Applets are used to add interactive features to web applications that HTML alone cannot provide. They can record mouse movements and include controls such as buttons and check boxes. An applet can change the provided graphic content in response to user actions.

An applet is a small application that performs a single task and runs within the scope of a dedicated widget engine or a larger program, usually as a plug-in, in computing.

The applet is capable of generating a graphical user interface. It has restricted access to resources so that it can perform complex calculations without the risk of introducing viruses or compromising data integrity. A class must extend the java.applet.Applet class in order to create an applet. There is no main() method in an Applet class. JVM is used to view it. To run an applet application, the JVM can use either a Web browser plug-in or a separate runtime environment.

How to run applet in java

The javac command is used to compile Java applets. It can be executed in two ways, but not using the java command. It can be run using either the java tool appletviewer or the applet tag in HTML to load the applet into a web browser. Let’s take a closer look at the details below.

Using a Web Browser: A java-enabled web browser is required to view the applet in a web browser. To enable Java in your browser, go to the advanced settings and check the box next to Java. The steps below should be followed:

a) As before, create an HTML file with the APPLET tag.
b) Use javac to compile the applet source code.
c) In the web browser, open the html file.

Using appletviewer: appletviewer is a java tool for viewing applets. It’s similar to a small browser that allows you to see how an applet would appear in a browser. It recognises the APPLET tag and uses it during the creation process. The APPLET tag should be written in the source code file, with comments around it. The steps below should be followed:

a) Create an HTML APPLET tag in the source file’s comments.
b) Use javac to compile the applet source code.
c) View the applet with appletviewer ClassName.class.

Life cycle applet in java

  1. Applet is initialized.
  2. Applet is started
  3. Applet is painted.
  4. Applet is stopped.
  5. Applet is destroyed.

Methods of applet life cycle

initialise(): The initialise() method is used to start an applet. It is only invoked once during the initialization process. The web browser creates initialised objects. This method is comparable to a Thread class born state.

start(): The applet is started using the start() method. It is called after the init() method has been called. It is called every time the browser loads or refreshes. The start() method is inactive until the init() method is called. This method is comparable to the Thread class’s initial state.

stop(): The applet is stopped using the stop() method. It is called whenever the browser is closed or minimised, or when the application crashes unexpectedly. After calling the stop() method, we can call the start() method whenever we want. This method is primarily used to clean up code. This method is comparable to the Thread class’s blocked state.

destroy(): When we are finished with our applet work, we use the destroy() method to destroy the application. It is only used once. We can’t start() the applet once it’s been destroyed. This method is comparable to the Thread class dead state.

paint(): paint() is a method for painting any shape, such as a square, rectangle, trapezium, eclipse, and so on. ClassGraphics is a parameter in the paint() method. In an applet, this Graphics class provides painting features. This method is comparable to the Thread class’s runnable state.

Similar Programs