Notes Java J2

Math 10 -- D. C. Smolarski, S.J.
Santa Clara University, Department of Mathematics and Computer Science

[Return to Math 10 Homepage | Return to Notes Listing Page]

Contents


Java Applets

A Java Applet is a program segment, usually involving some graphical interface, which can be embedded in a web page by being called by an HTML-file. Such applets can be very simple, for example only printing out predetermined text, or interactive in text mode, or interactive in graphical mode, or animated. In general they are limited only by the ingenuity of the programmer.

Java Applets which make use of Java 1.1 classes may have difficulty being viewed by Netscape browsers or older versions of Internet Explorer. One may need to use the latest version of appletviewer to test graphical applets using Java 1.1. All browsers should be able to view accurately applets written with Java 1.0.

Calling Java Applets from Web Pages

Java applets are referenced from web pages as in this example:
    <html>
    <title>Java sample</title>
    <body>
    Here we have a call to a Java file.
    <applet code="Testjava3.class" width=500 height=300>
    </applet>
    </body>
    </html>
In the line
    <applet code="Testjava3.class" width=500 height=300>
the code reference refers to the translated code file and gives its correct URL. The width and height references indicate the size of the applet window on the web page in pixels.

Embedding Code in an Applet

The following applet gives an example of how part of the loop structure given in Notes Java 1 can be incorporated into an applet. The initialization section of this applet (i.e., init method, which functions in a way similar to constructor of a C++ class) contains a simple for loop which generates 11 values for i. Each of these integer values is converted into a string which is then displayed in its own TextField box on a web page.

The actual "writing" of components to the web page is done by the method paint which is automatically invoked in its inherited, default form. This method will "paint" to the page any box or textfield specified by an init method, along with other text or figures specified in a user-defined form (e.g., specified by the drawString method), along with figures specified in other user-defined methods in the applet.

Note that this applet contains several import statements which are usually not necessary in a self-standing Java program.

Since this class inherits all the properties of the built-in Java class of Applet, the code must indictate this by the extends Applet clause.

The identifier t is declared as a Java type TestField which puts a text box on a web page and initializes the contents to its argument. The argument can include both text or an integer or both. The text is printed, and the integer indicates the width of the box in numbers of characters. Since the following code attempts to print out numbers, the numbers must first be converted into character strings so that the invocation of a new copy of t actually displays the number as a character rather than interpreting it as the size of the text field.

This must be done by first casting the int value of i into a class of type Integer which can then make use of a standard method (member function), toString() to convert the integer value into a character string. This becomes the initialization argument of TextField at the instantiation of a new copy of this class which is assigned to t.

After t receives its value, the value is added to the visible applet (i.e., on the web page), via the function add(t).

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

   public class Testjava3
      extends Applet
   {
     TextField t;
     public void init()
     { 
       for(int i=0;i<=10;i++)
       {
          t = new TextField((new Integer(i)).toString());
	  add(t);
       }
     }
   }
This applet may be seen by clicking on the link to file java3.html.


This page is maintained by Dennis C. Smolarski, S.J. dsmolarski@math.scu.edu
© Copyright 1998, Dennis C. Smolarski, SJ, All rights reserved. Last changed: 5 December 1998.