// file: AppletGrid.java // author: Robert Keller // purpose: Extend class Grid for drawing in an applet. /** * Class AppletGrid provides an extension of Grid for the purpose of * drawing the Grid in an applet. It adds drawing commands to the basic * Grid class. */ import java.awt.*; class AppletGrid extends Grid { /** * Color used in applet to draw Grid */ public static Color gridColor = Color.red; /** * Color used in applet to draw spam */ public static Color spamColor = Color.blue; /** * Color used in applet to draw DESC end */ public static Color DESCendColor = Color.red; /** * Color used in applet to draw DESC body */ public static Color DESCbodyColor = Color.green; /** * Color used in applet to draw blockage */ public static Color blockColor = Color.blue; /** * Color used in applet to draw mark */ public static Color markedColor = Color.orange; /** * Construct a grid from a Sample, which includes the relevant size * information and spam settins. * * @param sample the Sample specifying Grid size and contents */ public AppletGrid(Sample sample) { super(sample); } /** * Draw the grid in the Graphics, at a specified pair offsets and * with a certain feature size. */ void draw(Graphics graphics, int xOffset, int yOffset, int size) { drawGrid(graphics, xOffset, yOffset, size); for( int row = 0; row < rows; row++ ) { int y = yOffset+row*size; for( int col = 0; col < cols; col++ ) { int x = xOffset + col*size; switch( get(row, col) ) { case SPAM: drawSpam(graphics, x, y, size); break; case DESChead: drawDESCendCell(graphics, x, y, size); break; case DESCbody: drawDESCbodyCell(graphics, x, y, size); break; case BLOCKAGE: drawBlockage(graphics, x, y, size); break; } } } } /** * Draw Grid at the specified position. */ void drawGrid(Graphics graphics, int xOffset, int yOffset, int size) { graphics.setColor(gridColor); int xLast = xOffset + cols*size; int yLast = yOffset + rows*size; graphics.drawLine(xOffset, yLast, xLast, yLast); graphics.drawLine(xLast, yOffset, xLast, yLast); for( int row = 0; row < rows; row++ ) { int y = yOffset+row*size; graphics.setColor(gridColor); graphics.drawLine(xOffset, y, xLast, y); for( int col = 0; col < cols; col++ ) { int x = xOffset + col*size; graphics.setColor(gridColor); graphics.drawLine(x, yOffset, x, yLast); } } } /** * Draw spam at the specified position. */ void drawSpam(Graphics graphics, int x, int y, int size) { graphics.setColor(spamColor); graphics.drawLine(x, y, x+size, y+size); graphics.drawLine(x+size, y, x, y+size); } /** * Draw blockage at the specified position. */ void drawBlockage(Graphics graphics, int x, int y, int size) { graphics.setColor(blockColor); graphics.fillRect(x, y, size, size); } /** * Draw a end (head or tail) cell of a DESC. */ void drawDESCendCell(Graphics graphics, int x, int y, int size) { graphics.setColor(DESCendColor); graphics.fillOval(x+1, y+1, size-2, size-2); } /** * Draw a body cell of a DESC. */ void drawDESCbodyCell(Graphics graphics, int x, int y, int size) { graphics.setColor(DESCbodyColor); graphics.fillOval(x+1, y+1, size-2, size-2); } }