Java Source Code
THIS PAGE CONTAINS LINKS to the source code for examples appearing in the online text for CS 124.
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 CodeWarrior 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
This section contains the source code for the applets that are used as decorations at the end of each chapter. In general, you should not expect to be able to understand these applets at the time they occur in the text. Many of them use rather advanced techniques. By the time you finish the course, you should know enough to read the sources for these applets and hopefully learn something from them.
- Moire.java, an animated design, from end of Section 1.8. (You can use applet parameters to control various aspects of this applet's behavior. Also note that you can click on the applet and drag the pattern around by hand. See the source code for details.)
- JavaPops.java, and applet that shows multi-colored "Java!"s, from end of Section 2.8. (This depends on SimpleAnimationApplet.java.)
- RandomMosaicWalkApplet.java, showing a grid of randomly colored squares with a moving disturbance, from the end of Section 3.6. This is an applet implementation of the program RandomMosaicWalk.java. (This applet depends on MosaicCanvas.java.)
- SymmetricWalkApplet.java, showing a symmetric pattern of colored squares, from the end of Section 4.5. This file is a slight modification of RandomMosaicWalkApplet.java. (Depends on MosaicCanvas.java.)
- ScrollingMessage.java, from Section 5.5. This animation is also used as a coding example in the text, and it is listed above as well as here.
- TrackLines.java, an applet with lines that track the mouse, from Section 6.6. This applet uses Java 1.0 style event handling.
- Maze.java, an apple that creates a random maze and solves it, from Section 7.4.
- SimpleCA.java, a Cellular Automaton applet, from the end of Section 8.4. This applet depends on the file CACanvas.java.
- LittlePentominosApplet.java, the pentominos applet from the end of Section 9.3. This file defines two classes, LittlePentominosApplet and PentominosBoardCanvas. A pentomino is made up of five connected squares. This applet solves puzzles that involve filling a board with pentominos. If you click on the applet it will start a new puzzle. For more information see http://math.hws.edu/eck/xJava/PentominosSolver/ where you'll also find the big brother of this little applet. This applet uses the old-fashioned Java 1.0 style event-handling.
Required Auxiliary Files
This section lists many of the extra source files that are required by various examples in the previous sections, along with a description of each file. The files listed here are those which are general enough to be useful in other programming projects.
- TextIO.java which defines a class containing some static methods for doing input/output. These methods make it easier to use the standard input and output streams, System.in and System.out.
- ConsoleApplet.java, a class that can be used as a framework for writing applets that do console-style input/output. To write such an applet, you have to define a subclass of ConsoleApplet. See the source code for details. Several examples of applets created using ConsoleApplet are available above. Any project that uses this class also requires ConsolePanel.java and ConsoleCanvas.java.
- ConsolePanel.java, a support class that is required by any project that uses ConsoleApplet.
- ConsoleCanvas.java, a support class that is required by any project that uses ConsoleApplet or Console.
- Console.java, which defines a subclass, Console, of Frame that can be used for standalone applications that use console-style input/output. This class makes use of the ConsoleCanvas class, which is also used by the console-style applets in Chapters 2 and 3. This class is not used by any examples in the text, but all the examples that use TextIO can be converted to use Console instead. For information on how to do the conversion, see the example in ConsoleFrameThreeN1.java.
- SimpleAnimationApplet.java, a class that can be used as a framework for writing animated applets. To use the framework, you have to define a subclass of SimpleAnimationApplet. Section 2.6 explains how to do this.
- MosaicFrame.java which defines a subclass of Frame for implementing colored "mosaics," as in Section 3.6. (When you compile this file, you'll get two classes: MosaicFrame.class and MosaicFrame$1.class.) MosaicFrame.java depends on MosaicCanvas.java.
- MosaicCanvas.java, a subclass of the built-in Canvas class that implements a grid of colored squares.
- MessageDialog.java, a class for displaying modal dialogs that contain a message and one, two, or three buttons. From Section 6.5.
- TextReader.java, which contains the definition of the FilterReader discussed in Section 8.2.