Copyright© 1996 by Que® Corporation
All rights reserved. Printed in the United States of America. No part of this book may be used or reproduced in any form or by any means, or stored in a database or retrieval system, without prior written permission of the publisher except in the case of brief quotations embodied in critical articles and reviews. Making copies of any part of this book for any purpose other than your own personal use is a violation of United States copyright laws. For information, address Que Corporation, 201 W. 103rd Street, Indianapolis, IN 46290. You may reach Que's direct sales line by calling 1-800-428-5331.
This book is sold as is, without warranty of any kind, either express or implied, respecting the contents of this book, including but not limited to implied warranties for the book's quality, performance, merchantability, or fitness for any particular purpose. Neither Que Corporation nor its dealers or distributors shall be liable to the purchaser or any other person or entity with respect to any liability, loss, or damage caused or alleged to have been caused directly or indirectly by this book.
All terms mentioned in this book that are known to be trademarks or service marks have been appropriately capitalized. Que cannot attest to the accuracy of this information. Use of a term in this book should not be regarded as affecting the validity of any trademark or service mark.
|President and Publisher :||Roland Elgey|
|Associate Publisher :||Joseph B. Wikert|
|Director of Marketing :||Lynn E. Zingraf|
|Editorial Services Director||Elizabeth Keaffaber||Managing Editor||Sandy Doell|
|Title Manager||Bryan Gambrel||Acquisitions Editor||Fred Stone|
|Product Director||Ben Milstead||Production Editor||Mitzi Foster Gianakos|
|Assistant Product Marketing Manager||Kim Margolius|
|Technical Editors||David Medinets||Acquisitions Coordinator||Jane Brownlow|
|Operations Coordinator||Patty Brooks||Editorial Assistant||Andrea Duvall|
|Book Designer||Kim Scott||Cover Designer||Ruth Harvey|
|Production Team||Steve Adams, Marcia Brizendine, Jason Carr, Jenny Earhart, Joan Evan, Jessica Ford, Trey Frank, Amy Gornik, Jason Hand, Daniel Harris, Damon Jordan, Daryl Kessler, Clint Lahnen, Bob LaRoche, Kaylene Riemen, Laura Robbins, Bobbi Satterfield, Kelly Warner, Jeff Yesh, Jody York|
Clayton Walnum, who has a degree in computer science, has been writing about computers for almost 15 years and has published hundreds of articles in major computer publications. He is also the author of over 25 books, which cover such diverse topics as programming, computer gaming, and application programs. His most recent book is Windows 95 Game SDK Strategy Guide, also published by Que. His other titles include the award-winning Building Windows 95 Applications with Visual Basic (Que), 3-D Graphics Programming with OpenGL (Que), Borland C++ 4.x Tips, Tricks, and Traps (Que), Turbo C++ for Rookies (Que), Dungeons of Discovery (Que), PC Picasso: A Child's Computer Drawing Kit (Sams), Powermonger: The Official Strategy Guide (Prima), DataMania: A Child's Computer Organizer (Alpha Kids), Adventures in Artificial Life (Que), and C-manship Complete (Taylor Ridge Books). Mr. Walnum lives in Connecticut with his wife Lynn and their four children, Christopher, Justin, Stephen, and Caitlynn.
I would like to thank the following people for their contribution to this book: Joe Wikert for his confidence in my writing; Fred Slone for keeping everything running smoothly; Mitzi Gianakos, Anne Owen, and Joe Williams for keeping my abuse of the English language to a minimum; David Medinets for checking the facts; and all the other fine folks at Que. And, as always, thanks to my family-Lynn, Christopher, Justin, Stephen, and Caitlynn.
As part of our continuing effort to produce books of the highest possible quality, Que would like to hear your comments. To stay competitive, we really want you, as a computer book reader and user, to let us know what you like or dislike most about this book or other Que products.
You can mail comments, ideas, or suggestions for improving future editions to the address below, or send us a fax at (317) 581-4663. Our staff and authors are available for questions and comments through our Internet site at http://www.mcp.com/que., and Macmillan Computer Publishing also has a forum on CompuServe (type GO QUEBOOKS at any prompt).
In addition to exploring our forum, please feel free to contact me personally to discuss your opinions of this book: I'm [email protected] on the Internet, and 102121,1324 on CompuServe.
Thanks in advance-your comments will help us to continue publishing the best books available on computer topics in today's market.
201 W. 103rd Street
Indianapolis, Indiana 46290
What's so special about Java? Java enables programmers to create something called applets. Applets are special computer programs that can be included as an element of a Web page. When the user views a Web page containing one of these applets, the machine he's connected to automatically sends the applet to the user and the user's own Java-compatible browser runs the applet. Because applets are transferred in a non-machine-specific form, they can run on any machine that has a Java interpreter.
Using Java, you can do everything from adding simple animation to your Web pages to writing sophisticated computer programs that your Web page's users can use online. Applets that have already been released include games, spreadsheets, graphing programs, animation controllers, simulators, and much, much more. Java is so intriguing and so successful that even major players in the industry, including Netscape and Microsoft, have jumped aboard, providing Java-compatible software for the Internet.
In this book, you'll learn not only how Java applets work on the Internet, but also how to include Java applets in your Web pages. More importantly, you'll learn step-by-step how to write your own applets. You can write these applets for your own personal use, or write them for general release on the Internet. Imagine the thrill of seeing one of your own Java creations being used on Web pages all over the world!
This book is the perfect starting point for anyone wanting to learn from scratch about Java. Although it's helpful to have previous programming experience (especially with C or C++), this book includes a complete tutorial on the Java language and how to build applets with it. The Java tools, such as the compiler and interpreter, that you'll need to create your own applets are described in detail. Moreover, you'll learn the Java language starting from the very basics and working your way toward writing full-featured applets and applications.
Although this book is suitable for programming novices, more experienced programmers will find a great deal of interest here, as well. If you're already familiar with languages such as C and C++, you'll be able to skim over the Java language introduction and dive right into the business of creating applets. Although the Java language is very much like C++, the way it's used is unique. Up until Java, you've never seen anything quite like applets.
To summarize, this book is for both novice and intermediate programmers. Novice programmers will get a gentle introduction to the Java language, whereas more experienced programmers can concentrate on getting the most from the language by quickly learning how to build powerful applets for the Internet. Even expert programmers may find this book to be a useful introduction to the world of Java.
The Java language is currently supported on Windows 95, Windows NT, Sun Solaris, Macintosh, and UNIX machines. Most of this book's content is applicable to any type of computer that can run the Java Developers Kit. However, because Windows 95 will undoubtedly be the operating system under which the greatest majority of Java applets are created, the programs and examples in this book were written for the Windows 95 version of Java. Still, as long as you're familiar with your computer's operating system, you should have little difficulty following the examples in this book no matter what machine you use.
The minimum system requirements for Windows 95 or NT users are as follows:
* If you don't care about hearing sound files with Java's applets, you don't need a sound card.
The CD-ROM included with this book runs on Windows machines and includes the Windows versions of the Java Developers Kit and the HotJava Web browser. Users of other systems can get a copy of the Java Developers Kit for their machine from Sun Microsystems' Web site at http://www.sun.com.
As you work through the examples in this book, you'll learn to install the Java Developers Kit and to compile the example programs that are presented in each chapter. In general, though, you can compile the programs in this book by following the procedures given here.
First, you must have the Java Development Kit installed on your system, using the default root directory of C:\JAVA. It would also be useful to have a copy of Netscape Navigator 2.0 installed. You can get a copy of this Java-compatible browser from Netscape's Web site at http://www.netscape.com.
After installing the Java Development Kit, you must include the kit's path in your system's PATH statement. To do this, load your system files with SYSEDIT.EXE (you can find SYSEDIT.EXE in your WINDOWS\SYSTEM directory). When you start SYSEDIT, go to the AUTOEXEC.BAT window and find the PATH statement. At the end of the PATH statement, add a semicolon followed by the path C:\JAVA\BIN. Then, save the changes and restart your machine so the changes take effect. Adding the path to your PATH statement ensures that the system can find Java's tools.
Finally, you should create a directory called C:\CLASSES in which you will place the Java files you create throughout this book. To compile and run an applet's Java source-code file, follow these steps:
You can usually use the same HTML document for each applet just by changing the name of the applet in the document. Here is an example of a simple HTML document that will load and run an applet:
<applet code="filename.class" width=250 height=250> </applet>
In this HTML document, filename.class is the name of the compiled applet you want to run. Just change this file name for a new applet, and you're ready to go. You can also set the size of the applet when it runs by changing the values following the width and height parameters.
Running a Java stand-alone application is a little different. To compile the application, follow steps 1 through 3 above. To run the application type the command java filename, where filename is the name of the compiled Java application minus the .class file extension. This command line invokes the Java interpreter rather than the Appletviewer application.
As every programmer knows, a good program is virtually crash-proof. Error checking must be done for every action that may fail, and appropriate error messages must be given to the user. Unfortunately, good error checking requires a lot of extra program code. For the programmer working on his next magnum opus, this is all just part of the game. But for an author writing a programming book, this extra code has different implications.
A programming book should present its topics in as clear a manner as possible. This means featuring programs whose source code is not obscured by a lot of details that don't apply directly to the topic at hand. For this reason, the programs in this book do not always employ proper error checking. For example, user input often goes unverified and dynamic construction of objects is assumed to be successful.
In short, if you use any of the code in this book in your own programs, it's up to you to add whatever error checking may have been left out. Never assume anything in your programs. Any place in your code that you can't be 100 percent sure of your program's state, you must add error checking to ensure that the program doesn't come crashing down on your user. Just because this book's author may have been lax in his error checking (for good reasons), does not let you off the hook.
If you're still reading this introduction, you're probably convinced
that Java is something you really want to learn about. If you're
interested in the Internet, that decision is a wise one. (If,
on the other hand, you thought this was a book of coffee recipes,
return this book to the shelf and leave the store.) At this point,
Java is virtually guaranteed its place in Internet history. Want
to know why? Turn the page and keep reading.