Java Applets

Java applets are small applications that are executed within a web browser using a Java Virtual Machine (JVM). They were commonly used in the early days of the web to add interactive and multimedia content to web pages. However, their usage has declined in recent years due to security concerns and the popularity of other web technologies like HTML5.

To create a Java applet, you typically use the Java Applet API to extend the Applet class and define your applet's behavior. You can then embed your applet in an HTML page using the <applet> tag.

Here's an example of a simple Java applet:

import java.applet.Applet;
import java.awt.Graphics;

public class MyFirstApplet extends Applet {
    public void paint(Graphics g) {
        g.drawString("Hello, World!", 20, 20);
    }
}

To embed this applet in an HTML page, you would create an HTML file like this:

<html>
<head>
    <title>My First Applet</title>
</head>
<body>
    <applet code="MyFirstApplet.class" width="200" height="200"></applet>
</body>
</html>

When the user loads this HTML page in a web browser, the Java applet will be executed and will display the message "Hello, World!" on the page.

It's worth noting that Java applets are no longer supported in modern web browsers due to security concerns. If you need to add interactive or multimedia content to a web page, you should consider using other web technologies like HTML5 or JavaScript.

How to run Java applets ? 

 

Java applets can be run in web browsers that support the Java Plugin. However, modern web browsers no longer support the Java Plugin by default due to security concerns. Therefore, running Java applets in a web browser can be challenging.

Here are the general steps to run a Java applet in a web browser:

  1. Install the Java Runtime Environment (JRE) on your computer if it is not already installed. You can download the JRE from the Oracle website.
  2. Launch a web browser that supports the Java Plugin, such as Internet Explorer, Firefox, or Safari. Note that Google Chrome no longer supports the Java Plugin.
  3. Navigate to a web page that contains a Java applet.
  4. If the Java Plugin is not enabled, enable it in the browser settings. The location of this setting varies by browser.
  5. If the Java applet requires permissions to access your computer or network, grant the necessary permissions when prompted by the browser.

It's worth noting that Java applets are no longer widely used due to security concerns, and many modern web browsers have disabled the Java Plugin by default. Therefore, running Java applets in a web browser may not be possible in some cases.

Alternatively, you can run Java applets outside of a web browser using a standalone Java applet viewer. The Java Development Kit (JDK) includes an applet viewer that can be used to run Java applets on your computer. To use the applet viewer, you need to have the Java code for the applet and the HTML file that embeds the applet. You can then run the applet using the command line.

For example:
javac MyFirstApplet.java
appletviewer MyFirstApplet.html

This command would run the applet defined in the HTML file "MyFirstApplet.html".

Lifecycle methods for Applet:

The java.applet.Applet class 4 life cycle methods and java.awt.Component class provides 1 life cycle methods for an applet.

java.applet.Applet class

For creating any applet java.applet.Applet class must be inherited. It provides 4 life cycle methods of applet.

public void init(): is used to initialized the Applet. It is invoked only once.
public void start(): is invoked after the init() method or browser is maximized. It is used to start the Applet.
public void stop(): is used to stop the Applet. It is invoked when Applet is stop or browser is minimized.
public void destroy(): is used to destroy the Applet. It is invoked only once.

java.awt.Component class

The Component class provides 1 life cycle method of applet.

public void paint(Graphics g): is used to paint the Applet. It provides Graphics class object that can be used for drawing oval, rectangle, arc etc.

 

Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

23853