import java.applet.*; import java.awt.*; import java.net.*; import java.util.*; // Main Class for applet Type1 // //============================================================================== public class Type1 extends Applet implements Runnable { // THREAD SUPPORT: // m_Type1 is the Thread object for the applet //-------------------------------------------------------------------------- private Thread thread1 = null; private boolean running = true; String phrase1; String str[] = new String[25]; int y[] = new int[25]; char c_char; int x,n,i,speed,w,yc,c; FontMetrics fm; URL codebase; boolean sound, space; // Type1 Class Constructor //-------------------------------------------------------------------------- public Type1() { } //-------------------------------------------------------------------------- public void init() { codebase = getCodeBase(); sound = true; speed = Integer.parseInt(getParameter("speed")); phrase1 = getParameter("phrase"); String fontname = getParameter("fontname"); int s_sound = Integer.parseInt(getParameter("sound")); if ( s_sound != 1 ) { sound = false; } setBackground(Color.white); setForeground(Color.black); setFont(new Font(fontname, 0, 16)); fm = getFontMetrics(getFont()); yc = ( size().height - fm.getHeight() ); for ( int j=0; j<25; j++ ) { str[j] = " "; y[j] = yc; } c = 0; x = 5; w = 5; } // Type1 Paint Handler //-------------------------------------------------------------------------- public void paint(Graphics g) { for ( int j=0; j<=c; j++) { g.drawString(str[j], x, y[j]); } } //-------------------------------------------------------------------------- public void start() { if (thread1 == null) { thread1 = new Thread(this); thread1.start(); } } //-------------------------------------------------------------------------- public void stop() { if (thread1 != null) { thread1.stop(); thread1 = null; } } private void carriageReturn() { for ( int jj=0; jj <= c; jj++ ) { y[jj] = y[jj] - fm.getHeight() - 1; } phrase1 = phrase1.substring(i,n); w = 5; i=0; c++; if(sound) { try { thread1.sleep(50); } catch (Exception e) {} play(codebase, "bell.au"); try { thread1.sleep(500); } catch (Exception e) {} } } // THREAD SUPPORT //-------------------------------------------------------------------------- public void run() { i = 0; while (i<=n) { try { c_char = phrase1.charAt(i); n = phrase1.length(); str[c] = phrase1.substring(0,i); //--------------------------------------- if ( Character.isSpace(c_char) ) { space = true; int i_wl = phrase1.indexOf(32, i+1); i_wl = i_wl - i; i_wl = i_wl * fm.charWidth(c_char); int space_left = (size().width-5) - w; if (i_wl > space_left) { carriageReturn(); } } else { space = false; } //--------------------------------------- if ( new Character(c_char).charValue() == 36 ) { carriageReturn(); i++; } w += fm.charWidth(c_char); if (w>= (size().width - 5)) { carriageReturn(); } for ( int jjj=0; jjj<=c; jjj++ ) { if (str[jjj].startsWith("$") ) { str[jjj] = str[jjj].substring(1, str[jjj].length()); } if (str[jjj].startsWith(" ") ) { str[jjj] = str[jjj].trim(); } } if ( (sound) && (!space) ){ play(codebase, "key.au"); } if ( (sound) && (space) ) { play(codebase, "bar.au"); } repaint(); //---------- Adjust the Speed -------------------- //---------- So output is not constant ---------- Random random = new Random(); int adj_speed = random.nextInt(); adj_speed = Math.abs( (adj_speed % 150) + 50 ); adj_speed += speed; Thread.sleep(adj_speed); //--------------------------------------- i++; } catch (InterruptedException e) { // TODO: Place exception-handling code here in case an // InterruptedException is thrown by Thread.sleep(), // meaning that another thread has interrupted this one stop(); } } } // MOUSE SUPPORT: Stop what you're doing when clicked. //-------------------------------------------------------------------------- public boolean mouseDown(Event evt, int x, int y) { if ( (running) && (sound) ){ running = false; sound = false; thread1.suspend(); } else { running = true; thread1.resume(); } if ( (running) && (!sound) ) { running = false; sound = true; thread1.suspend(); } else { running = true; thread1.resume(); } if ( (running) && (sound) ) { showStatus("Applet Thread Running with Sound"); } if ( (running) && (!sound) ) { showStatus("Applet Thread Running with NO Sound"); } if (!running) { showStatus("Applet Thread Suspended"); } return true; } //-------------------------------------------------------------------------- public boolean mouseUp(Event evt, int x, int y) { // TODO: Place applet mouseUp code here return true; } //-------------------------------------------------------------------------- public boolean mouseEnter(Event evt, int x, int y) { // TODO: Place applet mouseEnter code here return true; } //-------------------------------------------------------------------------- public boolean mouseExit(Event evt, int x, int y) { // TODO: Place applet mouseExit code here return true; } //-------------------------------------------------------------------------- public String getAppletInfo() { return "Name: Type1\r\n" + "Author: Marcus Kazmierczak\r\n" + "Date: April 19th, 97\r\n" + "Created with Visual J++"; } } // END