// Chloe Chao // Typegrid.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 Typegrid extends Applet implements Runnable { // Globals and constants TileCanvas tileCanvas; Image image; Point mouse; double tileDimx, tileDimy; Thread thread; public static final long sleepRate = 100; // sleep between redraw in milliseconds public static final int GRIDWIDTH = 80; public static final int GRIDHEIGHT = 24; public void init() { 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); // Start the applet thread. thread = new Thread( this ); thread.start(); }// end init() public void start() { requestFocus(); 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 paint(Graphics g) { update(g); } public void update( Graphics g ) { requestFocus(); paintComponents(g); tileCanvas.paint(g); g.drawImage(image, 0, 0, this); }// end paint() public boolean mouseMove( Event e, int x, int y ) { requestFocus(); mouse.move(e.x, e.y); tileCanvas.setCursor( e.x, e.y ); return true; } public boolean keyDown(Event e, int nKey) { 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; } return true; } /*public boolean handleEvent( Event e ) { switch( e.id ) { case Event.KEY_PRESS: 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; } //tileCanvas.repaint(); break; case Event.MOUSE_MOVE: mouse.move(e.x, e.y); tileCanvas.setCursor( e.x, e.y ); //repaint(); break; default: //repaint(); break; } //repaint(); 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); } }// end Typegrid /////////////////////////////////////////////////////////////// // Given the parameters, the TileCanvas figures out how big // each tile should be and then creates a 2D Vector of Tiles. // Tile Vector [0,0] is the upper left of the canvas. class TileCanvas extends Canvas { Vector tiles; double tileDimx, tileDimy; Point cursorPoint; int gridx; int gridy; int canvasWidth; int canvasHt; Image image; Graphics offg; int RED = 0; public TileCanvas( int grx, int gry, int canvasW, int canvasH, Image im ) { gridx = grx; gridy = gry; canvasWidth = canvasW; canvasHt = canvasH; image = im ; offg = im.getGraphics(); tiles = new Vector(); tileDimx = (double)canvasWidth/gridx; tileDimy = (double)canvasHt/gridy; cursorPoint = new Point( canvasW/2, canvasH/2 ); 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 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 void setCursor() { // this. 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; } }// end LetterTile