import java.awt.*; import Parser; public class paintType extends java.applet.Applet { final int KERN = 1; final int SAFETY = 1; int col, dec, row; //column width, decent and row size (pixels) int x = 0, y = 0; boolean moved; Parser p; String buff = new String(""); char[] d = new char[1]; // this is a buffer for the keyboard character Font Helvetica = new Font ("Helvetica", Font.PLAIN, 12); Dimension offDimension; Image offImage; Graphics offGraphics; public void init() { d[0] = 0; repaint(); moved = false; p = new Parser(); p.start(); System.out.println("finished init"); } public void paint (Graphics g){ update(g); } public void update (Graphics g) { Dimension dime = size(); if ((offGraphics == null) || (dime.width != offDimension.width) || (dime.height != offDimension.height)) { offDimension = dime; offImage = createImage(dime.width, dime.height); offGraphics = offImage.getGraphics(); offGraphics.setColor(Color.white); offGraphics.fillRect(0,0,dime.width,dime.height); col = getFontMetrics(Helvetica).stringWidth("M") + 2*KERN; dec = getFontMetrics(Helvetica).getDescent(); row = getFontMetrics(Helvetica).getHeight() + dec + 2*SAFETY; System.out.println("cols: "+col); System.out.println("row: "+row); System.out.println("# cols: "+dime.width/col); System.out.println("# row: "+dime.height/row); } offGraphics.setFont(Helvetica); offGraphics.setColor(Color.white); offGraphics.fillRect(x,y-row+dec+SAFETY,col,row); offGraphics.setColor(Color.black); if(buff != null) offGraphics.drawString(buff,x+KERN,y+SAFETY); g.drawImage(offImage, 0, 0, this); //update the x and y according to the rules :) //x = x + col; } public boolean mouseMove (Event evt, int xclk, int yclk) { int xnew = (int)(xclk/col)*col; int ynew = (int)(yclk/row)*row; if(xnew != x || ynew != y) moved = true; y = ynew; x = xnew; d[0]=0; return false; } public boolean keyDown (Event evt, int key ) { d[0]=(char)key; buff = p.parse(new String(d)); repaint(); return true; } }