/** * Copyright (C) 1997 William Braynen * Contact information can be found at www.samdurak.com * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License * as published by the Free Software Foundation; either version 2 * of the License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * * Written by : William Braynen * Last modified: September 11, 1997 * Class name : Graph * *** * NOTE: display() updates the screen directly, * all other methods update the double-buffered image. * *** METHODS: *** * Graph() - public constructor (inherited from Canvas) * void setSize( int size ) * * void setGraphics( Graphics g ) - see NOTE below * void setImage( Image i ) - see NOTE below * NOTE: Because of a bug in JDK 1.1 image objects cannot be created inside an object class, * but can only be created inside the main applet (the driver program). * Because of this, the following lines must be present in the main applet: * image = createImage( SIZE, SIZE ); * graph.setImage( image ); * graph.setGraphics( image.getGraphics() ); * * void display() - displays the graph on the screen * void displayGrid() - displays the graph grid on the screen * void plot( ArrayI array, int yMAX_sorted ) - plot method exclusively for insertion sort: * plots values whose array position * is less than yMAX_sorted in green * (those that are sorted amongst themselves) * and thos whose array position is >= yMAX_sorted in blue * void plot( int x, int y, int yMAX, int color ) * 1 = blue (default) * 2 = red * 3 = green * 4 = lightGray * void plot( ArrayI array, int color, boolean presorted ) - used for displaying a presorted list as a background guideline * void plot( ArrayI array, boolean presorted ) * void plot( Graphics g, int x, int y ) - displays the point (x,y) on the graph grid * void plot( ArrayI array ) - displays the points ( array_values, array_index ) on the graph grid, * plotting array_values on the y-axis and array_index (position in the array) on the x-axis */ import java.awt.*; import java.applet.Applet; public class Graph extends Canvas { private int size = 200; private int xCoords[], yCoords[]; // double-buffering: Image image; // set by the driver applet thru setImage() Graphics graphics; // set by the driver applet thru setGraphics() // pre : image and graphics objects have been set // using setImage() and setGraphics() public void init() { xCoords = new int[ size + 1 ]; yCoords = new int[ size + 1 ]; translateCoordinates(); } // end init public void setSize( int size ) { this.size = size; init(); } // end setSize public void setGraphics( Graphics g ) { graphics = g; } // end setGraphics public void setImage( Image i ) { image = i; } // end setImage public void display() { paint( this.getGraphics() ); } // end display public void paint( Graphics g ) { g.drawImage( image, 0, 0, this ); } // end paint // clear the needed area of the screen private void clear() { graphics.setColor( Color.white ); graphics.fillRect( 0, 0, size, size ); } // end clear public void displayGrid() { clear(); /* Display the axis identifiers centered on their axis. * X-axis: "array index" (turned 0-degrees) * Y-axis: "array value" (turned 90-degrees) * * Create the X and Y numbering legends: * every 50 pixels, display the scale number * (e.g. in a 1:1 scale, display a multiple of 50) */ // Create X and Y 10 pixel lines in light gray graphics.setColor( Color.lightGray ); for (int y = 0; y <= size; y = y + 10) { graphics.drawLine( 1, y, size, y ); } // draw horizontal lines for (int x = 0; x <= size; x = x + 10) { graphics.drawLine( x, 1, x, size ); } // draw vertical lines // Create X and Y axis lines in black graphics.setColor( Color.black ); graphics.drawLine( 0, size, size, size ); // draw horizontal lines graphics.drawLine( 0, 0, 0, size ); // draw vertical lines } // end displayGrid /* plot method exclusively for insertion sort */ public void plot( ArrayI array, int yMAX_sorted ) { for (int index = 1; index < array.last(); index++) { int value = array.value( index ); if (index < yMAX_sorted) { plot( index, value, array.last(), 3 ); } // in green else { plot( index, value, array.last(), 1 ); } // in default blue } // end for } // end plot // colors: // 1 = blue // 2 = red // 3 = green // 4 = lightGray public void plot( int x, int y, int yMAX, int color ) { if (color == 1) graphics.setColor( Color.blue ); else if (color == 2) graphics.setColor( Color.red ); else if (color == 3) graphics.setColor( Color.green ); else if (color == 4) graphics.setColor( Color.lightGray ); if (x==0) x++; // this is to avoid if (x==1) x++; // run-time errors due to if (y==yMAX) y--; // ArrayIndexOutOfBounds Exception try { graphics.drawOval( xCoords[x-1], yCoords[y+1], 2, 2 ); graphics.drawLine( xCoords[x], yCoords[y], xCoords[x], yCoords[y]); } catch (ArrayIndexOutOfBoundsException exc) { // deBug graphics.drawString( "ArrayIndexOutOfBoundsException:", 300, 300 ); graphics.drawString( "x => " + String.valueOf( x-1 ) + ", y => " + String.valueOf( y+1 ), 300, 320 ); } // end catch } // end plot // x-axis: array index // y-axis: array values public void plot( ArrayI array, int color, boolean presorted ) { int value = 0; for (int index = 1; index < array.last(); index++) { if (presorted) value = array.sortedValue(index); else value = array.value(index); if ( !(presorted) && !(array.value(index)==array.sortedValue(index)) ) { plot( index, value, array.last(), 1 ); } // show, if not in place, in default blue else if (presorted) { plot( index, value, array.last(), 4 ); } // show presorted line in lightGray else { plot( index, value, array.last(), 2 ); } // show, if value is in place, in red } // end for } // end plot // parameter int dummy is for overloading public void plot( ArrayI array, int color, int dummy ) { for (int i = 1; i < array.last(); i++) plot( i, array.value(i), array.last(), color ); } // end plot public void plot( ArrayI array, boolean presorted ) { plot( array, 1, presorted ); } // end plot public void plot( ArrayI array ) { plot( array, 1, false ); } // end plot private void translateCoordinates() { for (int index = 1; index <= size; index++) { xCoords[index] = index; } for (int index = 1; index <= size; index++) { yCoords[index] = size - index; } } // end translateCoordinates } // end class Graph