/** * @Homework: Assignment 6 ( 2 of 2) * @Exercise: 9.19 in textbook, page 534 * @Author: Richard Parry * @Version: 1.0 - August 4, 1998 * @Filename: GridRect.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 "drawRect" method. The grid is created by drawing 8 * rectangles horizontally, moving down a row, drawing another * 8 rectangles, moving down, etc. until all 64 cells (rectangles) * are drawn giving us the desired grid. **/ // import packages import java.applet.Applet; import java.awt.*; import java.awt.event.*; public class GridRect extends Applet implements ActionListener { // declate constants private final int X_START = 20; private final int Y_START = 40; private final int HORIZONTAL_CELLS = 10; private final int VERTICAL_CELLS = 10; private Button makeGridButton; // declare instance variables private int cellWidth = 15; private int cellHeight = 20; private int horizontalCells, verticalCells, xCellSize, yCellSize; private int i, j, xpos, ypos, xBegin, yBegin; /** * 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 the button makeGridButton = new Button( "Make Grid" ); makeGridButton.addActionListener( this ); add( makeGridButton ); horizontalCells = HORIZONTAL_CELLS; verticalCells = VERTICAL_CELLS; } // end init method /** * This paint method displays 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 ) { Color fillColor = new Color( 200, 200, 200 ); Color outlineColor = new Color( 255, 0, 0 ); 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 rectangles xBegin = X_START; yBegin = Y_START; for ( j = 0; j < verticalCells; j++ ) { for ( i = 0; i < horizontalCells; i++ ) { g.setColor( fillColor ); g.fillRect( xBegin, yBegin, cellWidth, cellHeight ); g.setColor( outlineColor ); g.drawRect( xBegin, yBegin, cellWidth, cellHeight ); xBegin += cellWidth; } xBegin = X_START; yBegin += cellHeight; } } // 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