Applets The discussion that follows assumes that you are already familiar
with the rudiments of creating a web page. IntroductionAn applet is a Java application that runs in a web page. Two components are needed to run an applet:
An applet markup tag has the following form: <APPLET CODE="byte code file name" WIDTH=width HEIGHT=height> </APPLET> The width and height are the width and height, respectively, of the
applets screen area within the browser window as measured in pixels. For convenience, it is easiest to place applets and the HTML
documents that use them in the same directory. This
rule can be violated, but doing so involves some additional complexities that we are not
going to discuss. ExampleLet us assume that the Fahrenheit to centigrade conversion program
has already been rewritten as a Java applet. It
might appear in a web page as shown in the next snapshot.
Here is the HTML code for the example: <html> <head> <TITLE>Fahrenheit to Centigrade Converter</TITLE> </head> <body> <UL> <LI>Enter degrees Fahrenheit. <LI>Click the <STRONG>Convert</STRONG> button and the centigrade equivalent will be displayed. </UL> <APPLET CODE="FahrenheitToCentigrade.class" WIDTH=200 HEIGHT=150> </APPLET> <P> Applets greatly increase the power of the Web. <body> <html> Converting an Application to an AppletPreviously, we have used the class GBFrame
to provide the framework for GUI-based applications.
We now show how to use a similar class, GBApplet,
to write GUI-based applets. To convert Java
applications to applets, we must do four things:
public void init(){ ... } The following listing shows these changes: import java.awt.*; import BreezyGUI.*; public class FahrenheitToCentigrade extends GBApplet{ Label degreesFahrenheitLabel = addLabel ("Degrees Fahrenheit",1,1,1,1); IntegerField degreesFahrenheitField = addIntegerField (0,1,2,1,1); Label degreesCentigradeLabel = addLabel ("Degrees Centigrade",2,1,1,1); IntegerField degreesCentigradeField = addIntegerField (0,2,2,1,1); Button convertButton = addButton ("Convert",3,1,2,1); public void buttonClicked (Button buttonObj){ int fahrenheit; int centigrade;
fahrenheit = degreesFahrenheitField.getNumber(); centigrade = (fahrenheit - 32) * 5 / 9; degreesCentigradeField.setNumber
(centigrade); } } Constraints on AppletsThere are several constraints on the use of applets. Here they are:
Passing Parameters to AppletsIt is possible to send information from an HTML page to an applet. The information is passed in HTML parameter tags
and is retrieved in the applet's code. In the
following example, a parameter tag binds the string "5"
to the name numberOfClasses. The parameter tag must appear between the opening
and closing applet tag: <APPLET CODE="Courses.class" WIDTH=150 HEIGHT=100> <PARAM NAME=numberOfCourses VALUE="5"> </APPLET> At any point within the applet, the method getParameter
can retrieve the parameters value, but always as a string: String str = getParameter ("numberOfCourses"); int num = (new Integer(str)).intValue(); A common location for such code is in the init
method. If there are several parameters, each requires its own tag. |
| ||||