what is applet in java
An applet in Java is a small Java program that is embedded inside a web page and runs inside a browser using the Java Virtual Machine (JVM).
What is an applet in Java?
An applet is a mini-application written in Java that is downloaded from a web server and executed on the client side inside a browser or applet viewer. It was historically used to make web pages interactive with graphics, animations, small games, calculators, and teaching demos beyond what plain HTML could do.
Key points:
- Applets are Java programs that extend the
java.applet.Applet(or laterJApplet) class.
- They are embedded into HTML pages using
<applet>or<object>tags with attributes likecode,width, andheight.
- They run in a sandboxed environment inside the browser’s JVM for security.
- They are client-side: logic executes on the user’s machine, not the server.
Simple mental picture
Think of an applet as a tiny Java window drawn inside a web page, which can respond to mouse clicks, keyboard input, and repaint graphics, like a mini desktop app embedded in HTML.
Basic structure and lifecycle
A typical Java applet:
- Imports
java.applet.Appletandjava.awt.*.
- Extends
Applet. - Overrides lifecycle methods:
init()– one-time initialization when the applet is first loaded.
* `start()` – called each time the page becomes active (e.g., tab focused).
* `stop()` – called when the user navigates away or the applet becomes inactive.
* `destroy()` – final cleanup before the applet is removed from memory.
* `paint(Graphics g)` – used to draw text, shapes, or images on the applet area.
These methods define the applet’s lifecycle inside the browser, from load to termination.
How applets appear in HTML
You typically host the compiled .class file on a web server and embed it in
an HTML page like:
- Using
<applet>tag withcode,width,height.
- Or using
<object>for more modern embedding.
Then you can open that HTML in a Java-enabled browser or an applet viewer to see the applet running as part of the page.
Bottom note: Information gathered from public forums or data available on the internet and portrayed here.