Session 7 Lecture Notes for First Course in Java  

course homepage      course calendar

Introducing Applets


What is an applet?

Unlike a Java application, a Java applet runs inside of a browser, such as Netscape Navigator or Internet Explorer.

Applet Viewer

You can also run an applet by using the applet viewer the comes with the JDK. The applet view also allows you to debug your applet.

The appletview resides in the bin directory of your JDK. You can launch an applet by giving the relative path to the HTML page for the applet. For example:

D:\jdk1.3\bin>appletviewer ..\demo\applets\Fractal\example1.html

How does an applet run?

The differences between an applet and an application:

The "sandbox" is a security layer around the applet that prevents the applet from accessing:

(These restrictions can be customized by the client through the browser or by modifying the security properties file.)

Signed Applets

The provider of an applet can get the applet out of the "sandbox" restrictions by signing the applet.

If the user's browser detects a signed applet, it displays the certificate and prompts the user to respond by trusting this applet, all applets from this applet provider, or declining to trust this applet.

To sign your applet, you work with a third party, such as VeriSign, http://www.verisign.com/products/signing/index.html
that provides a digital signature, or key, that authenticates you as the provider of the applet and guarantees that the applet code has not been alterted. 

The client actually communicates both with the applet provider and the third party.

A signed applet has not of the applet restrictions, and is essentially an application even though it runs in the browser.

Basic HTML to embed an applet in a web page

To embed an applet in a web page, you must write HTML code to reference the the applet's class file and the size of the applet.

Sun describes this at http://java.sun.com/products/jdk/1.1/docs/guide/misc/applet.html

<applet code="MyApplet.class" width=100 height=140></applet>

Minimal HTML to display an applet

The glossary applet at http://www.wordesign.com/java/glossary/ uses very little HTML to display a glossary of Java terms:

<html>
<head>
<title>Glossary of Java Terms (by Thomas Albert and Alex Aybes)</title>
</head>
<body>
Glossary of Java Terms (click on a term for the definition)<br>
<APPLET CODE="com.wordesign.glossary.GlossaryApplet.class" height="80%" width="80%"></APPLET>
</body>
</html>

Note: The applet tag requires the fully qualified name of the class, which is the name of the class package followed by the class name.

Creating an applet

To create an applet, you extend the applet class and override methods of the java.applet package.

extending the applet class

Java defines the applet object in the java.applet package.

Key method definitions are:

Swing applets versus AWT applets

The java.awt packages provide the abstract windowing toolkit (AWT), which supports graphical user interfaces (GUIs).

More recent versions of Java also include extensions called javax.swing packages, which add more support for GUIs.

Basic methods: init(), paint()

The init() method is called when the browser is done loading the applet. Using this method, you will typically do all the initializations your applet needs.

An applet inherits the paint() method from the Component class. The paint() method takes as argument an instance of the Graphics class. The graphics object provides methods to draw geometric shapes, text, or other things.

For your first applet, you will typically override the paint() method to do your custom drawing.

Demonstration of SillyBalls Applet

The "silly balls" applet has animated objects.
Download SillyBallsApplet.zip.
**********************************
package sample;

import java.awt.*;
import java.awt.event.*;
import java.applet.*;
import java.util.*;

public class MyFirstApplet extends Applet
{
      private boolean done = false;
     
    /**Construct the applet*/
    public MyFirstApplet()
    {
    }

    /**Initialize the applet*/
    public void init()
    {
            done = false;
    }

    /**Start the applet*/
    public void start()
    {
            final Applet applet = this;
            new Thread()
            {
                  public void run()
                  {
                        while (!done)
                        {
                              applet.repaint();
                              try
                              {
                                    // specify the display time in milliseconds
                                    this.sleep(10);
                              }
                              catch (Exception e)
                              {
                              }
                        }
                  }
            }.start();
    }
     
      public void paint(Graphics g)
      {
            int top,
                  left,
                  width,
                  height;
            top = (int)(Math.random()*getHeight());
            left = (int)(Math.random()*getWidth());
            width = (int)(Math.random()*(getWidth() - left));
            height = (int)(Math.random()*(getHeight() - top));
            Color color = new Color((float)Math.random(), (float)Math.random(), (float)Math.random());

            new RandomOval( top, left, width, height, color).paint(g);
      }

    /**Stop the applet*/
    public void stop()
    {
            done = true;
    }

    /**Destroy the applet*/
    public void destroy()
    {
    }

    /**Get Applet information*/
    public String getAppletInfo()
    {
        return "Applet Information";
    }
     
      class RandomOval
      {
            private int top,
                              left,
                              width,
                              height;
            private Color color;
           
            public RandomOval(int top, int left, int width, int height, Color color)
            {
                  this.top = top;
                  this.left = left;
                  this.width = width;
                  this.height = height;
                  this.color = color;
            }
           
            public void paint(Graphics g)
            {
                  g.setColor(color);
                  g.fillOval(top, left, width, height);
            }
      }
}

**********************************

Other aspects

parameters

You can pass parameters to an applet from the HTML code. 
For example, your HTML page might have a parameter, language, with a value, French, that tells the applet to display the French version of the applet.

The syntax to add parameters to your HTML page is:

http://java.sun.com/products/jdk/1.1/docs/guide/misc/applet.html

<applet code="appletname.class" width=400 height=75>
 <param name="language" value="French"> 
 </applet>

How the applet gets parameter values from your HTML page

The applet class provides a method called getParameter() that takes as input a String that represents the name of the parameter. In our example, the parameter would be language. The return value of the getParameter() method is the value of the parameter. In our example, the parameter value is French.

size

The client can resize your applet if you specify the applet size as a percentage of the browser window. Alternatively, you can specify the applet size as a specific number of pixels.

________________________
course homepage      course calendar