Java实现打字机效果源代码

2014-11-24 02:58:06 · 作者: · 浏览: 3

  import java.applet.*;


  import java.awt.*;


  import java.io.*;


  import java.net.URL;


  import java.util.*;


  public class Typewriter4 extends Applet implements Runnable


  {


  AudioClip hit, cr, ding ;


  boolean alreadyRun = false,


  soundOn = false,


  loop = false;


  Color bgColor = Color.lightGray,


  textColor = Color.black ;


  Font font ;


  Image offScreen, background ;


  int width, height,


  currentline = 0,


  currentword = 0,


  currentletter = 0,


  depth = 0,


  margin = 0,


  cycles = 0,


  step = 0,


  pause = 0,


  speed = 0,


  update = 0,


  linecount = 0 ;


  long lastread = 0;


  MediaTracker mt ;


  String soundactivation, text, textfile, target ;


  Thread woohoo = null;


  URL hotlink = null;


  Vector lines = null;


  public Typewriter4()


  {


  alreadyRun = false;


  soundOn = false;


  loop = true;


  soundactivation = "enter";


  }


  public void buildLines()


  {


  lines = new Vector();


  FontMetrics fontmetrics =


  offScreen.getGraphics().getFontMetrics();


  StringTokenizer strTok = new StringTokenizer(text, "\n") ;


  while (strTok.hasMoreTokens())


  {


  StringTokenizer strTok1 =


  new StringTokenizer(strTok.nextToken());


  int wordcount = strTok1.countTokens();


  String[] words = new String[wordcount];


  for(int i = 0; i < wordcount; i++)


  words[i] = strTok1.nextToken();


  String s = "" ;


  for(int j = 0; j < wordcount; j++)


  {


  s = s != null s + words[j] + " " : words[0];


  if(fontmetrics.stringWidth(s) > width - margin * 2)


  {


  lines.addElement(s.substring(0,


  s.lastIndexOf(" ", s.lastIndexOf(" ") - 1)));


  s = words[j] + " ";


  }


  }


  lines.addElement(s);


  linecount = lines.size();


  }


  depth = height - fontmetrics.getHeight() / 2;


  }


  public void checkTextfile()


  {


  loop = true;


  text = "";


  try


  {


  DataInputStream datainputstream = new


  DataInputStream((new URL(getDocumentBase(),


  textfile)).openStream());


  boolean flag = true;


  while(flag)


  {


  String s = datainputstream.readLine();


  if(s == null)


  flag = false;


  else


  text = text + s + "\n";


  }


  datainputstream.close();


  lastread =


  (new Long((new Date()).getTime())).longValue();


  return;


  }


  catch(Exception exception)


  {


  System.out.println("OOOHH--" + exception.toString());


  }


  }