//****************************************************************************** // problem3.java: Applet for MAS962 ps#2 // // Author: Ingeborg Endter 9/19/97 //****************************************************************************** import java.applet.*; import java.awt.*; import java.io.*; import java.net.*; //============================================================================== // Main Class for applet problem3 // //============================================================================== public class problem3 extends Applet { public static int APPLETWIDTH = 540; // Changes to font style and size public static int APPLETHEIGHT = 382; // and applet size should be made here Font font= new Font("Courier", Font.PLAIN, 8); private int numRows = 24, numCols = 80, lastX = 0, lastY = 0, lastRow = 0, lastCol = 0, startX = 0, startY = 0, cellWidth = 0, cellHeight = 0, canvasWidth = 0, canvasHeight = 0; //String filePath = "D:\\IEROOT\\MLCourses\\MAS962\\PS1\\problem3\\code.txt"; //***** TEST - local String filePath = "http://acg.media.mit.edu/mas962/ps1/code.txt"; //***** TEST - for web String replacement; public void init() { resize(APPLETWIDTH, APPLETHEIGHT); setFont(font); cellWidth = APPLETWIDTH / numCols; cellHeight = APPLETHEIGHT / numRows; canvasWidth = cellWidth * numCols; canvasHeight = cellHeight * numRows; } // Place additional applet clean up code here. destroy() is called when // when you applet is terminating and being unloaded. //------------------------------------------------------------------------- public void destroy() { } // problem3 Paint Handler //-------------------------------------------------------------------------- public void paint(Graphics g) { // Draw a rectangle around the canvas area g.setColor(Color.black); g.drawRect(0,0, canvasWidth+2, canvasHeight+2); } 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(); char[] ca = {(char)key}; // Handling letters A to z if (Character.isUpperCase(ca[0]) || Character.isLowerCase(ca[0])) { // Repace the chars with the values from the file replacement = SearchStream(filePath, ca[0]); // and paint them downward paintDown(replacement, lastRow, lastCol); } else if (Character.isDigit(ca[0]) ) //if it's a number { // Make a String Buffer of as many chars as the number StringBuffer nums = new StringBuffer(ca[0]); for (int x=0; x < Character.digit(ca[0], 10); x++) { nums.append(ca[0]); } // and paint it diagonally upward on the screen paintDiag(nums.toString(), lastRow, lastCol); } return true; } //****** // SearchStream: searches a file at some URL, which contains lines // consisting of a "key" (a character) and its replacement (a string). // The method reads the file a line at a time, searching the keys, // and returns the replacement string if a match is found. //****** public String SearchStream(String inFile, char ch) { String testString, returnString; int keyIndex = 0; //the key is at this index int replaceIndex = 2; //the replacement string is at this index URL webFile; //***** TEST - for web DataInputStream inStream; try { webFile = new URL(inFile); //***** TEST - for web inStream = new DataInputStream(webFile.openStream()); //***** TEST - for web //inStream = new DataInputStream(new FileInputStream(inFile)); //***** TEST - local testString = inStream.readLine(); //read first line while (testString != null) { if (testString.charAt(keyIndex) == Character.toLowerCase(ch)) { returnString = testString.substring(replaceIndex); inStream.close(); return returnString; } testString = inStream.readLine(); } //if we got here, the substring was not found System.out.println("Substring not found"); inStream.close(); } catch (MalformedURLException e) { System.err.println(e); } catch (IOException e) { System.err.println(e); } catch (StringIndexOutOfBoundsException e) { System.err.println(e); } return null; } //****** // paintDown: paints a string downward and wraps //****** public void paintDown(String addStr, int row, int col) { Graphics g = getGraphics(); try { // Step through each char in the string for (int i = 0; i < addStr.length(); i++) { char[] ca = {addStr.charAt(i)}; // If the target row is not past the bottom boundary // add the char to the current row and move down a row if (row < numRows-1) { row++; } else if (col +1 < numCols) { // Move to the right col++; } else if (row-1 >= 0) // Move up the rows { row--; } // Compute the starting point for drawing in the cell startX = (col * cellWidth); startY = (row * cellHeight) + getFontAscent(); g.drawChars(ca, 0, 1, startX, startY); } } catch (StringIndexOutOfBoundsException e) { System.err.println(e); } return; } //****** // paintDiag: paints a string diagonally upward and wraps //****** public void paintDiag(String addStr, int row, int col) { Graphics g = getGraphics(); try { // Step through each char in the string for (int i = 0; i < addStr.length(); i++) { char[] ca = {addStr.charAt(i)}; // If we're not at the right edge or the top if (col+1 < numCols && (row-1 > 0)) { // Move to the right and up col++; row--; } else if (row-1 <= 0 && col+1 < numCols) // If we're at the top { col++; // move right } else if (row-1 <= 0 && col-1 < numCols) // If we can't go right, go left { col--; } else if (col+1 >= numCols && (row-1 > 0)) // If we're at the right edge { row--; // move up } else if (col+1 >= numCols && (row-1 <= 0)) // If we can't go up, go down { row++; } // Compute the starting point for drawing in the cell startX = (col * cellWidth); startY = (row * cellHeight) + getFontAscent(); g.drawChars(ca, 0, 1, startX, startY); } } catch (StringIndexOutOfBoundsException e) { System.err.println(e); } return; } } //end class problem3