In this section, we describe a simple applet, MultiplyApplet.java. This applet performs the same function , to output the product of two integers supplied as parameters. In contrast to an application that is a standalone program, an applet is invoked by a Web browser from a Web page. A Web page written in HTML (hypertext markup language) will contain an applet tag that specifies the location of the applet class files and the position of the applet on the Web page. The browser retrieves the applet class files across the Internet (of course, the applet class files may reside in the user's local computer), and runs the applet using the browser's JVM. Multiply.html is an example of a minimal Web page that loads MultiplyApplet.
It is sufficient to note that an HTML page consists of a series of tags and content that describes how a Web page looks when displayed. Tags start with a <> character. Some tags have a slash after the leading <. The tag without the slash is the start tag, and the tag with the slash is the end tag. Tags can be nested within one another. Within the tag are applet details such as the width and height of the applet display area. In particular, the clause in line 7,
CODE = ''MultiplyApplet"
gives the name of the applet class file.
WIDTH = 250
gives the initial width of the applet display area in pixels.
HEIGHT = 200
gives the initial height of the applet display area in pixels. In line 8,
NAME = "MultiplyApplet"
is the name given to the applet instance. This makes it possible for applets on the same page to communicate with each other. In line 9,
It is sufficient to note that an HTML page consists of a series of tags and content that describes how a Web page looks when displayed. Tags start with a <> character. Some tags have a slash after the leading <. The tag without the slash is the start tag, and the tag with the slash is the end tag. Tags can be nested within one another. Within the tag are applet details such as the width and height of the applet display area. In particular, the clause in line 7,
CODE = ''MultiplyApplet"
gives the name of the applet class file.
WIDTH = 250
gives the initial width of the applet display area in pixels.
HEIGHT = 200
gives the initial height of the applet display area in pixels. In line 8,
NAME = "MultiplyApplet"
is the name given to the applet instance. This makes it possible for applets on the same page to communicate with each other. In line 9,
assigns the value 7 to the named parameter, firstInt. The applet itself, MultiplyApplet, then uses the parameter names, in this case, firstInt and secondInt, to retrieve the parameters set in the HTML page, as we shall see shortly.
We now turn to the MultiplyApplet.java code itself.
MultiplyApplet.java
public class MultiplyApplet extends java.applet.Applet
{
private string param1;
private string param2;
private string resultString;
private int arg1;
private int arg2;
private int result;
public void init()
{
param1 = getParameter("firstInt");
param2 = getParameter("secondInt");
arg1 = Integer.parseInt(param1);
arg2 = Integer.parseInt(param2);
result = arg1 * arg2;
}
public void paint(java.awt.Graphics g) {
resultString = Integer.toString(result);
g.drawString(''The product of " + param1 + " and " +
param2 + " is " + resultString, 50, 100);
}
}
Note that just like a Java application, Java applet source code is stored in a file with a name of the form className.java. The declaration (line 1),
public class MultiplyApplet extends java.applet.Applet {
indicates that the MultiplyApplet class is an applet. More accurately, MultiplyApplet is a subclass of the java.applet.Applet class. As such, MultiplyApplet inherits a number of methods from the Applet class.
Note that we do not have a main method. main methods are the starting points for standalone applications written in Java. The life cycle of an applet is different. An applet usually contains an init method; this method is invoked when the applet is loaded into a Web browser for the first time. Typically, init will perform initialization; in our simple example, init actually performs the multiplication of the two input parameters.
getparameter is an Applet method that returns the value of the named parameter. The named parameter must be present in the PARAM NAME tag in the HTML Web page. For example, in the statement (line 11)
param1 = getParameter("firstInt");
the named parameter "firstInt" corresponds to that in line 9,
of the HTML page, Multiply.html.
In line 18 of MultiplyApplet.java, the paint method is used to output to the applet drawing area. It overrides the paint method of the java.awt.Component class. The paint method takes an object g of type java.awt.Graphics as an argument. This object represents the applet drawing area. drawString is one of the methods in the Graphics class, and lines 20–21 draw the text corresponding to the supplied String to
the applet drawing area, g, at x and y coordinates of 50 and 100.
As with applications, we use the javac compiler to produce a bytecode file
MultiplyApplet.class.
Figure 8.9: Applet Viewer invoking MultiplyApplet.
file, and MultiplyApplet.class files all reside in the same directory, we can issue the command
> appletviewer Multiply.html
The result is shown in Figure .
Assuming we have a Java-enabled Web browser installed on our test computer, we simply need to enter the full HTML file path name in the browser's address area. For example, Figure 8.10 shows the output from a Microsoft Internet Explorer version 5 Web browser when the path name C:\JavaExamples\Multiply.html has been entered in the address area.
No comments:
Post a Comment