How To Use a Java Applet

Java applets are small Java programs that are delivered over the web and run in accessors web browser. Applet may be a menu appearing on a web page, an animation or image displayed in a web page, or may be some form for getting user input.

Step 1

Open a new file (say, myapp.java) and write following example code in it:

import java.awt.*;

import java.applet.*;

import javax.swing.*;

public class myapp extends JApplet

{

public void paint(Graphics g)

{

g.drawString("My Applet",20,30);

}

}

This applet simply displays a message "My Applet" at given location.

Points to Remember

  • You need to import packages we imported in above code.
  • Applet class has to be public as it runs on remote machines
  • A user defined applet extends JApplet class.
  • Following methods of JApplet class may be overridden:
    • init- Fired once to initialize the applet
    • start- starts the applet
    • stop- stops the running applet
    • paint - redraws applet's ouput
    • update - redraws drawing area
    • destroy - Fired once to release the memory

An applet is invoked from init() method. If init() method is not defined then paint() method will serve as invocation point.

Step 2

Save above file and compile using following command:

javac myapplet.java

This will generate a file named myapplet.class

Step 3

Next step is to embed this class in an HTML file (myapplet.html) using following:

<HTML>

<BODY>

<APPLET CODE="myapplet.class" HEIGHT=300 WIDTH=300>

</APPLET>

</BODY>

</HTML>

Points to Remember

  • HTML file can have any name and not necessarily myapplet.html. However, naming it same as class name is a good idea as it provides offline association.
  • HTML provides APPLET tag to embed applets into a web page.
  • APPLET tag can have following attributes:

* Code - Name of the class file e.g. Code="myapplet.class"

* Height - Height of Applet. e.g. Height = 300

* Width - Width of Applet. e.g. Width = 300

* Codebase - Directory where applet class exists. Can be omitted if it is in current directory

* Alt - Alternate display if applet could not be displayed. e.g. Alt="This was my applet"

* Name - Assigning a name to an applet. e.g. name="My First Applet"

* Align - Aligning applet w.r.t. surrounding text and images. e.g. Align = Right. Other values can be Left, Top, Bottom, Middle, Baseline, Absmiddle, Absbottom, Texttop

* Vspace - Space above and below the applet. e.g. vspace = 20

* Hspace - Space on sides on applet. e.g. hspace = 20

Remember that except numeric values, other attribute values are enclosed in double quotes ("").

Step 4

Now, you are ready with your applet to run using any of the following ways:

  • Double click your HTML file to open it in a web browser
  • Open HTML file through File|Open menu item of a web browser
  • Use appletviewer in the command prompt as:

> appletviewer myapplet.html

Instead of separate HTML file, you can also put <APPLET CODE....></APPLET> code inside the Java file itself and use appletviewer as:

> appletviewer myapplet.java

Step 5

What ??? Unable to run applet. If applet could not be started, then do the following:

In Internet Explorer,

  • Goto Security tab of Internet Options GUI opened through Tools > Internet Options... menu item
  • Press Custom Level... button to do the following settings:
    • Under scripting, choose "Enable"
    • At the bottom, set Custom Level to LOW and press apply

If things still don't work:

On command prompt, enter following command to open policy tool:

>policytool

  • Press"Add Policy Entry" button
  • On Subsequent GUI, press "Add Permission" button
  • In Permissions GUI that opens up, Choose "AllPermission" in Permission drop-down list.
  • Press OK and Done.
  • Save the settings as "java.policy" file in security folder of lib folder in jre folder of your JDK installation. You may also put this file in Documents and Settings folder under C: drive on Windows.

Now you should be able to run your applet.


Applets are widely used and easy to use once you develop one or two applets. Applet runs in sandbox of the JVM and cannot access user's file system and as such are harmless.

Happy appleting. As an exercise, write an applet to draw a smiling face using circles and lines and display "Hello World" in it.

 

Share this article!

Follow us!

Find more helpful articles: