import java.applet.*; import java.awt.*; import java.io.*; import java.net.*; public class ps2e3 extends Applet { private int canvasWidth, canvasHeight; private int cellWidth, cellHeight; private Button btn1; int xloc, yloc,oldxloc,oldyloc,bar1x,bar1y,bar2x,bar2y; int letterx,lettery, xindex,yindex,xoffset,yoffset; char curkey[],curchar[], selectedkey[]; char grid[][]; boolean horizontal = false; String blank, alphatable[]; private DataInputStream instream; public void init() { canvasWidth = 512; canvasHeight = 384; curkey = new char[1]; curchar = new char[1]; selectedkey = new char[1]; grid = new char[32][24]; blank = new String(" "); resize(canvasWidth, canvasHeight); alphatable = new String[27]; // initialize the grid with blanks for (int i=0;i<32;i++) { for (int j=0; j<24;j++) { grid[i][j] = blank.charAt(0); } } // this section reads in the code file try { URL CodefileName = new URL("http://acg.media.mit.edu/mas962/ps1/code.txt"); URLConnection codefile = CodefileName.openConnection(); instream = new DataInputStream(codefile.getInputStream()); for (int i = 0 ; i < 26 ; i++) { alphatable[i] = instream.readLine(); alphatable[i] = alphatable[i].substring(2); } } catch (IOException e); } public boolean keyDown(Event e, int key) { if (e.id == Event.KEY_PRESS) { selectedkey[0] = (char) key; if (xloc < 512 && yloc < 384) { for (int j = 0; j < 26; j++) { char firstchar = alphatable[j].charAt(0); int wordlength = alphatable[j].length(); if (selectedkey[0] == (firstchar)) { letterx = xloc; lettery = yloc; if ((horizontal == true) && (letterx + (wordlength*16) > 512)) break; if ((horizontal == false) && (lettery + (wordlength*16) > 384)) break; for (int k = 0; k < wordlength; k++) { curchar[0] = alphatable[j].charAt(k); Graphics g = getGraphics(); Font font1 = new Font("Courier",Font.PLAIN, 12); g.setFont(font1); g.setColor(Color.white); g.fillRect(letterx,(lettery - 16),16,16); g.setColor(Color.black); g.drawRect (letterx,(lettery - 16),16,16); g.drawChars(curchar,0,1,letterx+5,lettery-4); grid[letterx/16][lettery/16] = curchar[0]; if (horizontal == true) letterx = letterx + 16; else lettery = lettery + 16; } // horizontal = !horizontal; break; } } } } return true; } public boolean mouseMove(Event e, int x, int y) { Graphics g = getGraphics(); //erase previous cursor line g.setColor(Color.black); if (xloc < 512 && yloc < 384) g.drawLine(xloc,yloc,xloc+xoffset,yloc+yoffset); xloc = (x/16)*16; yloc = 16+(y/16)*16; //draw new cursor line g.setColor(Color.red); if (xloc < 512 && yloc < 384) g.drawLine(xloc,yloc,xloc+xoffset,yloc+yoffset); oldxloc = xloc; oldyloc = yloc; return true; } public boolean mouseDown(Event e, int x, int y) { Graphics g = getGraphics(); g.setColor(Color.black); g.drawLine(xloc,yloc,xloc+xoffset,yloc+yoffset); if (horizontal == true) { xoffset = 0; yoffset = -16; } else { xoffset = 16; yoffset = 0; } g.setColor(Color.red); g.drawLine(xloc,yloc,xloc+xoffset,yloc+yoffset); horizontal = !horizontal; return true; } public void paint(Graphics g) { Font font1 = new Font("Courier",Font.PLAIN, 24); g.setFont(font1); g.setColor(Color.black); g.fillRect(0, 0, canvasWidth+1, canvasHeight); } }