Javatpoint Logo
Javatpoint Logo

Applet Skeleton in Java

Java applets were once a popular way to bring interactive content to web browsers. Although their usage has declined over the years in favor of more modern web technologies like JavaScript and HTML5, it's still essential to understand the fundamentals of Java applets, including the applet skeleton. In this article, we'll explore what an applet skeleton is, its components, and provide full Java programs with outputs to illustrate how it works.

What is an Applet Skeleton?

An applet skeleton is a basic structure or template for creating Java applets. It includes the essential methods and structure required for an applet to run within a web browser. The primary class for creating an applet is java.applet.Applet, and it must be extended to create a custom applet.

Here are the key methods and their purposes in an applet skeleton:

init(): This method is called when the applet is first loaded. It's typically used for initializing variables, setting up the applet's initial state, and performing any necessary setup.

start(): When the applet becomes visible or is restarted after being paused, this method is called. It's often used for starting animations or any other ongoing activities.

stop(): This method is called when the applet becomes invisible or is paused. It's used to stop any ongoing activities or animations to conserve resources.

destroy(): When the applet is no longer needed or the web page is closed, this method is called. It's used for cleaning up resources and performing any final tasks.

paint(Graphics g): This method is called whenever the applet needs to be redrawn. It's where you define the applet's visual appearance and display content.

Now, let's dive into a complete Java program that demonstrates the applet skeleton in action.

In this example, we've created a basic applet named MyFirstApplet. It extends the Applet class and overrides the key methods to provide custom functionality. In the paint method, we use the Graphics object to draw "Hello, World!" at coordinates (50, 50).

Running the Applet

To run the applet, you need an HTML file that embeds the applet. Here's a simple HTML file (index.html) to do just that:

Save this HTML file in the same directory as your Java source file (MyFirstApplet.java). In the <applet> tag, we specify the code attribute as the name of the compiled applet class (MyFirstApplet.class). The width and height attributes define the applet's dimensions.

When you open the HTML file in a web browser with Java support enabled, we should see your applet displaying "Hello, World!" at the specified coordinates (50, 50).

Output:

Hello, World!

In this section, we've explored the applet skeleton in Java, including its key methods and structure. We've also provided a complete Java program that demonstrates how to create a basic applet and embed it in an HTML page. While Java applets are no longer as widely used as they once were, understanding their fundamentals can still be valuable for historical and educational purposes.







Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA