Applets - Part 1

bullet

Just as controls placed on forms extend the Form superclass, applets (executable JAVA code that runs on the Web) that you write extend the Applet superclass.

bullet

java.applet.* is the package to import that contains the definition of the superclass Applet

bullet

There are 3 interfaces to Applets --

bullet

AppletContext - for communication between applets

bullet

AppletStub - to write applet viewers (like browsers)

bullet

AudioClip - to play sounds

bullet

Web pages host applets. To make applets truly portable (that is, so that they can run on Windows machines, UNIX machines or Mac machines), controls that are used should come from the Abstract Window Toolkit (AWT)

bullet

Security - Applet cannot:

bullet

access a local hard drive once downloaded from the web;

bullet

use any server except the one from which it was launched.

Creating an Applet --

  1. File - New Project - Web Pages
  2. Applet on HTML - name= MyFirstApplet
  3. J++ will create Applet1.java --> the Java code
    and Page1.htm --> the HTML host code
  4. Debug - Start Without Debugging
    The Internet Explorer will start and you'll see the message
    "This string was passed from the HTML host" displayed.

A Closer Look --

import java.awt.*; // required for the abstract window toolkit
import java.applet.*; // required to get the Applet superclass

The HTML Code --

  1. Select Page1.htm from the Project Explorer window
  2. Note the 3 tabs at the bottom:
    bulletDesign - to create the user interface,
    bulletSource - to view the HTML source code,
    bulletQuickView - to see what you've created without invoking a browser
  3. Right-click on the picture (grey area), and select Always View As Text
    This will show the applet HTML code.

HTML Tags --

<HTML> denotes the start of HTML instructions
<HEAD> to document the purpose & author of the HTML
<META> what was used to create the HTML, and keywords, descriptions, etc.
<BODY> home for Java components and what to be displayed
<P> start a new paragraph
nbsp; non-breakable space (always displays a single space)
<APPLET
code=Applet1.class
width=320
height=200>
specification of applet to run ...
code= name of the JAVA class file
width and height = size of display area in pixels
Other Applet Tags codebase - alternate location of files
name - names running applet
alt - string to display if JAVA is disabled
align - left, right, top, middle, bottom
hspace, vspace - space around the applet display area
<BR> break current line (like a carriage return)
<HR> inserts a horizontal line across the web page
<TITLE> the string to display in the title window of the web page
<PARAM
NAME="label"
VALUE="This string was passed from the host">
Parameters passed to the upload at the start of applet execution after it's been downloaded -
NAME = specifies the control that will receive the parameter
VALUE = the actual value being passeed
<PARAM NAME="background" VALUE="008000"> Set the background color of the Java display area
<PARAM NAME="foreground" VALUE="FFFFFF"> Set the foreground color of the Java display area
</tag> Be sure to add the closing tag for each opening tag you've used. Closing tags begin with a slash (/)

Creating an Applet from Scratch --

  1. Menu - Visual J++ Projects - EMPTY PROJECT named ScratchApplet
  2. Project Explorere - Right Click ScratchApplet -
    Add Class - ScratchApplet.java
  3. In the code window, add the 2 import lines before the class definition --

    import java.awt.*;
    import java.applet.*;
  4. Extend the ScratchApplet class definition to be a subclass of Applet --

    class public ScratchApplet extends Applet
  5. Create a webpage and add the applet tag to reference the ScratchApplet class
    Project Explorer -
    Right click ScratchApplet.java
    Add Webpage
    - name = ScratchApplet.htm
    Click Source tab at bottom to add the applet tag --
    <APPLET code="ScratchApplet.class" height=200 width=200 VIEWASTEXT>
    Hello World! Your browser does not support JAVA if you see this!
    </APPLET>

Override Methods from Superclasses --

Using the paint class, we'll overload the inherited method --

  1. Class Outline Window -
    Right click Inherited Members
    Highlight paint and Right click -
    Select Override Method
  2. Add this line of code --
    g.drawString("Hello from Java!", 50, 50);
    // the 50, 50 specifies X and Y coordinates
    // within the display area from the top-left corner.
  3. Start without debugging

Download ScratchApplet.ZIP by clicking here

Created & Maintained by DrB - Comments welcome.
Last edited on Wednesday, February 18, 2009