Java Examples.


This page is an index into all the Java examples developed for MTHSC 865 in the Fall of 2000.

  1. Hello World version 1.
  2. Applet Test.
  3. Hello World version 2.
  4. Hello World version 3.
  5. First Shape Application.
  6. Second Shape Application.
  7. Simple Draw Applet.
  8. Matrix Demo using Pascal's Triangle.
  9. Checker 1.
  10. Checker 2.
  11. Checker 3.
  12. Coke Machine.

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.

Applet Test.
This page displays the previous Hello World applet but it also prints messages to the Java console which show the different situations in which the init(), start(), and paint() methods are called by the browser.

Hello World version 2.
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 3.
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.

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.

Matrix demo using Pascal's Triangle.
When it is initialized, this applet creates a triangular matrix and fills in the entries with the corresponding entries for Pascal's Triangle. The matrix is displayed in the paint method.

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.

Coke Machine.
This applet imitates a Coke Machine using a Finite State Machine, Technically, the machine in this applet is a Moore automaton.