Your first Java Applet
This tutorial focuses on creating your first Java Applet in:
- Windows
- Linux
Windows
-
Open notepad by going to Start -> Programs -> Accessories -> Notepad OR go to Start -> Run -> Type notepad and press Enter.
-
Type in the following code:
import java.applet.*; import java.awt.*; public class FirstApplet extends Applet{ public void paint(Graphics g){ g.drawString("My first applet", 10, 50); } } -
Save the file as FirstApplet.java
NOTE: Make sure that you give the file a .java extension and that you save it as type 'All Files', otherwise you will get plain text. Also, make sure that you save the file as FirstApplet.java with the same capitalization. Java is a case sensitive language!
-
Open the command prompt by going to Start -> Run -> Type cmd and press Enter and switch to the directory where you saved the file (to do so type CD and the absolute path of the directory where you saved the file).
-
Compile the file using the javac command by typing javac FirstApplet.java and press Enter.
-
Upon successful compilation, you will get the name of the directory you are in with no output.
NOTE: If you got an error, make sure the code is the same as the one shown on this page. Also, make sure that the name of the file is FirstApplet.java with the same capitalization, and the file must be saved as type 'All files'.
-
After successful compilation you can view the applet, but not the same way as you would view a regular Java program.
You can view an applet by either placing it on a webpage or using the appletviewer utility.
-
Viewing an applet by placing it on a webpage:
-
Open notepad by going to Start -> Programs -> Accessories -> Notepad OR go to Start -> Run -> Type notepad and press Enter.
-
Type in the following code:
<html> <head> <title>My first applet</title> </head> <body> <applet title="An applet" code="FirstApplet.class" width="200" height="200"></applet> </body> </html>
-
-
-
Save the file as firstApplet.html
NOTE: Make sure that you give the file a .htm or .html extension and that you save it as type 'All Files', otherwise you will get plain text. Also, make sure that you save the file as FirstApplet.java with the same capitalization. Java is a case sensitive language!
-
Open your web browser and view the file. A gray box (which is the applet) with the text "My first applet" should appear on the page (or no gray box, but just the text depending on your browser):
-
Viewing an applet with the appletviewer utility:
-
On the command line type appletviewer nameOfWebpageWhereAppletIsLocated and press Enter. So if the name of the webpage where the applet is located firstApplet.html you would type appletviewer firstApplet.html The applet should appear on screen:
The actual applet:
NOTE: The applet will not appear if your browser has Java disabled.
Linux
Coming soon.....




