Java Examples.


Last updated on February 16, 2000.


This page is an index into all the Java examples used in MTHSC 983 in the Spring of 2000. Many of these examples were developed during earlier offerings of this course.

  1. Hello World version 1.
  2. Hello World version 2a.
  3. Hello World version 2b.
  4. Scribble.
  5. Hello World version 3a.
  6. Hello World version 3b.
  7. Digital Clock.
  8. Swirling Colors.
  9. Checker 1.
  10. Checker 2.
  11. Checker 3.
  12. First Shape Application.
  13. Second Shape Application.
  14. Simple Draw Applet.
  15. Swirly Clock.
  16. Thermometer Example.
  17. Thermostat Example.
  18. RPN Calculator 1.
  19. Simple Icon Test.
  20. Simple Test of Xlabels and Ylabels.

Hello World version 1.
This page displays the trivial applet with a little graphics pizzazz, including colors and fonts. It's a simple variation of the first example presented in Exploring Java by Niemeyer and Peck.

Hello World version 2a.
This page adds interactivity. The message, which is specified through the HTML param tag, can be dragged around the applet's panel. This version is based on the example by Niemeyer and Peck. It uses the Java 1.1 event model, and consequently it will not behave correctly with older browsers.

Hello World version 2b.
On this page the message, which is specified through the HTML param tag, can also be dragged around the applet's panel. This version uses the older Java 1.0 event model. Note that the applet does not register to receive mouse events. It just overrides the default method for handling such events.

Scribble.
This applet allows the user to scribble on the applet's panel with the mouse. It uses the 1.0 event model, which simply requires us to override the mouseDown and mouseDrag methods. Note that we can erase the window simply by passing another window over it. There is no internal model and no paint method which can be invoked by the system to redraw the display. Thus, there is no way to recreate the scribbling once something has passed in front of the window.

Hello World version 3a.
This page adds a button for additional interactivity. When the button is clicked the message changes color. This version is based on the example by Niemeyer and Peck. It uses the Java 1.1 event model, and consequently it will not behave correctly with older browsers.

Hello World version 3b.
This page provides the same functionality as version 3a, but this version uses the older Java 1.0 event model. Note that the applet does not register to receive mouse events. It just overrides the default method for handling such events.

Digital Clock.
This applet displays the date and time. It uses a thread so that it can update the display every second.

Swirling Colors.
This applet displays a message whose color continuously changes . It uses a thread so that it can update the display every 50 milliseconds.

Checker 1.
This first version of the checker animation introduces a thread for running the applet. Once the applet starts, the thread runs until the applet stops. The thread draws a black square, a white square, and a red circle. Then the thread sleeps for 100 milliseconds. Each time the thread awakes it adjusts the xpos and ypos so that the circle will move back and forth between the black box and the white box. Each time the update method is invoked the panel is erased and set to the background color, blue. The update method is the default method built into the applet, so it is not immediately obvious what is happening. However there is a very noticeable flicker.

Checker 2.
This second version of the checker animation introduces a clipping rectangle so that redrawing in only done inside the clipping region. We have to override the update method to achieve this, so we also eliminate the erasing step which sets the entire panel to the background color. This improves things a good deal, but there is still flickering inside the circle.

Checker 3.
This third and final version of the checker animation introduces "double buffering." When the applet is initialized we create an offscreen graphics object. All drawing is done to this object. Then we simply update the screen with the "g.drawImage(offscreenImg,0,0,this)" statement, which is executed very quickly.

First Shape Application.
This page displays the results of running the Java application, ShapeApp. This application includes five classes: ShapeApp, Shape, myRectangle, myEllipse, and mySquare.

ShapeApp is the application. First, it prints out the HTML header information. Then it performs 8 basic operations and prints out information after each operation.

  1. It creates a default rectangle, 'a', at (10,10).
  2. It creates a 15 by 30 rectangle, 'b', at (20,20).
  3. It moves rectangle 'b' to (75,25).
  4. It resets the size of rectangle 'a' to the size of rectangle 'b'.
  5. It creates a default ellipse, 'c', at (30,30).
  6. It resets 'a' to a 100 by 50 ellipse at (50,50).
  7. It sets 'd' to a default square at (40,40).
  8. It stretches 'd' into a 40 by 20 square(?).
Finally, it prints out the HTML trailer information.

Shape is an abstract class that defines the fields, methods, and behavior that will be common to all shapes. In particular, all shapes will have an x and y location, a width and height, and a color. All shapes will have the following methods: moveTo(), draw(), area(), and perimeter(). Shape includes a constructor which is used to initialize a shape. Since moving only requires changing the x and y coordinates of a shape, that method can be defined in the Shape class itself. On the other hand, drawing and computing areas and perimeters requires specific knowledge about the kind of shape, so these methods are declared "abstract" and their implementation is deferred to the subclasses.

myRectangle and myEllipse extend the class shape and provide specific methods for drawing, and for computing the area and perimeter. The constructors use the constructor of their superclass through the "super" keyword. The moveTo() method is inherited without change from the superclass, Shape. On the other hand, the draw(), area(), and perimeter() methods are overridden to provide the proper behavior. The toString() method adds the classname of the object to the general information returned by the superclass method.

mySquare extends the rectangle class. It simply adds new constructors to insure that it is a "square" rectangle. It also has a toString method that adjusts the result from the rectangle method.

Steps 4 and 8 illustrate that the fields of the shape objects are accessible outside the class and can be changed. In step 4 this doesn't appear to be a problem, but step 8 shows that accessible fields can lead to serious problems. In this case we can't insure that some outside part of the program won't destroy the squareness of our square. The next example will show how to protect fields from outside intervention.

The application, ShapeApp.class, is actually run on the math server when you request the page, and it prints out both the results and the html tags for dispaying the page. ShapeApp.cgi is a small shell file which actually runs the java application. Here is the text of ShapeApp.cgi.


#!/usr/bin/csh
setenv CLASSPATH .
setenv JAVA_HOME /usr/local/java
setenv LD_LIBRARY_PATH /usr/local/java/lib:/usr/local/lib
/usr/local/java/bin/java ShapeApp
This cgi interface is very dependent on the server you are running. This example is running on an Apache server in the Department of Mathematical Sciences.

Second Shape Application.
This page displays the results of running a second version of the Java application, ShapeApp. This version does the same things as the first version. The difference is in the packaging and the control of accessibility. Each class is defined with a separate source file, and the classes myRectangle and Shape declare themselves to be members of the ShapePack package.

The fields in Shape are declared protected, which means that they can only be accessed by classes in the same package or subclasses. Methods for retrieving and adjusting the field values then have to be added. However, this makes it possible to override the setWidth() and setHeigth() methods in the case of a square so that the constraint of "squareness" is never violated.

Simple Draw Applet.
This page displays an applet which uses the same shape classes demonstrated in the previous two examples. The applet includes three GUI elements: a choice object for selecting the shape, a choice object for selecting the color, and a clear button. The applet also makes use of the vector class for keeping track of the shapes.

Swirly Clock.
This page displays an applet which blends the Digtial Clock and Swirling Colors examples. The applet uses two threads. The first changes the color and the second changes the the date and time. The color changing thread sleeps for 50 milliseconds, while the date changing thread sleeps for an entire second.

Thermometer Example.
This page displays an applet which displays three different views of a single model. In this case the model generates "random temeratures" and the three view correspond to a text display in Fahrenheit, a text display in Celsius, and a visual thermometer display. This uses the original event model.

Thermostat Example.
This page extends the previous Thermometer example by adding a controller, so that we have a complete example of a Model, View, Controller system. In this case the controller is a thermometer-like indicator where we can set a temperature by clicking the mouse where we want the temperature to be. The model implements a simple "Newton cooling" whose rate of change is proportional to the difference between the current model temperature and the thermostat setting.

RPN Calculator 1.
This page displays an applet which acts as a Reverse Polish Notation calculator, similar to an HP-48. The stack and the command line are displayed as labels within a "stack display" panel, and the buttons are displayed within a "keypad" panel. Each of these two panels uses a grid layout manager (5 by 1 for the "stack display" panel, and 5 by 4 for the "keypad" panel. In turn the calculator is a panel with a 2 by 1 grid layout manager which displays the "stack display" panel and the "keypad" panel.

The calculator buttons are a CalculatorButton class designed by Sven.VanCaekenberghe.

Simple Icon Test.
This page displays a simple staircase drawing using four icon objects.
Simple Xlabel and Ylabel Test.
This page displays three Xlabels and three interspered Ylabels. Xlabel extends Label and only adds a default font and a getTextWidth() method. Ylabel extends canvas and provides the identical functionality - in a vertical direction. In order to get drawn properly, Ylabel must set its size so that the layout managers know how to lay it out. This example uses the default flow layout.