Developing Professional Java Applets
by K.C. Hopson
Stephen E. Ingram
Chapter 1 The Java Development Environment
- Tough Problems in Search of One Solution
- General Features of the Java Programming Language
- Applets and Standalone Applications
- Applets Versus Standalone Applications
Chapter 2 Object-Oriented Development in Java
- Introduction to Java Classes and Objects
- Inheritance in Java
- Important Core Classes
- More about Classes
- Introduction to Exception Handling
- Organizing Projects in Java
- The Java Developer's Kit
Chapter 3 Building a Spreadsheet Applet
- Overview of AWT: Part 1
- Exception Classes
- The Throwable Class
- Exception Class Hierarchy
- Exception Handlers and Throwable Classes
- Writing Custom Exception Handlers
- Class Organization
- The Cell Class
- The Cell Container Class
- The FormulaParserClass
- The FormulaParserException Class
- The ArgValue Class
- The SpreadsheetCell Class
- The SpreadsheetContainer Class
- The SpreadsheetFrame Class
- The SpreadsheetApplet class
Chapter 4 Enhancing the Spreadsheet Applet
- Overview of AWT: Part 2
- I/O and Streams
- I/O and Security
- Adding Scrollbars
- Marking Cells
- Drawing Graphs
Chapter 6 Building a Catalog Applet
- Basics of the Applet Class
- Applets and HTML
- Applets and Images
- Applets and Audio
- Under the Applet Hood
- Creating and Reading a URL
- Chapter Project
Chapter 7 Java and Images
- Displaying Images
- Tracking Image Loading
- The Consumer/Producer Model
- Java Color Models
- Chapter Project: Displaying a Windows BMP Image
Chapter 8 Adding Threads to Applets
- What Is a Thread?
- Creating a Thread with the Thread Class
- The Runnable Interface
- More About Threads
- Talking Threads: Pipes and Threads
- Chapter Project
- The MediaLoaderThread and MediaObserver Classes
Chapter 9 Java Socket Programming
- An Introduction to Sockets
- Chapter Project: HTTP Server Application and Client Applet
Chapter 10 Native Methods and Java
- Deciding to Use Native Methods
- Native Methods from the Java Side
- Writing Native Methods
- Chapter Project: A Database Interface Library Using ODBC
- Database Server
- Adding Packet Assembly to DGTP
- Election Client
Chapter 11 Building a Live Data Applet
- Observers and the Model-View Paradigm
- Chapter Project
Chapter 12 Handling Dynamic Content
- Introducing the HotJava Browser
- Altering the HotJava Source
- Toward a More Perfect Server
- Creating New Content Types
Chapter 13 Animation and Image Filters
- Simple Animation Using Images
- Image Producers
- Filtering an Image
- Corporate Presentation Applet
Chapter 14 Advanced Image Processing
- Chapter Project
- Class Organization
- How It Works
- Fractals and the Mandelbrot Set
- Using the Applets
- The Mandelbrot Class
- CalculateFilterNotify Interface
- CalculatorProducer Interface
- The CalculatorFilter Class
- The CalculatorImage Class
- The MandelApp Class
- The MandelZoomApp Class
- The BmpImage Class
Appendix A Inside the Java Virtual Machine
- The Class File
- The Virtual Machine
- Primitive Types
- Local Variables
- Garbage Collection
B Language Grammar
To my wife, Anne, and my son, Mitchell; their love and encouragement gave me the strength to write this book.-Stephen Ingram
Copyright © 1996 by Sams.net Publishing
All rights reserved. No part of this book shall be reproduced, stored in a retrieval system, or transmitted by any means, electronic, mechanical, photocopying, recording, or otherwise, without written permission from the publisher. No patent liability is assumed with respect to the use of the information contained herein. Although every precaution has been taken in the preparation of this book, the publisher and author assume no responsibility for errors or omissions. Neither is any liability assumed for damages resulting from the use of the information contained herein. For information, address Sams.net Publishing, 201 W. 103rd St., Indianapolis, IN 46290.
All terms mentioned in this book that are known to be trademarks or service marks have been appropriately capitalized. Sams.net Publishing 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.
Java is a trademark of Sun Microsystems, Inc.
|President, Sams Publishing||Richard K. Swadley|
|Publishing Manager||Mark Taber|
|Managing Editor||Cindy Morrow|
|Marketing Manager||John Pierce|
|Assistant Marketing Manager||Kristina Perry|
|Acquisitions Editor||Mark Taber||Development Editor||Kelly Murdock|
|Software Development Specialist||Cari Skaggs||Production Editor||Lisa M. Lord|
|Copy Editors||Howard Jones, Anne Owen||Technical Reviewer||Greg Guntle|
|Editorial Coordinator||Bill Whitmer||Technical Edit Coordinator||Lynette Quinn|
|Formatter||Frank Sinclair||Editorial Assistant||Sharon Cox|
|Cover Designer||Tim Amrhein||Book Designer||Alyssa Yesh|
|Production Team Supervisor||Brad Chinn|
|Production||Mary Ann Abramson, Stephen Adams, Carol G. Bowers, Georgiana Briggs, Jeanne Clark, Jason Hand, Daniel Harris, Mike Henry, Clint Lahnen, Paula Lowell, Steph Mineart, Ryan Oldfather, Casey Price, Laura Robbins, Bobbi Satterfield, Ian Smith, SA Springer, Chris Wilcox|
We would like to thank our acquisitions editor, Mark Taber, our development editor, Kelly Murdock, and our production editor, Lisa Lord, for patiently guiding some lost sheep through their first book.
About the Authors
K.C. Hopson is president of Geist Software and Services, Inc., an independent consultant firm in the Baltimore/Washington D.C. metro area. However, K.C. thrives in cyberspace and enjoys using it anywhere in the world. He specializes in distributed computing solutions and has deep experience in GUI programming (especially Windows), relational databases, and client/server programming. K.C. was a lead architect of the software used in Bell Atlantic's Stargazer interactive television system and writes regularly about Java. K.C. has a B.S. in applied mathematics from the University of California-Irvine and a master's in computer science from the University of Maryland, Baltimore County. In his spare time, K.C. enjoys his family, worships music of all kinds, studies history, and loves literature.
Stephen E. Ingram is an computer consultant in the Washington D.C. metro area, specializing in embedded data communications and object-oriented design. He holds an electrical engineering degree from Virginia Tech and has been programming for 15 years. He was the architect behind the language of Bell Atlantic's Stargazer interactive television project; it was here that he first encountered Java. When he's not working, Steve likes to sail the Chesapeake Bay with his wife and son. Steve can be reached at firstname.lastname@example.org.
Java is frequently described as a programming language for the Internet. This reference mainly alludes to Java's ability to be executed as an applet, which is a program that runs inside a Java-enabled World Wide Web browser, such as Netscape Navigator 2.0 or HotJava. Applets naturally function as small programs that can enhance your Web page. This stems from Java's structure as a distributed language and its corresponding ability to load small pieces of code as needed. In the short run, programmers will use Java to add small snippets of code, such as animations, to enhance a Web page experience.
Java applets, however, are much more than small programs that supplement Web pages. Java is a sophisticated and extendable environment, providing the foundation for building industrial-strength applications. This power stems from Java's many facets-from its object-oriented nature to its simplicity to its ability to function in distributed environments. Consequently, to view Java as a "neat" tool for sprucing up your Web pages is a serious understatement of its capabilities. Java applets will appear in more guises than just the Internet, and Java's role inside internal networks-the so-called "Intranet"-is already being given serious consideration.
Developing Professional Java Applets aims to help bring Java applets out of the world of lightweight programs-like simple animations-into the world of practical programming. This book attempts to fill a gap that has been seriously lacking in currently existing Java literature: a book full of serious, practical, and professional Java projects and examples. The projects in this book illustrate how to create Java applets that solve real-life problems, and they do so in such a way that you will learn all the general features, as well as subtleties, of Java while you're working through practical applications of serious projects. In doing so, you will not only become a more capable Java programmer, but you might also develop insights on how to use Java to create a great new application.
Who Should Read This Book
Developing Professional Java Applets is intended for people who do not want to spend 200 pages learning basic Java (such as for loops), but want to quickly step into serious Java programming. To do this, however, requires you to have some background. In general, a basic background in programming and a rudimentary understanding of the principles of object-oriented programming should be enough. If you have any of the following credentials, then you are set:
- Have read and understood another book or literature on the basics of Java.
- Have some experience in C or C++.
If you are lacking these credentials, don't worry. Just take more time going through the first part of the book (an introduction to Java). If you have the background just described, however, this book will lead you deep into Java programming before you know it!
How This Book Is Organized
This book is based on the Java Development Kit (JDK), version 1.0. Most of the chapters of Developing Professional Java Applets have a tutorial section that covers some JDK topics, followed by a serious chapter project. The chapter project is accompanied by a discussion of the project's general architecture, as well as any Java subtleties it might uncover. By the time you are done with any given chapter, you will have been exposed to at least one serious Java programming concept.
Part I of the book is a quick overview of the fundamentals of Java programming. If you have no experience with Java, this part should be enough to get you ready for the heart of the book. It includes plenty of examples to illustrate key ideas. However, the book's focus is on practical Java programming, so some of the discussions of Java basics are terse. However, almost all the topics broached in Part I will reappear frequently throughout the rest of the book.
Parts II through IV are the heart of the book. Each of these parts walks through a serious project over the course of three chapters. By the end of each part, you will not only have seen the development of a major project, but will have attained some deep understanding of one or more crucial topics in Java programming.
In Part II, you'll see the development of a spreadsheet applet. This applet will guide you through the basics of Java's graphical toolkit, AWT. You will not only learn the general workings of AWT, but will also be exposed to some of its hidden gems, such as its Toolkit class. Since (at least for a while) most Java applets will use AWT as the basis for their user interfaces, a solid understanding of the package is indispensable. Part II also includes in-depth discussions of I/O streams in Java, as well as an exploration of Java's powerful approach to exception handling.
Part III uses a catalog-style applet to introduce you to the more advanced concepts of URL streams, threads, and image processing. This catalog applet shows how to tie information on one catalog page to that on another. The applet features a media loader that uses threads and advanced imaging concepts to bring in images that may be needed in the future; this is a "pre-loader" that can be used to reduce the latencies often associated with images. By the end of this part, you will have been through some of Java's most complex features.
Part IV takes you into the world of Java client/server programming. It introduces you to Java network programming through the creation of an HTTP server. This server will actually download Java applets to a client! You will also be shown how to use Java native methods-code written in C-by tying in databases (through the ODBC mechanism) to your server. Finally, you will see an applet that gets data from the server as it changes in real-time-what the book calls "live data." After this part of the book, you will have seen serious treatments of most of the major concepts in Java programming.
Part V lets you have a little break-but just a little one. Rather than one large project, each chapter has a smaller project. The focus is on more advanced uses of images and threads, but there is also a discussion of HotJava and its content-handling features. The goal of Part V is to further refine the knowledge you have gained in the first four parts of the book. By the time the book ends, you should feel that you have a solid understanding of everything needed to build high-quality, professional Java applets.
Thoughts Before Starting
There are a variety of Web sites cropping up that help you with various aspects of Java. The main launching pad for Java is http://www.javasoft.com/, which is where you can download the latest Java release and all kinds of first-rate documentation. The newsgroup comp.lang.java has excellent discussions of all aspects of Java programming, from novice to advanced. A couple of other Web sites, such as http://www.digitalfocus.com/ and http://www.gamelan.com/, offer good starting places for exploring Java in cyberspace.
Finally, it should be pointed out that the JDK 1.0 is not without bugs. An unfortunate side-effect of this is that some features that work on one platform might not work on another. If you are having isolated, inexplicable problems running any of the applets in this book, consider downloading the latest version of the JDK for your platform.