// Chloe Chao // PeteNSally.java // Sync sound and text with two silly phrases import java.applet.*; import java.awt.*; import java.io.*; import java.lang.*; import java.util.Vector; import java.util.StringTokenizer; import java.net.*; public class PeteNSally extends Applet implements Runnable { AudioClip petey; AudioClip sally; Thread thread; // animation thread int sleepRate = 100; // sleep between redraw in milliseconds Button button; Choice choice; int PETER = 0; int SALLY = 1; ImageCanvas imageCanvas; Graphics offg; Image displayImage; int maxFrames=0; int currFrameNum=0; Vector alphabet; public static final int NW = 0; public static final int NE = 1; public static final int SE = 2; public static final int SW = 3; int UNIT = 4; int currx, curry; public void init() { alphabet = new Vector(); // Draw the gui. Panel p = new Panel(); p.setLayout(new GridLayout(0,2)); makeChoice(); p.add("West", choice); makeButton(); p.add("East", button); BorderLayout b = new BorderLayout(); setLayout(b); add( "North", p ); makeImageCanvas(); add( "South", imageCanvas ); // Create the image displayImage = createImage(size().width,size().height); offg = displayImage.getGraphics(); offg.setColor( Color.black ); imageCanvas.setImage(displayImage); imageCanvas.repaint(); //Read in the homemade font file. fetch("http://acg.media.mit.edu/mas962/users/cchao/ps8/soundfontcode.txt"); // Load up the two sounds. URL up = null; URL us = null; try { up = new URL(getDocumentBase(),"petey.au"); us = new URL(getDocumentBase(),"sally.au"); } catch (MalformedURLException e) {} petey = getAudioClip(up); sally = getAudioClip(us); // Start the applet thread. thread = new Thread( this ); thread.start(); } // init public void start() { if (thread != null) { thread.resume(); } } // start public void stop() { if (thread != null) { thread.suspend(); } } //stop public void destroy() { if(thread != null) { thread.stop(); } } // destroy public void run() { try { while( true ) { long start = System.currentTimeMillis(); offg = displayImage.getGraphics(); speak(); repaint(); // pause between frames (if it didn't take too long already)e long elapsed = System.currentTimeMillis() - start; long sleep = (long)sleepRate - elapsed; if (sleep > 0) { thread.sleep(sleep); } } } catch (InterruptedException e) {} } // run private void speak() { // Each sound less than a second. (1 sec=1000 milliseconds?) currFrameNum++; if( currFrameNum >= maxFrames ) { // The animation is over. maxFrames = 0; currFrameNum = 0; } else if( choice.getSelectedIndex()==PETER ) { switch( currFrameNum ) { //case 1: case 2: drawLetter('P', Color.black, UNIT*12, currx, curry); writeWord(new String("ETER")); break; case 7: drawLetter('P', Color.black, UNIT*12, currx, curry); writeWord(new String("IPER")); break; case 14: drawLetter('P', Color.black, UNIT*12, currx, curry); writeWord(new String("ICKED A")); break; case 18://18: drawLetter('P', Color.black, UNIT*12, currx, curry); writeWord(new String("ECK OF")); break; case 25://25: drawLetter('P', Color.black, UNIT*12, currx, curry); writeWord(new String("ICKLED")); break; case 29://29: drawLetter('P', Color.black, UNIT*12, currx, curry); writeWord(new String("EPPERS")); break; case 3: case 8: case 15: case 19: case 26: case 30: drawLetter('P', Color.gray, UNIT*12, currx, curry); break; default: offg.setColor(Color.white); offg.fillRect( 0, 0, this.size().width, this.size().height); break; } // switch }// else if peter else if( choice.getSelectedIndex()==SALLY ) { switch( currFrameNum ) { case 2: drawLetter('S', Color.black, UNIT*12, currx, curry); writeWord("ALLY"); break; case 9: drawLetter('S', Color.black, UNIT*12, currx, curry); writeWord("ELLS"); break; case 12: case 27: //offg.setColor(Color.white); //offg.fillRect( 0, 0, this.size().width, this.size().height); drawLetter('S', Color.black, UNIT*12, currx, curry); writeWord("EA"); break; case 15: drawLetter('=', Color.black, UNIT*12, currx, curry); writeWord(" ELLS"); break; case 24: case 25: writeWord("AT THE"); break; case 30: drawLetter('=', Color.black, UNIT*12, currx, curry); writeWord(" ORE"); break; case 3: case 10: case 13: case 28: drawLetter('S', Color.gray, UNIT*12, currx, curry); break; case 16: case 31: drawLetter('=', Color.gray, UNIT*12, currx, curry); break; default: offg.setColor(Color.white); offg.fillRect( 0, 0, this.size().width, this.size().height); break; }// switch }// else if sally } // speak private void writeWord(String s) { int xx = currx+(11*UNIT); int yy = curry; for( int i=0; i': case '?': case '@': this.width = u*19; break; default: this.width = u*12; break; } } public void addSerif( Polygon p ) { serifs.addElement(p); } public char getCode() { return this.code; } public void setColor(Color col) { this.color = col; } public Color getColor() { return this.color; } public Image getImage() { return this.i; } public void setSound( AudioClip audio ) { this.sound = audio; } public AudioClip getSound() { return this.sound; } public int getWidth() { return this.width; } } // CodeString