//
// CHAPTER 9
// Applet Source Name [StringSway.java]
// 1996.2
// Author [ Junzo hagimoto ]
import java.applet.*;
import java.awt.*;
public class StringSway extends Applet implements Runnable {
Dimension d;
String s[];
Thread kicker = null;
int xScroll;
Font font;
int strHeight,strWidth;
int maxText;
int curTextCount=0 ;
int red,blue,green;
Color color;
int speed;
Image offs;
Graphics grf; //
char separated[]; // ADD
int charWidth; // ADD
public void init() {
d = size();
xScroll = d.width;
offs = createImage( d.width, d.height);
grf = offs.getGraphics();
String param = getParameter( "speed" );
speed = (param != null) ? Integer.parseInt(param) : 1;
if(speed < 1 || speed > 5 ){
speed = 1;
}
param = getParameter( "maxText" );
maxText = (param != null) ? Integer.parseInt(param) : 1;
s = new String[maxText];
int i=0;
do{
param = getParameter( "text"+ (i+1) );
if(param != null ){
s[i] = new String(param);
}else{
if( i==0 ){
s[i] = "Java";
}
maxText = i+1;
}
}while( param != null && ++i != maxText );
font = new Font("TimesRoman",Font.BOLD,20);
strHeight =(getFontMetrics(font)).getLeading() ;
strHeight -=(getFontMetrics(font)).getDescent() ;
strHeight +=(getFontMetrics(font)).getAscent() ;
}
public void run() {
Thread.currentThread().setPriority(Thread.NORM_PRIORITY-3);
while (kicker != null) {
repaint();
try {
Thread.sleep( 20 );
} catch (InterruptedException e) {}
}
kicker=null;
}
public void update(Graphics g) {
paint(g);
}
public void paint(Graphics g) {
grf.setFont( font );
grf.setColor(Color.black);
grf.fillRect(0, 0, d.width, d.height);
if(xScroll == d.width){
red = (int)(Math.random() * 256);
blue = (int)(Math.random() * 256);
green = (int)(Math.random() * 256);
color = new Color( red, green, blue);
curTextCount++;
if(curTextCount > maxText){
curTextCount=1;
}
strWidth = (getFontMetrics(font)).stringWidth(s[curTextCount-1]);
//
separated = new char [ s[curTextCount-1].length() ]; // ADD
s[curTextCount-1].getChars(0, // ADD
s[curTextCount-1].length(),separated,0); // ADD
}
grf.setColor(color);
xScroll -= speed;
//
int _xOffset=0; // ADD
for(int i=0;i < s[curTextCount-1].length();i++){ // ADD>
int _y= (int) (Math.random()* 5); // ADD
int _x= (int) (Math.random()* 5); // ADD
_xOffset += (i != 0)? // ADD
(getFontMetrics(font)).charWidth(separated[i-1]) : 0; // ADD
grf.drawChars(separated, i,1 ,xScroll + _x + _xOffset, // ADD
d.height/2 +strHeight/2 + _y ); // ADD
}
// DEL grf.drawString(s[curTextCount-1],
// DEL xScroll,d.height/2 +strHeight/2 );
if(xScroll < -strWidth ){
xScroll = d.width;
}
g.drawImage(offs, 0, 0, this);
}
public void start() {
if (kicker == null) {
kicker = new Thread(this);
kicker.start();
}
}
public void stop() {
if (kicker != null) {
kicker.stop();
kicker = null;
}
}
}
//