In This Chapter
Java Relies on a "Virtual Computer"
In order to pull off the trick of being useable on a variety of platforms,
Java applets actually run on what's
called the Java Virtual Machine. Look at it this way: Certain programs available today enable you to run
Windows software on a Macintosh, or UNIX software on a Windows computer, and so on. They do this
by emulating the behavior of a particular computer type (they "pretend" to be a Mac or Windows or
UNIX machine so that the software thinks it's running on the correct computer). The Java Virtual Machine
is another type of emulator, but it doesn't emulate Mac, Windows, or UNIX. Rather, it pretends to be a
totally different type of system. And by defining what the Java computer can and cannot do, Sun was able
to create a program that—no matter what physical hardware it's run on—will always look like a Java
computer to the applets.
Java is an "object-oriented" programming language most closely related to C++. "Object-oriented" is a difficult concept to explain clearly, but in essence, it means that the language revolves around manipulating end-results instead of designing the tools for manipulation. An object-oriented sculptor, for instance, would be more concerned with melding together various blocks of clay than with the tools that carve the clay initially. The popular construction toy Legos is a nice example of an object-oriented activity.
To start, you'll need to do a little surfing and pick up a copy of the collection of utilities Sun puts out to aid Java programmers. It’s called the Java Developer's Kit.
The Java Developer's Kit
Before you dive into Java programming, you need to pick up a copy of the Java Developer's Kit (JDK for short). The JDK includes:
In a nutshell, the JDK has everything the budding Java programmer needs to start.
What? How much does it cost? Well, put your checkbook away! The JDK is available free off the Internet. Simply fire up your browser (any browser will do) and point it at the Sun Java Homesite at the address below. The following figure shows the Sun Java Homesite.
Links to download the Java Developer’s Kit.
Browse your way into the Download section, and you'll find the links for downloading the JDK for your machine. Note that, as pictured above, there are actually two JDK’s currently available: one for Java 1.02 and one for Java 1.1. If you want to create applets compatible with the current crop of Java-capable browsers, stick with 1.02. Java 1.1 is the newest release of Java and contains special features that the 3.0 browsers don’t support yet (presumably, the 4.0 versions of Internet Explorer and Netscape Navigator will support JDK 1.1 applets). The file you'll be downloading is rather large (4M or more), so start the download process and then go get a cup of coffee.
When the file has been downloaded completely, follow these steps:
The file will automatically unpack itself, create a \java directory and a bunch of subdirectories, and copy a ton of files into these new directories. When all this is done, you can delete the original file (or store it in a safe place, if you like to keep a copy around just in case).
Where Are the Manuals?
One of the nice things about the JDK is the existence of very thorough documentation. What documentation, you ask? Well, if you fire up your browser and open the \java\progguide\index.htm file, you'll be introduced to one of the nicest documentation collections on Java and the JDK available (see the following figure).
JDK online documentation.
Now, scroll down a bit. As you can see in the next figure, below the Getting Started heading, you'll find Duke, the Java mascot (a cute little guy who looks amazingly like the Starfleet insignia). Look at that—he's waving at you! You guessed it. Duke's a Java applet!
Duke waves a friendly greeting. Resist the temptation to wave back.
All of the documentation that comes with the JDK is available in HTML format, so you might want to browse around a bit right now before going further in the book. One thing to note, though, is that not all the HTML files in the JDK are linked to the other files. The JDK contains a collection of document sets. You can simply list all of the JDK help files with the File Manager or the Windows Explorer and double-click any of the HTML files you like. They will then be opened into your browser.
Back from your local surfing? Good! Take a quick detour into HTML to see how Java applets connect to Web pages: the <APPLET> tag.
Introducing the <APPLET> Tag
The <APPLET> tag's structure is relatively simple. Actually, there are two tags that work together: APPLET and PARAMS. The <APPLET> tag looks like this:
The CODE attribute identifies the applet program and is a file with a ".class" extension located in the same directory as the HTML document. To make your page do something for those poor souls who don't have a Java-enabled browser, you can place any additional alternative HTML statements inside the <APPLET> tag. Java browsers will ignore these statements, and non-Java browsers will ignore the <APPLET> tag (which works out quite well, don't you think?).
The WIDTH and HEIGHT attributes define the size of the applet on the page. An applet's size is specified in pixels. In addition, there are several other optional attributes that you can add to the <APPLET> tag to modify its behavior.
The CODEBASE attribute lets you specify a URL (directory path) where your applet .class files are located (if they're in a place other than your HTML documents). As I mentioned previously, if you don't specify CODEBASE, the applets are assumed to be in the same directory as the HTML files.
If, for some reason, a browser understands the <APPLET> tag but can't run applets (for example, if the browser’s Java capability has been disabled by the user), you can specify some text to display with the ALT attribute. This is similar to the ALT attribute of the <IMG> tag, which displays the specified text string on browsers that don’t show graphics (text-based browsers or browsers with image loading disabled).
Just like the <IMG> tag attribute of the same name, ALIGN controls how the applet is lined up. Acceptable values of the attribute are LEFT, RIGHT, TOP, TEXTTOP, MIDDLE, ABSMIDDLE, BASELINE, BOTTOM, and ABSBOTTOM.
VSPACE= and HSPACE=
These attributes also behave the same way their <IMG> tag counterparts do: They specify the amount of space (in pixels) to leave above and below the applet (VSPACE) and on each side of the applet (HSPACE).
Passing Applet Parameters
Those of you who remember the days of DOS (those glory days before Windows, X-Windows, and the Macintosh made pictures out of everything) might recall that you could run some programs by typing the name of the program and adding additional information on the command line before pressing the Enter key. Those additional pieces of information were called parameters.
It's possible to create applets that can take parameters. (In the case of applets, parameters are additional options that are unique to the applet and aren't covered by other <APPLET> attributes.) Passing parameter values to an applet is done with the <PARAM> tag. <PARAM> tags are placed after the <APPLET> tag and before any of the "alternate HTML" statements and the </APPLET> tag. The <PARAM> tag looks like this:
In that expression, NAME identifies the particular attribute, and VALUE identifies its value. If you have more than one parameter, you use more than one <PARAM> tag. The parameters available depend on the particular applet in question and are defined by the applet’s programmer in the source code to the Java applet. For Java programmers, inside the applet code, you access the values passed in with <PARAM> with the getParameter() function.
For example, imagine an applet named ScrollText. As advertised, this applet can scroll a text message across the browser window. It’s stored in the file ScrollText.class and can take several parameters that customize its behavior:
In the above HTML example, you first specify the applet ScrollText.class, name it scroller, and set its dimensions. Then, having referred to the applet’s documentation, you set three parameters: text (with the message to scroll), font (with the font style to render the text), and speed (which controls the pace of the scroll). Remember that these parameters are individually programmed into each applet and are particular to an applet.
Java with Class
Classes are at the heart of what makes Java an object-oriented language. A class is like a library of objects, all of which inherit certain characteristics from the class itself. "Class" is a hierarchical concept: One class could be a subset of a higher-level class. In Java, there is an "abstract class," which is a parental class containing many child classes. In this case, though, the abstract class is never directly used. Rather, it is defined solely to provide the "genes" for its children classes.
If any of this seems confusing, that's because it is. That is why entire books are written about object-oriented programming and Java in particular. Now, though, at least you have a taste for what object-orientedness is about: modules that are part of larger modules, which are part of even larger modules.
C++ is a very common object-oriented programming language that also revolves around these class concepts. One major difference between C++ and Java, in terms of how they deal with classes, is that Java programs can more easily withstand changes in class definitions without breaking any programs based on the previous definitions. In C++, on the other hand, if you change any class definitions, every program written using the previous definitions must be recompiled. For this reason, Sun calls Java "dynamic."
Save this file as HelloWorld.java. Then, after you type that, you need to compile the applet. Get yourself to a DOS prompt (you'll need to open a DOS window to do this) and type the following:
Finally, you can run your new applet right from DOS by typing this:
Congratulations! You've just taken another big step: Java programming!
Command Line? I'm Running a Mac!
If you're running a Macintosh (or a PowerPC), you're probably a bit
confused by the reference to a
"command line" because the Mac operating system doesn't have one. Does this mean that Mac users are
out of luck when it comes to creating Java applets? Sadly, at this moment, the answer is "Yes—unless you're
willing to spend some money." There are no environments or library sets (source code) for Java compiling on
the Mac available for free off the Internet. Several companies (Symantec at http://www.symantec.com/ and
Metrowerks at http://www.metrowerks.com/ to name two) have announced that they will be integrating Java
applet creation into their compilers, but their products aren't available yet.
Natural Intelligence, however, does market a package called Roaster
that gives Mac users the ability to
compile and develop Java applets on the Mac from inside the Mac OS. For more information, check out
Natural's web page at http://www.natural.com/.
Because we haven’t delved very deeply into authoring Java source code, let’s just consider a relevant snippet of the scroller applet source:
This statement will launch the start() method in the applet named scroller on the current page.
Similarly, this applet also contains a public method named stop(). As you can imagine, the statement document.scroller.stop() would stop the applet’s execution.
To close this chapter, take a look at a small sample page that pulls together the LiveConnect principles you’ve learned. This page contains a text-scrolling applet, as well as two form buttons. By exploiting LiveConnect in onClick event handlers, these form buttons can be used to start and stop the scrolling message.
The Least You Need to Know
For comments or technical support for our books and software, select Talk to Us. © 1997, QUE Corporation, an imprint of Macmillan Publishing USA, a Simon & Schuster Company.
To order books, call us at 800-716-0044 or 317-228-4366.
© 1997, QUE Corporation, an imprint of Macmillan Publishing USA, a Simon & Schuster Company.