//Chloe Chao //Parse a special homemade font file and allow for a noise factor. //Translate text (A-Z) from the textfield into homemade font import java.applet.*; import java.awt.*; import java.awt.image.*; import java.net.*; import java.util.*; import java.io.*; public class FontNoiseParser extends Applet implements Runnable { public void init() { UNIT = 4; LEFT_MARGIN = 6; TOP_MARGIN = 30; alphabet = new Vector(); toBeCoded = new String(); noiseFactor = 0; Panel p = new Panel(); p.setLayout(new GridLayout(0,3)); //Create the URL textfield and place in view. makeUrlField(); p.add("West", urlField); // Create the noise factor textfield. makeNoiseField(); p.add("Center", noiseField); //Create the button and place in view. makeButton(); p.add("East", button); // Use BorderLayout. BorderLayout b = new BorderLayout(); setLayout(b); add( "North", p ); //Create the display Image and place in view. makeImageCanvas(); add( "South", imageCanvas ); displayImage = createImage(size().width,size().height); offg = displayImage.getGraphics(); offg.setColor( Color.white ); imageCanvas.setImage(displayImage); imageCanvas.repaint(); //Read in the homemade font file. fetch("http://acg.media.mit.edu/mas962/users/cchao/ps5/fontcode.txt"); //fetch("file://C|/fontcode.txt"); // Start the applet thread. thread = new Thread( this ); thread.start(); } 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 ) { coded( noiseFactor-(theta), toBeCoded.toUpperCase() ); repaint(); if( INC ) theta+=(noiseFactor*0.1); else theta-=(noiseFactor*0.1); if( theta>=noiseFactor && INC) { INC = false; theta = noiseFactor; thread.sleep(5*sleepRate); } else if( theta<=0 && !INC ) { INC = true; theta = 0.0; thread.sleep(2*sleepRate); } else thread.sleep(sleepRate); } } catch (InterruptedException e) {} } public void update(Graphics g) { paint(g); } public void paint (Graphics g) { paintComponents(g); imageCanvas.setImage(displayImage); imageCanvas.repaint(); g.drawImage(displayImage, 0, 0, this); } public boolean action(Event e, Object arg) { if (e.target == button) { toBeCoded = urlField.getText(); Double tempDouble; if (noiseField.getText() != null) tempDouble = new Double(noiseField.getText()); else tempDouble = new Double(0.0); noiseFactor = tempDouble.doubleValue(); if( noiseFactor!=0 && noiseFactor!=1.0 ) noiseFactor = 1.0-tempDouble.doubleValue(); if( noiseFactor==1.0 ) noiseFactor=.05; offg = displayImage.getGraphics(); coded( noiseFactor, toBeCoded.toUpperCase() ); //This guarantees uppercase. return true; } return false; } public Image noiseImage(double bf, CodeString cs1, int gx, int gy) { int dim = UNIT*12; Image i2 = cs1.getImage(); Graphics g2 = i2.getGraphics(); g2.setColor( Color.white ); g2.fillRect( 0, 0, dim, dim ); if( bf==0 ) { g2.setColor( Color.black ); for( int i=0; i 1.0) noise = 1.0; //Reset drawing coordinates to upper-left and clear screen int currx, curry; currx = LEFT_MARGIN; curry = TOP_MARGIN; offg.setColor( Color.white ); offg.fillRect( 0, 0, this.size().width, this.size().height); //StringBuffer sb = new StringBuffer(); for( int i=0; i(size().width-(12*UNIT)) ) { currx = LEFT_MARGIN+(int)(((i%UNIT)*UNIT*10)*(noiseFactor-theta)); curry+=(12*UNIT); } }//end for each letter }// end coded() public void fetch (String address) { byte buf[] = null; Image img; Graphics gg; if (!alphabet.isEmpty()) alphabet.removeAllElements(); try { URL url = new URL(address); StreamTokenizer tokens = new StreamTokenizer(url.openStream()); int token; tokens.ordinaryChar(';'); // signifies end of commands tokens.ordinaryChar('|'); // signifies eol tokens.whitespaceChars( '(', ')' ); tokens.eolIsSignificant(true); tokens.commentChar('%'); // Get first token. token = tokens.nextToken(); // if (FILE_READABLE) { // While not eof while (token != tokens.TT_EOF) { // While not eol while ( token != tokens.TT_EOL ) { // Take the first character of the line as the letter. if( tokens.sval != null ) { String codestr = tokens.sval; char code = codestr.charAt(0); //System.out.println("code: "+code); Image ii=createImage(UNIT*12, UNIT*12 ); CodeString cs = new CodeString( code, UNIT, ii ); alphabet.addElement( cs ); img = cs.getImage(); gg = img.getGraphics(); gg.setColor( Color.black ); // Get the next token and start parsing for operators. // I don't have any error-checking. Sorry. //token = tokens.nextToken(); while( (token=tokens.nextToken())!='|' ) { if( token == tokens.TT_WORD ) { if (tokens.sval.equals( "serifs" )) { // Get the number of serifs to draw. //tokens.nextToken(); //int numOfSerifs = (int)tokens.nval; //for(int i=0; i