Java Applet Example W3Schools Guide With Code Demos

Robert Jul 13, 2026

Java Applets, while largely phased out in modern web development, were once a key technology for creating interactive web applications. W3Schools, renowned for its web development tutorials, offers a wealth of examples to help understand and use Java Applets. Let's explore some key aspects with simple, practical examples.

concept of java #java  #javanotes
concept of java #java #javanotes

Before delving into Java Applets, it's crucial to understand that they require a Java Runtime Environment (JRE) on the client's machine. As they're no longer supported by modern browsers, they're mostly used in legacy applications or for educational purposes.

Object in java
Object in java

Creating Basic Java Applets

Let's start with a simple "Hello, World!" Java Applet example from W3Schools. In your HTML file, include a parameter specifying the applet's class and ensure you've got a Java Applet viewer installed:

The OOP'S Concept in JAVA🤔💯✅👇
The OOP'S Concept in JAVA🤔💯✅👇

<applet code="HelloWorld.class" width=200 height=40></applet>

Understanding Applet Life Cycle

Java Collection Framework: ArrayList Explained Visually
Java Collection Framework: ArrayList Explained Visually

Java Applets have a specific life cycle: init(), start(), paint(), stop(), destroy(). In our simple applet, "Hello, World!" is displayed in the paint() method:

Using Java Applet Parameters

Applets can receive parameters from the HTML page. In the same "HelloWorld" applet, we can modify the displayed message based on a parameter named "msg":

Inheritance in Java Explained | OOP Concepts for Beginners
Inheritance in Java Explained | OOP Concepts for Beginners

public void init() { if (getParameter("msg") != null) { greeting = getParameter("msg"); } else { greeting = "Hello, World!"; } }

Drawing in Java Applets

Java Applets can create animations or complex visual representations. W3Schools provides an example of drawing shapes using the Graphics object:

public void paint(Graphics g) { g.drawRect(20, 20, 50, 50); g.fillOval(20, 20, 50, 50); }

Java Patterns Notes 1
Java Patterns Notes 1

Using Timers for Animation

By using the java.awt.event.ActionListener and Timer classes, you can create animations in your Java Applet. Here's a simple example of a moving line:

☕ Java Basics Cheat Sheet for Beginners | Learn Java in One Page 🚀
☕ Java Basics Cheat Sheet for Beginners | Learn Java in One Page 🚀
30 Days of Java – Day 4 | Java If-Else Statement Explained for Beginners
30 Days of Java – Day 4 | Java If-Else Statement Explained for Beginners
30 Days of Java – Day 15 | Java Polymorphism Explained for Beginners
30 Days of Java – Day 15 | Java Polymorphism Explained for Beginners
Master Two Pointers Technique 💻 | Java DSA Interview Preparation Guide
Master Two Pointers Technique 💻 | Java DSA Interview Preparation Guide
30 Days of Java – Day 7 | Java Arrays Explained for Beginners
30 Days of Java – Day 7 | Java Arrays Explained for Beginners
30 Days of Java – Day 2 | Variables & Data Types Explained for Beginners and students
30 Days of Java – Day 2 | Variables & Data Types Explained for Beginners and students
Level Up Your Java 🚀 Intermediate Cheat Sheet You’ll Actually Use
Level Up Your Java 🚀 Intermediate Cheat Sheet You’ll Actually Use
Java Cheat Sheet: Operators, Scanner & User Input ☕
Java Cheat Sheet: Operators, Scanner & User Input ☕
Java Project Ideas : -
Java Project Ideas : -
java array operations
java array operations
Logical Operators in Java: Think Smarter, Code Better ☕🚀
Logical Operators in Java: Think Smarter, Code Better ☕🚀
Java Data Types Made Easy for Beginners
Java Data Types Made Easy for Beginners
30 Days of Java – Day 17 | Java Interfaces Explained for Beginners
30 Days of Java – Day 17 | Java Interfaces Explained for Beginners
the 50 java project ideas final year list is shown in orange and white, with text
the 50 java project ideas final year list is shown in orange and white, with text
Java Basics Cheat Sheet | Core Java Programming Notes for Beginners | AKTU Notes
Java Basics Cheat Sheet | Core Java Programming Notes for Beginners | AKTU Notes
30 Days of Java – Day 8 | Java For-Each Loop Explained for Beginners
30 Days of Java – Day 8 | Java For-Each Loop Explained for Beginners
Top 5 Java Projects for Beginners
Top 5 Java Projects for Beginners
30 Days of Java – Day 22 | ArrayList vs LinkedList Explained for Beginners
30 Days of Java – Day 22 | ArrayList vs LinkedList Explained for Beginners
30 Days of Java – Day 18 | Java Encapsulation Explained for Beginners
30 Days of Java – Day 18 | Java Encapsulation Explained for Beginners
9 Great JAVA Project Ideas 🙋‍♂️📲🖥️
9 Great JAVA Project Ideas 🙋‍♂️📲🖥️

Timer t = new Timer(500, new ActionListener() { public void actionPerformed(ActionEvent e) { x += 5; repaint(); } });

Integrating Java Applets with JavaScript

While not commonly used, it's possible to communicate between Java Applets and JavaScript. W3Schools demonstrates this with a simple click event:

In Java Applet

Set a flag when the applet is clicked:

public boolean mouseDown(Event e, int x, int y) { clicked = true; return true; }

In JavaScript

Check for and react to the Java Applet's state:

function checkApplet() { if (document.applets.Applet1.clicked) { alert("Applet was clicked!"); document.applets.Applet1.clicked = false; } setTimeout("checkApplet()", 1000); }

While Java Applets are no longer widely used, understanding their inner workings can be invaluable for any Java developer. W3Schools' examples provide a solid foundation for those seeking to explore this deprecated but historic technology.

Don't let the past hold you back. Explore these examples, learn the principles, and apply them in modern contexts. Ready to take your Java skills to the next level? Why not explore W3Schools' tutorials on JavaFX or web development with Java?"