/* Slide Anagrams - given three input strings, have the strings' letters slide around to form each successive string. I based this very loosely on the NervousText demo provided by Sun, but I've changed it so much that only the way the thread is initialized and put to sleep is the only thing left from the original code. This is my first java program, so it's probably poorly written. Brian Stormont brian@stormyprods.com TODO: change the input method to get the list of strings from a URL That way there doesn't have to be a fixed number of strings. */ import java.awt.Graphics; import java.awt.Image; import java.awt.Color; import java.awt.Font; import java.awt.FontMetrics; public class slide_anagram extends java.applet.Applet implements Runnable { Image OSC = null; Graphics OSC_g = null; Font f = null; FontMetrics fm = null; char separated[]; char dest[]; int src_pos, dest_pos; int curr_pos_x[], curr_pos_y[], target[], dest_pos_x[], dest_pos_y[]; String s1 = null; String s2 = null; String s3 = null; String dest_str = null; String src_str = null; Thread killme = null; int i, move_count, y_move; boolean bi_move; int strlen; int x_coord = 0, y_coord = 0; boolean threadSuspended = false; int direction; public void init() { setBackground(Color.white); f = new Font("TimesRoman",Font.BOLD,15); fm = getFontMetrics(f); s1 = getParameter("text1"); if (s1 == null) { s1 = "hotjava"; } s2= getParameter("text2"); if (s2 == null) { s2 = "javahot"; } s3= getParameter("text3"); if (s3 == null) { s3 = "javahot"; } strlen = s1.length(); separated = new char [strlen]; dest = new char [strlen]; curr_pos_x = new int [strlen]; curr_pos_y = new int [strlen]; target = new int [strlen]; dest_pos_x = new int[strlen]; dest_pos_y = new int[strlen]; s1.getChars(0,strlen,separated,0); s2.getChars(0,strlen,dest,0); resize((strlen+1)*15, 45); OSC = createImage(size().width, size().height); OSC_g = OSC.getGraphics(); OSC_g.setFont(f); /* load the initial letter positions */ load_pos(separated, curr_pos_x, curr_pos_y); load_pos(dest, dest_pos_x, dest_pos_y); build_target(s1,s2); src_pos = 0; dest_pos = target[src_pos]; dest_str = s2; src_str = s1; move_count = 0; direction = 1; y_move = 0; bi_move = false; } public void load_pos (char s1[], int pos_x[], int pos_y[]) { for(i=0;i= strlen) { src_pos = 0; } dest_pos = target[src_pos]; } } // check if we have moved all the letters if (move_count >= strlen) { try {Thread.sleep(2000);} catch (InterruptedException e){} // get a new dest word src_str = dest_str; if (dest_str == s1) dest_str = s2; else if (dest_str == s2) dest_str = s3; else dest_str = s1; src_str.getChars(0,strlen,separated,0); dest_str.getChars(0,strlen,dest,0); /* load the initial letter positions */ load_pos(separated, curr_pos_x, curr_pos_y); load_pos(dest, dest_pos_x, dest_pos_y); build_target(src_str,dest_str); src_pos = 0; dest_pos = target[src_pos]; move_count = 0; y_move = 0; bi_move = false; } else { // TODO: put in the animation here... if (y_move < 10) { curr_pos_y[src_pos] += direction; if (bi_move) curr_pos_y[dest_pos] += direction; ++y_move; } else { if (curr_pos_x[src_pos] > dest_pos_x[dest_pos]) curr_pos_x[src_pos] -= 1; else curr_pos_x[src_pos] += 1; if (curr_pos_x[src_pos] == dest_pos_x[dest_pos]) { direction *= -1; y_move = 0; // if the target spot hasn't already been vacated, then // move that letter up or down as this one moves in if (target[dest_pos] != -1) bi_move = true; else bi_move = false; } } } } public void run() { while (killme != null) { try {Thread.sleep(5);} catch (InterruptedException e){} move_letter(); repaint(); } killme = null; } public void paint(Graphics g) { OSC_g.setColor(Color.white); // fill OSC with white OSC_g.fillRect(0,0,size().width,size().height); OSC_g.setColor(Color.black); // fill OSC with white for(i=0;i