// Chloe Chao // Codegrid.java // September 19, 1997 // Like a TTY window, it should display an 80 by 24 matrix of // characters in a font that is fixed-width (i.e. non-proportionally // spaced). When the cursor is near one of the cells and typing // occurs, place the letter that is typed into that cell. Repeat for // wherever the cursor is, and whatever key is currently pressed, // place that character in the cell. import java.applet.*; import java.awt.*; import java.net.*; import java.util.*; import java.io.*; public class Codegrid extends Applet implements Runnable { // Globals and constants TileCanvas tileCanvas; Image image; Point mouse; double tileDimx, tileDimy; Thread thread; Vector strings; // table of codes StringBuffer keyBuffer; // stores keystrokes to be coded public static final int sleepRate = 100; // sleep between redraw in milliseconds public static final int GRIDWIDTH = 80; public static final int GRIDHEIGHT = 24; public void init() { strings = new Vector(); keyBuffer = new StringBuffer(); image = createImage( size().width, size().height ); tileCanvas = new TileCanvas(GRIDWIDTH, GRIDHEIGHT, size().width, size().height, this.image); mouse = new Point( size().width/2, size().height/2 ); tileDimx = (double)size().width/GRIDWIDTH; tileDimy = (double)size().height/GRIDHEIGHT; // Use the GridBagLayout layout to construct rows and columns. GridBagLayout gridbag = new GridBagLayout(); GridBagConstraints constraints = new GridBagConstraints(); setLayout(gridbag); constraints.anchor = GridBagConstraints.NORTH; constraints.weighty = 0; constraints.weightx = 1; addFormComponent(0, 0, 1, 1, gridbag, tileCanvas, constraints); // Read in the secret code file. fetch("http://acg.media.mit.edu/mas962/users/cchao/ps1/specialcode.txt"); // Start the applet thread. thread = new Thread( this ); thread.start(); }// end init() public void start() { if (thread != null) { thread.resume(); } } public void stop() { if (thread != null) { thread.suspend(); } } public void destroy() { if(thread != null) { thread.stop(); } } public void run() { try { while( true ) { repaint(); thread.sleep(sleepRate); } } catch (InterruptedException e) {} } public void update(Graphics g) { paint(g); } public void paint( Graphics g ) { requestFocus(); clearBuffer(); paintComponents(g); tileCanvas.paint(g); g.drawImage(this.image, 0, 0, this); }// end paint() public void clearBuffer() { StringBuffer blitToGrid = new StringBuffer( coded(keyBuffer.toString()) ); for( int i=0; i 0 ) keyBuffer.setLength(0); } public boolean handleEvent( Event e ) { switch( e.id ) { case Event.KEY_PRESS: keyBuffer.append((char)e.key); tileCanvas.fillTile( (char)e.key, mouse.x, mouse.y ); //System.out.println("Hit a key: "+(char)e.key+"\tat x: "+mouse.x+"\ty: "+mouse.y); // Move the cursor forward. double mx = (double)mouse.x+tileDimx; mouse.x = (int)mx; // To account for line wrapping. if( mouse.x>(GRIDWIDTH*tileDimx)) { mouse.x = 0; double my = (double)mouse.y+tileDimy; mouse.y = (int)my; if( mouse.y>(GRIDHEIGHT*tileDimy)) mouse.y = 0; } break; case Event.MOUSE_MOVE: mouse.move(e.x, e.y); tileCanvas.setCursor( e.x, e.y ); break; default: break; } return super.handleEvent(e); }// end handleEvent() private void addFormComponent(int x, int y, int w, int h, GridBagLayout grid, Component comp, GridBagConstraints c) { c.gridx = x; c.gridy = y; c.gridwidth = w; c.gridheight = h; grid.setConstraints(comp, c); add(comp); } public String coded( String codeme ) { StringBuffer sb = new StringBuffer(); for( int i=0; i20 ) RED=0; } RED++; // draw the cursor offg.fillRect( cursorPoint.x, (int)(cursorPoint.y-tileDimy), (int)(tileDimx/2), (int)(tileDimy) ); offg.setColor( Color.white ); // draw the letters for( int i = 0; i=gridx ) colval = 0; if( rowval>=gridy ) rowval = 0; LetterTile lt = getTile( rowval, colval ); lt.setChar( ch ); LetterTile cursor; if( (cursor = getTile( rowval, colval+1 )) != null ) {} else if( (cursor = getTile( rowval+1, 1 )) != null ) {} else { cursor = getTile( 1, 1 ); } cursorPoint = cursor.getPos(); } /* public void clearTile( int x, int y ) { int colval = (int)(x/tileDimx); int rowval = (int)(y/tileDimy); //System.out.println("gridx: "+colval+" gridy: "+rowval ); if( colval>=gridx ) colval = 0; if( rowval>=gridy ) rowval = 0; LetterTile lt = getTile( rowval, colval ); } */ public void setCursor( int x, int y ) { // We're assuming that x,y are in real coords. // We then have to convert it to grid coords. int colval = (int)(x/tileDimx); int rowval = (int)(y/tileDimy); LetterTile cursor; if( (cursor = getTile( rowval, colval )) != null ) {} else if( (cursor = getTile( rowval+1, 1 )) != null ) {} else { cursor = getTile( gridy, gridx ); } cursorPoint = cursor.getPos(); } }// end TileCanvas ////////////////////////////////////////////////////////////////// // The LetterTile is merely an Object that holds the point where // the character should be drawn. class LetterTile extends Object { StringBuffer c; Point pos; public LetterTile( char l, Point p ) { this.c = new StringBuffer(l); this.pos = p; } public LetterTile( Point p ) { this.c = new StringBuffer(); this.pos = p; } public void setChar( char ch ) { this.c.setLength(0); // clear the buffer this.c.append(ch); } public String getString() { return this.c.toString(); } public Point getPos() { return this.pos; } public boolean notEmpty() { if( c.length()>0 ) return true; else return false; } public void clearTile() { this.c.setLength(0); // clear the buffer } }// end LetterTile class CodeString extends Object { char code; String codestring; public CodeString( char code, String buffer ) { this.code = code; this.codestring = new String(buffer.toString()); } }