The code below demonstrate the very basic of Java applet. Applet is a small Java application that can be embedded on the web browser.
package org.kodejava.applet;
import java.applet.*;
import java.awt.*;
public class HelloWorldApplet extends Applet {
public void init() {
}
public void start() {
}
public void stop() {
}
public void destroy() {
}
public void paint(Graphics g) {
g.setColor(Color.GREEN);
g.drawString("Hello World", 50, 100);
}
}
To display the applet we need to create an HTML document. Here is a simple example of the document.
<!DOCTYPE html>
<html lang="en">
<head>
<title>Hello World Applet</title>
</head>
<body>
<applet
code="org.kodejava.applet.HelloWorldApplet"
height="250"
width="250">
</applet>
</body>
</html>
You can now load the applet in your browser or by using the appletviewer
utility.
appletviewer.exe hello-world-applet.html
Latest posts by Wayan (see all)
- How do I compile and execute a JDK preview features with Maven? - December 8, 2023
- How do I sum object property using Stream API? - December 7, 2023
- How do I iterate through date range in Java? - October 5, 2023