Java Applets provide a way to run Java programs directly within web browsers. While modern browsers have reduced support for them, understanding applets is still valuable for grasping fundamental Java concepts, especially for beginners. This tutorial will guide you through creating a simple applet program to draw a car using Java.
Originally designed to add interactive content to web pages, Java applets are small applications executed within a web browser. They are written in Java and require a Java Virtual Machine (JVM) to run. To create an applet, you’ll need to work with the java.applet.Applet
class and the java.awt.Graphics
class for drawing.
Let’s dive into the steps to create your own car drawing applet.
Prerequisites
Before you begin, ensure you have:
- Java Development Kit (JDK): Java applets are best supported with older versions of Java, specifically Java 8 or earlier. Newer versions may not be compatible with running applets in browsers. Make sure you have a compatible JDK installed.
Step-by-Step Guide: Creating a Car Drawing Applet
Follow these steps to write, compile, and run your Java applet that draws a car.
Step 1: Writing the Java Applet Code
First, create a new file named Car.java
. This file will contain the Java code for our applet. Open your preferred text editor and paste the following code into Car.java
:
import java.applet.Applet;
import java.awt.Color;
import java.awt.Graphics;
public class Car extends Applet {
public void paint(Graphics g) {
// Car Body
g.setColor(Color.RED);
g.fillRect(50, 100, 200, 50); // Main body
g.fillRect(75, 75, 150, 50); // Upper body
// Windows
g.setColor(Color.CYAN);
g.fillRect(110, 80, 30, 30); // Back window
g.fillRect(160, 80, 30, 30); // Front window
// Wheels
g.setColor(Color.BLACK);
g.fillOval(75, 140, 50, 50); // Front wheel
g.fillOval(175, 140, 50, 50); // Rear wheel
}
}
This Java code does the following:
- Imports necessary classes:
java.applet.Applet
for creating applets,java.awt.Color
for colors, andjava.awt.Graphics
for drawing. - Defines the
Car
class: This class extendsApplet
, making it an applet. - Overrides the
paint(Graphics g)
method: This method is automatically called when the applet needs to be drawn or redrawn. TheGraphics
objectg
provides methods for drawing shapes and text. - Draws the car: It uses
fillRect()
to draw rectangles for the car body and windows, andfillOval()
to draw circles for the wheels.setColor()
is used to set the drawing color before each shape.
Step 2: Creating the HTML File to Embed the Applet
To run your applet in a browser, you need an HTML file to embed it. Create a new file named index.html
in the same directory as your Car.java
file and add the following HTML code:
<!DOCTYPE html>
<html>
<head>
<title>Simple Car Applet</title>
</head>
<body>
<applet code="Car.class" width="300" height="200">
Your browser does not support Java applets.
</applet>
</body>
</html>
This HTML file includes:
- Basic HTML structure:
<html>
,<head>
,<title>
, and<body>
tags. <applet>
tag: This tag is used to embed the Java applet into the HTML page.code="Car.class"
: Specifies the name of the compiled Java applet class file.width="300" height="200"
: Sets the width and height of the applet display area in pixels.- “Your browser does not support Java applets.”: This message will be displayed if the browser cannot run Java applets.
Step 3: Compiling the Java Code
Now, you need to compile your Car.java
file into a .class
file that the applet viewer or a browser can understand.
-
Open Command Prompt or Terminal: Navigate to the directory where you saved
Car.java
andindex.html
. -
Compile Command: Run the following command:
javac Car.java
If the compilation is successful, a new file named
Car.class
will be created in the same directory. This.class
file contains the bytecode of your Java applet.
Step 4: Running the Java Applet
To run your applet, you can use the appletviewer
tool that comes with the JDK.
-
Run Command: In the same command prompt or terminal, execute:
appletviewer index.html
This command will open a new window displaying your Java applet.
Output of the Car Drawing Applet
After running the appletviewer
command, you should see a window displaying a simple red car with cyan windows and black wheels, as drawn by your Java applet program.
Important Notes:
- File Location: Ensure that
Car.class
andindex.html
are in the same directory. Theappletviewer
and browser need to find the.class
file specified in the HTML. - Browser Support: Modern web browsers have largely dropped support for Java applets due to security concerns and the emergence of alternative web technologies. To run applets, you might need to use older browsers or the
appletviewer
provided with the JDK. Theappletviewer
is primarily for testing applets during development.
Conclusion
Congratulations! You have successfully created a simple Java applet program to draw a car. This tutorial demonstrated the basic steps of writing, compiling, and running a Java applet. While applets are less common in modern web development, this exercise provides a foundational understanding of Java graphics programming and the applet structure. You can now experiment further by adding more details to your car drawing, or exploring other shapes and graphics methods available in Java AWT (Abstract Window Toolkit).