// 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; /** * 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.getRows(), sample.getCols()); setSpam(sample.elements()); } /** * Set spam on Pairs in an enumeration of Pairs. * * @param pairs the enumeration of Pairs given */ public void setSpam(java.util.Enumeration pairs) { while( pairs.hasMoreElements() ) { setSpam((Pair)pairs.nextElement()); } } /** * 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) { for( int row = 0; row < rows; row++ ) { for( int col = 0; col < cols; col++ ) { int x = xOffset+col*size; int y = yOffset+row*size; graphics.setColor(gridColor); graphics.drawRect(x, y, size, 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; } } } } /** * 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 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); } }