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());
}
}
public void init()
{
mt = new mediatracker(this);
lastread = 0l;
width = getsize().width;
height = getsize().height;
offscreen = createimage(width, height);
string param ;
if ((param = getparameter("background")) != null)
{
try
{
background = getimage(new url(getdocumentbase(),
param));
}
catch(exception e) { }
if(background != null) mt.addimage(background, 0);
}
if ((param = getparameter("bgcolor")) != null)
bgcolor = new color(integer.parseint(param, 16)) ;
if ((param = getparameter("textcolor")) != null)
textcolor = new color(integer.parseint(param, 16)) ;
string fontname = "helvetica" ;
if ((param = getparameter("fontname")) != null)
fontname = param ;
int fontsize = 12 ;
if ((param = getparameter("fontsize")) != null)
fontsize = integer.parseint(param) ;
int fontstyle = font.plain ;
if ((param = getparameter("fontstyle")) != null)
{
param = param.touppercase() ;
if (param.indexof("bold") != -1)
fontstyle |= font.bold ;
if (param.indexof("italic") != -1)
fontstyle |= font.italic ;
}
font = new font(fontname, fontstyle, fontsize);
param = getparameter("cycles");
if(param == null || param.equalsignorecase("infinite"))
{
cycles = 1;
step = 0;
}
else
{
cycles = integer.parseint(param);
step = 1;
}
param = getparameter("margin");
margin = param == null ?
width / 10 : integer.parseint(param);
param = getparameter("pause");
pause = param == null ? 2000 : integer.parseint(param);
param = getparameter("soundactivation");
soundactivation = param == null ?
"enter" : param.tolowercase();
soundon = soundactivation.equals("auto");
if((param = getparameter("sound.keystroke")) != null)
try
{
hit = getaudioclip(new url(getdocumentbase(),
param));
}
catch(exception e) { }
if((param = getparameter("sound.return")) != null)
try
{
cr = getaudioclip(new url(getdocumentbase(),
param));
}
catch(exception e) { }
if((param = getparameter("sound.bell")) != null)
try
{
ding = getaudioclip(new url(getdocumentbase(),
param));
}
catch(exception _ex) { }
param = getparameter("speed");
speed = param == null ?
100 : math.max(10, integer.parseint(param));
param = getparameter("target");
target = param == null ? "_self" : param;
if((param = getparameter("url")) != null)
try
{
hotlink = new url(getdocumentbase(), param);
}
catch(exception e) { }
param = getparameter("text");
text = param == null ?
"this is a test… \nthis is a test…" : param;
text = text.replace(\\, \n);
textfile = getparameter("textfile") ;
param = getparameter("update");
update = param == null ? 15 : integer.parseint(param);
buildlines();
try
{
mt.waitforid(0);
return;
}
catch(interruptedexception _ex) { return; }
}
public void run()
{
currentline = 0;
for(int i = 0; i < cycles; i += step)
{
long l = (new long((new date()).gettime())).longvalue();
if(l – lastread > (long)(update * 60000)
&& textfile != null)
{
checktextfile();
buildlines();
}
for(int j = 0; j < linecount; j++)
{
currentletter = 1;
string s = (string)lines.elementat(j);
for(int k = 0; k < s.length(); k++)
{
if(soundon && hit != null)
hit.play();
if(k == s.length() && soundon && cr != null)
cr.play();
repaint();
int i1 = 75 + (int)(math.random() * 100d);
try
{
thread.sleep((i1 * speed) / 100);
}
catch(interruptedexception interruptedexception)
{
system.out.println("bb: "
+ interruptedexception.tostring());
}
currentletter++;
}
currentletter = 0;
currentline++;
alreadyrun = false;
}
currentline = currentline % linecount;
try
{
thread.sleep(pause);
}
catch(interruptedexception interruptedexception)
{
system.out.println("aa: " +
interruptedexception.tostring());
}
}
}
public void paintbuffer(graphics g)
{
if(background != null)
{
g.drawimage(background, 0, 0, this);
}
else
{
g.setcolor(bgcolor);
g.fillrect(0, 0, width, height);
}
g.setcolor(textcolor);
g.setfont(font);
fontmetrics fontmetrics = g.getfontmetrics();
for(int i = 0; i < currentline; i++)
g.drawstring(
(string)lines.elementat(currentline – i – 1),
margin, depth – (i + 1) * fontmetrics.getheight()
);
string s = (string)lines.elementat(currentline);
string s1 = currentletter >= s.length() ?
s : s.substring(0, currentletter);
if(fontmetrics.stringwidth(s1) > (8 * width) / 10
&& !alreadyrun && soundon)
{
alreadyrun = true;
if(ding != null) ding.play();
}
g.drawstring(s1, margin, depth);
}
public void paint(graphics g)
{
paintbuffer(offscreen.getgraphics());
g.drawimage(offscreen, 0, 0, this);
}
public void update(graphics g)
{
paint(g);
}
public void start()
{
if(woohoo == null)
{
woohoo = new thread(this);
woohoo.start();
}
}
public void stop()
{
woohoo.stop();
woohoo = null;
}
public boolean mouseenter(event event, int i, int j)
{
if(hotlink != null) showstatus("link to " + hotlink.tostring());
if(soundactivation.equals("enter")) soundon = true;
return true;
}
public boolean mouseexit(event event, int i, int j)
{
if(soundactivation.equals("enter")) soundon = false;
return true;
}
public boolean mousedown(event event, int i, int j)
{
try
{
getappletcontext().showdocument(hotlink, target);
}
catch(exception e) { }
return true;
}
}
