//****************************************************************************** // problem2.java: Applet for MAS962 ps#2 // // Author: Ingeborg Endter 9/19/97 //****************************************************************************** import java.applet.*; import java.awt.*; //============================================================================== // Main Class for applet problem2 // //============================================================================== public class problem2 extends Applet { private int numRows = 24, // Set # of rows for the canvas here numCols = 80, // likewise, # of columns lastX = 0, lastY = 0, lastRow = 0, lastCol = 0, startX = 0, startY = 0, cellWidth = 0, cellHeight = 0, canvasWidth = 0, canvasHeight = 0; Font font= new Font("Courier", Font.PLAIN, 8); // and font public static int APPLETWIDTH = 540; public static int APPLETHEIGHT = 382; public void init() { resize(APPLETWIDTH, APPLETHEIGHT); setFont(font); cellWidth = APPLETWIDTH / numCols; cellHeight = APPLETHEIGHT / numRows; canvasWidth = cellWidth * numCols; canvasHeight = cellHeight * numRows; setBackground(Color.white); } // Place additional applet clean up code here. destroy() is called when // when you applet is terminating and being unloaded. //------------------------------------------------------------------------- public void destroy() { } // problem2 Paint Handler //-------------------------------------------------------------------------- public void paint(Graphics g) { g.setColor(Color.black); g.drawRect(0,0, canvasWidth+1, canvasHeight+1); } //-------------------------------------------------------------------------- public void clear(Graphics g) { g.setColor(Color.white); g.clearRect(1,1, canvasWidth-1, canvasHeight-1); } public int getFontAscent() { Graphics g = getGraphics(); FontMetrics fm = g.getFontMetrics(); int ascent = fm.getAscent(); return ascent; } // MOUSE SUPPORT: // The mouseEnter() method is called when the mouse first enters // this component. //-------------------------------------------------------------------------- public boolean mouseEnter(Event evt, int x, int y) { //Don't acknowledge any events outside the canvas area if (x < canvasWidth && y < canvasHeight) { lastX = x; lastY = y; } return true; } // The mouseMove() method is called when the mouse is moved inside // this component with the mouse button not pushed //-------------------------------------------------------------------------- public boolean mouseMove(Event evt, int x, int y) { //Don't acknowledge any events outside the canvas area if (x < canvasWidth && y < canvasHeight) { lastX = x; lastY = y; } return true; } public boolean keyDown(Event evt, int key) { // Based on the cell size... // compute which cell the mouse cursor is in; // row and col numbering begins at 0 lastCol = lastX / cellWidth; lastRow = lastY / cellHeight; // then compute the starting point for drawing in the cell startX = (lastCol * cellWidth); startY = (lastRow * cellHeight) + getFontAscent(); // draw the character at the baseline of the cell Graphics g = getGraphics(); char[] ca = {(char)key}; // Handle only letters a to z if (Character.isUpperCase(ca[0]) || Character.isLowerCase(ca[0])) g.drawChars(ca, 0, 1, startX, startY); // or numbers 0 to 9 else if (Character.isDigit(ca[0]) ) g.drawChars(ca, 0, 1, startX, startY); else if (key == 27) clear(g); return true; } } //end class problem2