Sample Applets Provided with the JDK

Let's consider several demonstration applets provided with the JDK. Each sample applet comes with its source code. Some programmers find it interesting to read this source code to learn new and exciting Java features.

The demonstration programs provided with the JDK are located in a directory called demo. For Windows, the default location of the JDK 5.0's demo directory is

 C:Program FilesJavajdk1.5.0demo

On UNIX/Linux/Mac OS X, the default location is the directory in which you install the JDK followed by jdk1.5.0/demofor example,

 /usr/local/jdk1.5.0/demo

For other platforms, there will be a similar directory (or folder) structure. This chapter assumes that the JDK is installed in C:Program FilesJavajdk1.5.0demo on Windows or in your home directory in ~/jdk1.5.0 on UNIX/Linux/Max OS X. You may need to update the locations specified here to reflect your chosen installation directory and disk drive, or a different version of the JDK.

If you are using a Java development tool that does not come with the Sun Java demos, you can download the JDK (with the demos) from the Sun Microsystems Java Web site

java.sun.com/j2se/5.0/

TicTacToe Applet

The TicTacToe demonstration applet allows you to play Tic-Tac-Toe against the computer. To execute this applet, open a command window and change directories to the JDK's demo directory.

The demo directory contains several subdirectories. You can list them by issuing the dir command on Windows or the ls command on UNIX/Linux/Max OS X. We discuss sample programs in the applets and jfc directories. The applets directory contains several demonstration applets. The jfc (Java Foundation Classes) directory contains applets and applications that demonstrate Java's graphics and GUI features.

Change directories to the applets directory and list its contents to see the directory names for the demonstration applets. Figure 20.1 provides a brief description of each sample applet. If your browser supports J2SE 5.0, you can test these applets by opening the site java.sun.com/j2se/5.0/docs/relnotes/demos.html in your browser and clicking the link to each applet. We will demonstrate three of these applets by using the appletviewer command in a command window.

Figure 20.1. The examples from the applets directory.

Example

Description

Animator

Performs one of four separate animations.

ArcTest

Demonstrates drawing arcs. You can interact with the applet to change attributes of the arc that is displayed.

BarChart

Draws a simple bar chart.

Blink

Displays blinking text in different colors.

CardTest

Demonstrates several GUI components and layouts.

Clock

Draws a clock with rotating hands, the current date and the current time. The clock updates once per second.

DitherTest

Demonstrates drawing with a graphics technique known as dithering that allows gradual transformation from one color to another.

DrawTest

Allows the user mouse to draw lines and points in different colors by dragging the mouse.

Fractal

Draws a fractal. Fractals typically require complex calculations to determine how they are displayed.

GraphicsTest

Draws shapes to illustrate graphics capabilities.

GraphLayout

Draws a graph consisting of many nodes (represented as rectangles) connected by lines. Drag a node to see the other nodes in the graph adjust on the screen and demonstrate complex graphical interactions.

ImageMap

Demonstrates an image with hot spots. Positioning the mouse pointer over certain areas of the image highlights the area and displays a message in the lower-left corner of the applet container window. Position over the mouth in the image to hear the applet say "hi."

JumpingBox

Moves a rectangle randomly around the screen. Try to catch it by clicking it with the mouse!

MoleculeViewer

Presents a three-dimensional view of several chemical molecules. Drag the mouse to view the molecule from different angles.

NervousText

Draws text that jumps around the applet.

SimpleGraph

Draws a complex curve.

SortDemo

Compares three sorting techniques. Sorting (described in Chapter 16) arranges information in orderlike alphabetizing words. When you execute this example from a command window, three appletviewer windows appear. When you execute this example in a browser, the three demos appear side-by-side. Click in each demo to start the sort. Note that the sorts all operate at different speeds.

SpreadSheet

Demonstrates a simple spreadsheet of rows and columns.

TicTacToe

Allows the user to play Tic-Tac-Toe against the computer.

WireFrame

Draws a three-dimensional shape as a wire frame. Drag the mouse to view the shape from different angles.

Change directories to subdirectory TicTacToe, where you will find the HTML document example1.html that is used to execute the applet. In the command window, type the command

 appletviewer example1.html

and press Enter. This executes the appletviewer applet container, which loads the HTML document example1.html specified as its command-line argument. The appletviewer determines from the document which applet to load and executes the applet. Figure 20.2 shows several screen captures of playing Tic-Tac-Toe with this applet.

Figure 20.2. TicTacToe applet sample execution.

(This item is displayed on page 962 in the print version)

You are player X. To interact with the applet, point the mouse at the square where you want to place an X and click the mouse button. The applet plays a sound and places an X in the square if the square is open. If the square is occupied, this is an invalid move and the applet plays a different sound indicating that you cannot make the specified move. After you make a valid move, the applet responds by making its own move.

To play again, click the appletviewer's Applet menu and select the Reload menu item (Fig. 20.3). To terminate the appletviewer, click the appletviewer's Applet menu and select the Quit menu item.

Figure 20.3. Applet menu in the appletviewer.

 

DrawTest Applet

The DrawTest applet allows you to draw lines and points in different colors. In the command window, change directories to directory applets, then to subdirectory DrawTest. You can move up the directory tree incrementally toward demo by issuing the command "cd .." in the command window. The DrawTest directory contains the example1.html document that is used to execute the applet. In the command window, type the command

 appletviewer example1.html

and press Enter. The appletviewer loads example1.html, determines from the document which applet to load and executes the applet. Figure 20.4 shows a screen capture after some lines and points have been drawn.

Figure 20.4. DrawTest applet sample execution.

(This item is displayed on page 963 in the print version)

By default the applet allows you to draw black lines by dragging the mouse across the applet. When you drag the mouse, note that the start point of the line always remains in the same place and the end point of the line follows the mouse pointer around the applet. The line is not permanent until you release the mouse button.

Select a color by clicking one of the radio buttons at the bottom of the applet. You can select from red, green, blue, pink, orange and black. Change the shape to draw from Lines to Points by selecting Points from the combo box. To start a new drawing, select Reload from the appletviewer's Applet menu.

Java2D Applet

The Java2D applet demonstrates many features of the Java 2D API (which we introduced in Chapter 12). Change directories to the jfc directory in the JDK's demo directory, then change to the Java2D directory. In the command window, type the command

 appletviewer Java2Demo.html

and press Enter. The appletviewer loads Java2Demo.html, determines from the document which applet to load and executes the applet. Figure 20.5 shows a screen capture of one of this applet's many demonstrations of Java's two-dimensional graphics capabilities.

Figure 20.5. Java2D applet sample execution.

(This item is displayed on page 964 in the print version)

At the top of the applet are tabs that look like file folders in a filing cabinet. This demo provides 12 tabs with Java 2D API features demonstrated on each tab. To change to a different part of the demo, simply click a different tab. Also, try changing the options in the upper-right corner of the applet. Some of these affect the speed with which the applet draws the graphics. For example, click the checkbox to the left of the word Anti-Aliasing to turn off anti-aliasing (a graphics technique for producing smoother on-screen graphics in which the edges of the graphic are blurred). When this feature is turned off, the animation speed increases for the animated shapes at the bottom of the demo (Fig. 20.5). This performance increase occurs because shapes that are not anti-aliased are less complex to draw.

Introduction to Computers, the Internet and the World Wide Web

Introduction to Java Applications

Introduction to Classes and Objects

Control Statements: Part I

Control Statements: Part 2

Methods: A Deeper Look

Arrays

Classes and Objects: A Deeper Look

Object-Oriented Programming: Inheritance

Object-Oriented Programming: Polymorphism

GUI Components: Part 1

Graphics and Java 2D™

Exception Handling

Files and Streams

Recursion

Searching and Sorting

Data Structures

Generics

Collections

Introduction to Java Applets

Multimedia: Applets and Applications

GUI Components: Part 2

Multithreading

Networking

Accessing Databases with JDBC

Servlets

JavaServer Pages (JSP)

Formatted Output

Strings, Characters and Regular Expressions

Appendix A. Operator Precedence Chart

Appendix B. ASCII Character Set

Appendix C. Keywords and Reserved Words

Appendix D. Primitive Types

Appendix E. (On CD) Number Systems

Appendix F. (On CD) Unicode®

Appendix G. Using the Java API Documentation

Appendix H. (On CD) Creating Documentation with javadoc

Appendix I. (On CD) Bit Manipulation

Appendix J. (On CD) ATM Case Study Code

Appendix K. (On CD) Labeled break and continue Statements

Appendix L. (On CD) UML 2: Additional Diagram Types

Appendix M. (On CD) Design Patterns

Appendix N. Using the Debugger

Inside Back Cover





Java(c) How to Program
Java How to Program (6th Edition) (How to Program (Deitel))
ISBN: 0131483986
EAN: 2147483647
Year: 2003
Pages: 615
Similar book on Amazon

Flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net