import java.applet.*; import java.awt.*; public class ps4e2a extends Applet implements Runnable { Thread animator = null; Button btn1 = new Button("expand"); Button btn2 = new Button("revert"); Label lab1 = new Label("enter characters to add: "); TextField tf1 = new TextField(10); String message = "teststring"; String str1 = "randomness"; String str2,str3; String strx; Image bufimage; Font font1; boolean expand,revert,lastone,prevstate; int str1w,str1h,str1d,str1a,str2w,str2h,str2d,str2a,str3w,str3h,str3d,str3a; int x1,x2,x3,y1,y2,y3,s1x,s2x,s3x,s1y,s2y,s3y,d1x,d1y,d2x,d2y,d3x,d3y; int state[] = new int[500]; int x = 10; int y = 40; int cstate = 1; int split,splitx,splity; public void init () { add(lab1); add(tf1); add(btn1); add(btn2); lastone = true; font1 = new Font("Serif",Font.PLAIN, 30); animator = new Thread(); for (int i = 0; i < str1.length();i++) state[i] = 1; x1 = 180; y1 = 200; y3 = 200; s1x = 180; s1y = 200; s2y = 200; s3y = 200; System.out.println("starting Bill's java appet"); } public boolean action( Event e, Object o ) { if (e.target == btn1 & tf1.getText().length() != 0) { expand = true; strx = tf1.getText(); animator = new Thread(this); animator.start(); } if (e.target == btn2) if(cstate > 1) { { revert = true; animator = new Thread(this); animator.start(); // cstate = cstate--; } } return true; } public void paint(Graphics g) { g.setColor(Color.white); g.fillRect(0,30,540,200); g.setColor(Color.black); g.setFont(font1); g.drawString(str1,x1,y1); if (lastone == false) { g.drawString(str2,x2,y2); g.drawString(str3,x3,y3); } } public void run() { int oldx,oldy; FontMetrics fm1 = this.getFontMetrics(font1); str1h = fm1.getHeight(); str1w = fm1.stringWidth(str1); str1d = fm1.getDescent(); str1a = fm1.getAscent(); if (expand == true) { System.out.println("x loop: " + strx); System.out.println("str1: " + str1); cstate++; lastone = false; System.out.println(" "); for (int i = 0;i < strx.length();i++) { split = (int)(Math.random() * str1.length()); for ( int j = str1.length(); j > split; j--) state[j] = state[j-1]; state[split] = cstate; str3 = str1.substring(split,str1.length()); str2 = strx.substring(i,i+1); str1 = str1.substring(0,split); x1 = s1x; x3 = s1x + fm1.stringWidth(str1); y1 = s1y; y2 = 50; y3 = s1y; s1x = s1x - (fm1.stringWidth(str2)/2); s2x = s1x + fm1.stringWidth(str1); x2 = s1x + fm1.stringWidth(str1); s3x = s1x + fm1.stringWidth(str1) + fm1.stringWidth(str2); s2y = 200; while (y2 != s2y) // while (x1 != s1x) { // Dimension d = this.size(); // bufimage = this.createImage(d.width,d.height); // if (x1 != s1x) x1--; // if (x2 != s2x & x2 < s2x) // x2 = x2+2; // else // x2 = x2 -2; if (x3 != s3x) x3++; if (y1 != s1y) y1++; if (y2 != s2y) y2 = y2 +6; if (y2 == s2y) { lastone = true; str1 = str1.concat(str2); str1 = str1.concat(str3); } if (y3 != s3y) y3++; Graphics g=getGraphics(); // Graphics g=bufimage.getGraphics(); paint(g); // g = this.getGraphics(); // g.drawImage(bufimage,0,0,this); lastone = false; try Thread.sleep(50); catch (InterruptedException e); } } expand = false; } if (revert == true) { prevstate = true; lastone = false; while (prevstate == true) { int k = 0; while(state[k] != cstate & k < str1.length())k++; if (k == str1.length() & state[k] != cstate) // if (k == str1.length()) { cstate = cstate -1; prevstate = false; } else { str3 = str1.substring(k+1,str1.length()); str2 = str1.substring(k,k+1); str1 = str1.substring(0,k); x1 = s1x; x3 = s1x + fm1.stringWidth(str1)+fm1.stringWidth(str2); y1 = s1y; y2 = s1y; y3 = s1y; s1x = s1x + (fm1.stringWidth(str2)/2); s2x = s1x + fm1.stringWidth(str1); x2 = s1x + fm1.stringWidth(str1); s3x = s1x + fm1.stringWidth(str1); s2y = 50; for ( int j = str1.length(); j > split; j--) state[j] = state[j-1]; while (y2 > 50) { // Dimension d = this.size(); // bufimage = this.createImage(d.width,d.height); // if (x1 != s1x) x1++; // if (x2 != s2x & x2 < s2x) // x2 = x2+2; // else // x2 = x2 -2; if (x3 != s3x) x3--; if (y1 != s1y) y1++; if (y2 != s2y) y2 = y2 -6; if (y2 == s2y) { lastone = true;; str1 = str1.concat(str3); for ( int m = k; m < str1.length(); m++) state[m] = state[m+1]; } if (y3 != s3y) y3--; Graphics g=getGraphics(); // Graphics g=bufimage.getGraphics(); paint(g); // g = this.getGraphics(); // g.drawImage(bufimage,0,0,this); lastone = false; try Thread.sleep(50); catch (InterruptedException e); } } } revert = false; } } }