Java Source Code
Examples from the Text
This section contains the full Java source code for examples given in the text. You should be able to compile these files and use them. Note however that some of these examples depend on other classes, such as TextIO.class and MosaicFrame.class. To use examples that depend on other classes, you will need to compile the source code for the required classes and place the compiled classes in the same directory with the main class file. If you are using an integrated development environment such as Java Workshop or Visual J++, you can simply add any required the source code files to your project.
- GUIDemo.java, a simple GUI demo applet from Section 1.6. (You won't be able to understand the source code until you read Chapters 5 and 6.)
- Examples from Chapters 2 and 3 that use TextIO. These examples depend on TextIO.java. (Each of these examples appears in the text; they are provided here for convenience, in case you want to compile and run them.)
- Interest.java, from Section 2.2.
- Interest2.java, from Section 2.3.
- PrintSquare.java, from Section 2.4.
- Interest3.java, from Section 2.4.
- ThreeN1.java, from Section 2.5.
- GuessingGame.java, from Section 3.2.
- GuessingGame2.java, the improved guessing game from Section 3.2.
- ThreeN2.java, the improved "3N+1" program from Section 3.4.
- Applets from Chapters 2 and 3 that simulate console input/output. This is the source code for the applets that actually appear in the text. These applets simulate some of the sample programs in the preceding list. All of these console applets depend on ConsoleApplet.java, ConsolePanel.java, and ConsoleCanvas.java.
- ConsoleApplet.java, a basic class that does the HelloWorld program in Section 2.1. (The other console applets, below, are defined as subclasses of ConsoleApplet.)
- Interest1Console.java, the first investment example, from Section 2.2. Simulates Interest.java.
- Interest2Console.java, the second investment example, from Section 2.3. Simulates Interest2.java.
- Interest3Console.java, the third investment example, from Section 2.4. Simulates Interest.java.
- ThreeN1Console.java, the "3N+1" program from Section 2.5. Simulates ThreeN1.java
- GuessingGameConsole.java, the guessing game from Section 3.2. Simulates GuesingGame.java
- TheeN2Console.java, the improved 3N+1 program from Section 3.4. Simulates ThreeN2.java
- MovingRects.java, the sample animation applet from Section 2.6. (This depends on SimpleAnimationApplet.java.)
- RandomMosaicWalk.java, the program that displays a window full of colored squares with a moving disturbance, from Section 3.6. (This depends on MosaicCanvas.java and MosaicFrame.java.)
- ShapeDraw.java, the applet with dragable shapes, from Section 4.3. This file defines six classes. You won't be able to understand everything in this file until you've read Chapters 5 and 6.
- SymmetricMosaicFrame.java, the subclass of the MosaicFrame class, from Section 4.5. The SymmetricMosaicFrame class is used by SymmetricRandomWalk.java, the program for doing random walks symmetry that is described in Section 4.5. The program differs from RandomMosaicWalk.java in exactly one word. To run the symmetric random walk program, you need SymmetricRandomWalk.java, SymmetricMosaicFrame.java, and MosaicCanvas.java.
- HelloWorldApplet.java, the utterly basic first sample applet, from Section 5.1.
- ColoredHelloWorldApplet1.java, the first sample applet that uses a button, from Section 5.1.
- ColoredHelloWorldApplet2.java, an apple that introduces a simple layout, consisting of a drawing canvas with a bar of control buttons, from Section 5.2. This applet depends on ColoredHelloWorldCanvas.java.
- BlinkingHelloWorld1.java, an applet that blinks a message when the user clicks on a button, from Section 5.4. This is the first example of using a thread. This applet depends on ColoredHelloWorldCanvas.java.
- BlinkingHelloWorld2.java, an applet that blinks a message when the user clicks on a button and stops when the user clicks again, from Section 5.4. This is the first example of communication between two threads. This applet depends on ColoredHelloWorldCanvas.java.
- ScrollingHelloWorld.java, an applet that scrolls a message, from Section 5.4. This is the first example of using synchronization with the wait() and notify() methods ColoredHelloWorldCanvas.java.
- ScrollingMessage.java, an animation applet from Section 5.5. This applet uses an offscreen image to for double buffering.
- SimpleTrackMouse.java, which displays information about mouse events, from Section 6.2.
- TrivialSketch.java, an extremely simple sketching program that demonstrates dragging, from Section 6.2.
- KeyboardAndFocusDemo.java, which demonstrates key and focus events, from Section 6.2.
- EventDemo.java, which demonstrates various GUI components, from Section 6.3.
- ShapeDrawWithMenu.java, an extended version of ShapeDraw.java that uses a pop-up menu, from Section 6.3.
- RandomColorGrid.java, which uses nested and anonymous classes, from Section 6.4.
- ShapeDrawFrame.java, another version of ShapeDraw that uses a Frame, with a menu bar, instead of an applet. From Section 6.5. The ShapeDrawFrame class contains a main() routine and can be run as an application. The applet ShapeDrawLauncher.java, merely displays a button. When you click on the button, a ShapeDrawFrame window is opened.
- MessageDialog.java, a class for displaying modal dialogs that contain a message and one, two, or three buttons. From Section 6.5. The applet DialogDemoLauncher.java is a button that opens a frame that runs a little demo of the MessageDialog class.
- SortTest.java, a very simple program that tests the selection sort and insertion sort subroutines from Section 7.3.
- DateDemo.java, a tiny apple that demonstrates the Date class, from Section 7.5.
- ReverseFile.java, a program that reads a file of numbers and writes another file containing the same numbers in reverse order. From Section 8.3. This file depends on TextReader.java.
- ShapeDrawWithFiles.java, a final version of ShapeDraw.java that uses files to save and reload the designs created with the program. This version only runs as an independent program, not as an applet. It depends on the file MessageDialog.java. It is described at the end of Section 8.3.
- URLExampleApplet.java, an applet that reads data from a URL, from Section 8.4