/** * @Homework: Assignment 6 (1 of 2) * @Exercise: 9.18 in textbook, page 534 * @Author: Richard Parry * @Version: 1.0 - August 3, 1998 * @Filename: GridLine.java * School: UCSD, Summer 98 Java 1 class * * Description: * This applet displays a grid that is 8 x 8. It can be easily * changed by modifying class constants. The grid starting point * (upper left) is fixed. A button is provided which allows one to * change height and width of each cell resulting in the gird size * changing. Random numbers are used for changing the cell * width and height. Each cell is created using the Graphics * class "drawLine" method. Note that the program does not draw * separate cells (4 individual line for each cell), but draws * lines and uses the intersection to create individual cells. * For example, an 8 x 8 grid is made by drawing 9 vertical and * 9 horizontal lines which results in 64 cells. **/ // import packages import java.applet.Applet; import java.awt.*; import java.awt.event.*; public class GridLine extends Applet implements ActionListener { // declare constants private final int X_START = 20; private final int Y_START = 40; private final int HORIZONTAL_CELLS = 8; private final int VERTICAL_CELLS = 8; private Button makeGridButton; // declare instance variables private int cellWidth = 15; private int cellHeight = 20; private int xCellSize, yCellSize, horizontalLines, verticalLines; private int horizontalCells, verticalCells; private int i, xpos, ypos, xBegin, yBegin, xEnd, yEnd; /** * This is the initialization method called once which sets * various parameters. It also configures the window with a * button that allows the user to modify the displayed grid. * * @param - none * @return - none **/ public void init() { // initialize default values horizontalLines = VERTICAL_CELLS + 1; verticalLines = HORIZONTAL_CELLS + 1; horizontalCells = HORIZONTAL_CELLS; verticalCells = VERTICAL_CELLS; // initialize the button makeGridButton = new Button( "Make Grid" ); makeGridButton.addActionListener( this ); add( makeGridButton ); } // end init method /** * This paint method displays the grid and the grid parameters * such as the number of cells, cell width and height, etc. * * @param - Graphics object * @return - none **/ public void paint ( Graphics g ) { xpos = 250; ypos = 100; g.drawString ( "Horizontal Cells = " + horizontalCells, xpos, ypos ); ypos += 15; g.drawString ( "Vertical Cells = " + verticalCells, xpos, ypos ); ypos += 15; g.drawString ( "Cell Width = " + cellWidth, xpos, ypos ); ypos += 15; g.drawString ( "Cell Height = " + cellHeight, xpos, ypos ); ypos += 15; // draw horizontal lines xBegin = X_START; yBegin = Y_START; xEnd = X_START + horizontalCells * cellWidth; yEnd = Y_START; for ( i = 0; i < horizontalLines; i++ ) { g.drawLine( xBegin, yBegin, xEnd, yEnd ); yBegin = yBegin + cellHeight; yEnd = yBegin; } // draw vertical lines xBegin = X_START; yBegin = Y_START; xEnd = X_START; yEnd = Y_START + verticalCells * cellHeight; for ( i = 0; i < verticalLines; i++ ) { g.drawLine( xBegin, yBegin, xEnd, yEnd ); xBegin = xBegin + cellWidth; xEnd = xBegin; } } // end paint method /** * This method is called each time the button is depressed * by the user. It changes the width and height of the * grid cells. * * @param - action event from the mouse button * @return - none **/ public void actionPerformed( ActionEvent e ) { cellWidth = 5 + (int) (Math.random() * 20); cellHeight = 5 + (int) (Math.random() * 20); repaint(); } // end actionPerformed method } // end class