Download Appendix H: Applets

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Appendix H: Applets
Although Java's current popularity rests on its ability to create applications, its initial popularity was due to its ability
to create small programs that could be embedded within a Web page. This type of Java program is referred to as an
applet
To execute an applet embedded within a Web page, a Web browser is required. A Web browser is a
program that is run on a user's computer and whose primary function is to accept and display Web pages. The Web
pages themselves can be stored locally on the system running the browser, in which case the computer need not be
connected to the Internet, or the pages can be sent over the Internet into the user's computer and then displayed by
the browser. This second approach, where a user is connected to the Internet while the browser is active, is the
approach taken by an Internet user, but for developers of both Web pages and applets, the first approach is initially
taken. Currently the two most popular browsers are Microsoft's Internet Explorer and Netscape's Navigator. Both of
these browsers have the capability to run Java applets embedded into a Web page and together these two browsers
control almost 100 percent of the browser market.
In this appendix, we show how to create a very simple Web page and display the page using a browser
without being connected to the Internet. We next embed an applet into the basic Web page, and then show how to
create and view an applet independently of a Web page using a Java provided applet viewer program.
Creating and Viewing a Web Page
In its simplest form a Web page is simply a page of text that has been formatted to appear on a video screen. The
formatting is provided by specific "tag" marks inserted into the text. These marks can be used to describe the color
of the text, the color of the background, the title to be placed in the title bar area of a window, and how lists should
appear. They also create links to other pages or to other areas in the currently displayed page. Additionally, graphic
images and sounds can be included in a page as well as Java applets that themselves are graphic images. When
graphics and sounds are included, the page is said to be a multimedia page.
Web pages are created using the hypertext markup language (HTML). These pages consist of a text
1
documented with certain parts of the text "marked up" by "tags" to produce desired screen effects. Tags are always
enclosed in the angle bracket pair, <>. The simplest Web page consists of the tags placed in the form shown in
Figure H-1.
<HTML>
<HEAD>
<TITLE>Your title in here</TITLE>
</HEAD>
<BODY>
The body of your text in here
</BODY>
</HTML>
Figure H-1 A Sample Web Page Format
Although HTML is not case sensitive, which means that both uppercase and lowercase letters can be freely
mixed within the tags that are enclosed within the angle bracket pairs <>, by convention uppercase letters are used
for the tags. The important points to notice about the format shown in Figure H-1 are:
1. A Web page document must begin with an <HTML> tag. The ending </HTML> tag is optional.
2. An HTML Web page has two sections, a head and body, with each section contained within appropriate tags.
3. Closing tags require a forward slash, /.
4. Tag placement is not rigid. Thus, the format shown in Figure H-1 could have been written as:
<HTML><HEAD><TITLE>Your title in here</TITLE></HEAD>
<BODY>The body of your text in here</BODY></HTML>
As a specific example of a Web page, consider the following code:
<HTML>
<HEAD>
2
<TITLE>Testing an applet</TITLE>
</HEAD>
<BODY>
This page is for testing applets.
</BODY>
</HTML>
To create this code, you will need to use either a word processor or an editor program such as Notepad. Also, when
saving the file, make sure to save it with an html extension. For purposes of illustration, assume that this code is
saved as the file named apptester1.html.
To view the Web page created by the preceding code you must first launch a Web browser and then, for
both Internet Explorer and Netscape Navigator, select the File and Open menu options. Then enter the correct drive,
directory, and file name for the stored file. Figure H-2 shows these options for the apptester1.html file using
Microsoft's Internet Explorer and how the Web page produced by this file is displayed. Notice that the title appears
in the title bar at the top of the display and the body text appears within the displayed page. If no title were included
in the code for the page, the title bar at the top of the window would display the drive, directory, and file name of the
selected file.
3
Point of Information
Java Enabled
For a browser to execute a Java applet it must be Java Enabled. The setting for this is as follows:
For Internet Explorer, select the Internet Options under the View menu on the menu bar. Then select the
Advanced Tab, scroll down until you come to the Java VM options ,and select them.
For Netscape Navigator, select Preferences under the Edit menu on the menu bar. Then click on Advanced
and select the Java options.
Security
Because an applet is meant to be executed on someone else's computer via the Web page that it is
embedded in, browser designers were very careful to ensure that an applet could not destroy information on the
executing computer or cause the operating system to crash. These security checks are enforced by a Security
Manager contained within each browser. Chief among the security checks performed by the Security Manager is that
an executing applet is not permitted to access a computer's file system or execute any statement that would modify
the operation of the computer or alter any system variables.
Figure H-2 The apptester1.html Web page
4
Including an Applet
It is now time to include an applet within our Web page. To do this we will have to create a Java applet and
then add the appropriate tags into the Web page directing the browser to execute the applet. The code for the applet
that we want to execute is listed in Figure H-3.
import java.applet.*;
import java.awt.*;
public class HelloWorld extends Applet
{
public void paint(Graphics g)
{
g.drawString("Hello World!", 75, 50);
}
}
Figure H-3 The HelloWorld.java applet
Notice that this code does not have a main() method and the header line for the class defines the
HelloWorld class as extending the Applet class. To access this class requires the first import statement, and
the second import statement is necessary to access the graphics library of classes. Specifically, we want to use the
graphic class' drawString() method to display the text Hello World! at pixel coordinates 75 from the left of
the drawing area and 50 from the top of the drawing area. The actual drawing area allocated for the applet will be set
in the HTML document that calls the applet. In a manner similar to creating a Java application, the code for a Java
applet must be stored in a file having the name of the class and a .java extension. Thus, the code in Figure H-3
must be stored in a file named HelloWorld.java. Before this code can be executed as an applet, it must be
compiled using the command javac HelloWorld.java.
Once the applet has been compiled, which in this case will create a file named HelloWorld.class, its
execution can be indicated within an HTML document using the <APPLET> </APPLET> tags. For example, the
listing for the document named apptester.html, which specifically calls for the execution of the applet defined
in the HelloWorld.class , is provided in Figure H-4. Notice that this document does not display any text on its
own but simply calls for the execution of the HelloWorld.class, which contains the compiled applet. In
addition to specifying the class code that is to be executed, the <APPLET> tag requires specifying the height and
5
width of the area in which the applet is to be displayed. In our particular example, we have specified this area as 100
pixels high by 200 pixels wide. Figure H-5 illustrates how this applet appears when the HTML document is opened
by a browser.
<HTML>
<HEAD>
<TITLE>Testing an applet</TITLE>
</HEAD>
<BODY>
<APPLET code=HelloWorld.class height=100 width=200>
</APPLET>
</BODY>
</HTML>
Figure H-4 The apptester.html Code
Figure H-5 Display of the apptester.html Web Page
Stand-alone Applets
Although applets are always used within the context of a Web page, an individual applet can be tested and executed
by itself without the necessity of embedding it into a Web page. This is done using the appletviewer program that is
provided with the Java development system. This program is command line driven and is used much like the javac
compiler. For example, the statement
appletviewer testapp.html
will start appletviewer and run the HTML document named testapp.html. Because the appletviewer's purpose is
to execute applets, the only HTML tags that it can respond to are <HTML> </HTML> and <APPLET>
6
</APPLET>. Thus, a typical HTML program that would be executed by appletviewer is written in the form
<HTML>
<APPLET code="appletClassName.class" height = n width=m>
</APPLET>
</HTML>
Notice that the complete development process we have used consists of first writing an applet, compiling it,
and then writing an HTML document to execute it. This can be both time consuming and frustrating when all we
want to achieve is the testing of a single applet. To sidestep this tedious process, the appletviewer program is
designed to read any text file. This permits us to eliminate the separate HTML document entirely by placing the
desired <APPLET></APPLET> tags directly into the Java code as comments. For example, the applet code in
Figure H-6 incorporates all of the HTML that is required by appletviewer to produce the same applet shown in
Figure H-5.
// <APPLET code=HelloWorldApp Width=100 HEIGHT=200>
// </APPLET>
import java.applet.*;
import java.awt.*;
public class HelloWorldApp extends Applet
{
public void paint(Graphics g)
{
g.drawString("Hello World!", 75, 50);
}
}
Figure H-6 A Java Applet with Embedded HTML Tags
Placing the <APPLET> tags as comments in the Java program means they will be ignored by the
compilation phase initiated by appletviewer but will be read by the browser action of the program. The command
line necessary to execute the applet defined in Figure H-6 is
appletviewer HelloWorldApp.java
When this command is executed, the applet is first compiled and then displayed according to the specifications in the
<APPLET> tag. Figure H-7 shows the output produced by this command for the HelloWorldApp.java applet.
Once the applet's operation is verified, it can be incorporated into a Web page. Thus, using appletviewer, the
development and testing of an applet are isolated from the design and testing of the Web page that will ultimately
hold it.
7
Figure H-7 The HelloWorldApp Applet Executed by Appletviewer
8