home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Programming Languages Suite
/
ProgLangD.iso
/
VCAFE.3.0A
/
Sample.bin
/
ScrollingText.java
< prev
next >
Wrap
Text File
|
1997-09-06
|
5KB
|
209 lines
/*
* ScrollingText.java
*
* This file contains class ScrollingText.
*/
package samples.conversion;
import java.applet.*;
import java.awt.*;
public class ScrollingText extends Canvas implements Runnable
{
private Thread thread;
private Image image;
private Graphics imageGC;
public final static int MAX_RATE = 30;
public final static int MIN_RATE = 1;
public final static boolean RANDOM = true;
public final static boolean CENTER = false;
protected Color backgroundColor = Color.black;
protected Color textColor = Color.yellow;
protected int refreshRate = 50;
protected int width;
protected int height;
protected int posX = 0;
protected int posY = 0;
protected int scrollRate = 3;
protected int textWidth;
protected int textHeight;
protected boolean random = true;
protected String text;
protected Font font;
ScrollingText()
{
this( "SYMANTEC. Visual Cafe. ", 320, 280 );
}
ScrollingText( String s, int w, int h )
{
width = w;
height = h;
setBackground( backgroundColor );
setSize( width, height );
text = s;
font = new Font( "TimesRoman", Font.PLAIN, 15 );
textWidth = Toolkit.getDefaultToolkit().getFontMetrics( font ).stringWidth( s );
textHeight = Toolkit.getDefaultToolkit().getFontMetrics( font ).getHeight();
posX = width;
}
public void addNotify()
{
super.addNotify();
image = createImage( width, height );
imageGC = image.getGraphics();
}
public void run()
{
/*
* This is the thread's main body.
*/
while( true )
{
repaint();
try
{
Thread.sleep( refreshRate );
}
catch( Exception e )
{
System.out.println( e.toString() );
System.exit( 1 );
}
Thread.yield();
}
}
public void start()
{
/*
* This will start the animation.
*/
if( thread == null )
thread = new Thread( this );
thread.start();
}
public void stop()
{
/*
* This will stop the animation.
*/
if( thread != null )
{
thread.stop();
thread = null;
}
}
public void update( Graphics g )
{
paint( g );
}
public void paint( Graphics g )
{
/*
* if posX is at starting position,
* get the next y position either randomly
* or set at the center.
*/
if( posX == width )
if( random )
{
posY = (int)( (double) height * Math.random() );
if( posY <= textHeight )
posY += textHeight;
}
else
posY = height / 2;
/*
* draw the text.
*/
if( imageGC == null )
{
/*
* if imageGC == null
* just use the default graphics context
* although this may flicker.
*/
g.setColor( backgroundColor );
g.fillRect( 0, 0, width, height );
g.setColor( textColor );
g.setFont( font );
g.drawString( text, posX, posY );
}
else
{
imageGC.setColor( backgroundColor );
imageGC.fillRect( 0, 0, width, height );
imageGC.setColor( textColor );
imageGC.setFont( font );
imageGC.drawString( text, posX, posY );
}
/*
* set the next x position.
*/
posX -= scrollRate;
if( posX <= -textWidth )
posX = width;
g.drawImage( image, 0, 0, this );
}
public void setBackgroundColor( Color c )
{
stop();
backgroundColor = c;
}
public void setTextColor( Color c )
{
stop();
textColor = c;
}
public void setText( String s )
{
stop();
text = s;
}
public void setRate( int rate )
{
if( rate < MIN_RATE )
scrollRate = MIN_RATE;
else if( rate > MAX_RATE )
scrollRate = MAX_RATE;
else
scrollRate = rate;
}
public String getText()
{
return text;
}
public void setPosition( boolean pos )
{
random = pos;
posX = width;
repaint();
}
}