/** Banner.java Applet David Fourer * Draws a moving text banner for a web page. * Click on the banner and it stops moving. * Accepts the following param strings, which are optional. Words * are not case-sensitive: * text -- a string of any length (default="The Quick Brown Fox..." ) * typeface -- (default)Serif or SansSerif * typestyle -- plain, bold, italic, (default)bold + italic * speed -- a wait time in milliseconds. default is 25 * background -- "000000", red-green-blue color, hexidecimal * foreground -- "000000", red-green-blue color, hexidecimal * */ import java.awt.*; import java.awt.event.*; import java.applet.Applet; import java.io.*; import java.net.URL; public class Banner extends Applet implements Runnable, MouseListener { private Image buffer; private Graphics gc; private Thread animate; private String text; private Color backColor; private Color foreColor; private Font font1; private boolean active = true; //is running private int width, height; //applet size private int sleepTime; private int bufWidth; int p = 0; //position to paint start of banner //________________________________________________________________init()____ public void init() { width = getSize().width; //applet size height = getSize().height; //applet size getHtmlParam(); setBackground( backColor ); //find length of string in chosen font buffer = createImage( 1, 1 ); gc = buffer.getGraphics(); int textLength = gc.getFontMetrics( font1 ).stringWidth( text ); int spaceLength = gc.getFontMetrics( font1 ).stringWidth( "xxx" ); bufWidth = textLength + spaceLength; p = width * 3 / 4; //position of text //creat off screen buffer and graphics context buffer = createImage( bufWidth, height ); gc = buffer.getGraphics(); gc.setFont( font1 ); //font for graphics context addMouseListener( this ); }//________________________________________________________getHtmlParam() public void getHtmlParam() { String s; s = getParameter( "text" ); //"text" string if( s == null || s.equals( "" ) ) text = new String( "Have you fed Sarah and Esther yet?" ); else text = new String( s ); s = getParameter( "typeface" ); //"typeface" string String typeface = new String(); if( s == null ) typeface = "TimesRoman"; else typeface = s; s = getParameter( "typestyle" ); //"typestyle" string int typestyle; if( s == null ) typestyle = Font.ITALIC + Font.BOLD; else if( s.equalsIgnoreCase( "plain" ) ) typestyle = Font.PLAIN; else if( s.equalsIgnoreCase( "italic" ) ) typestyle = Font.ITALIC; else if( s.equalsIgnoreCase( "bold" ) ) typestyle = Font.BOLD; else typestyle = Font.ITALIC + Font.BOLD; font1 = new Font( typeface, typestyle, height*2/3 ); s = new String(); //"speed" integer (wait millisec) s = getParameter( "speed" ); try{ sleepTime = Integer.parseInt( s ); } catch( NumberFormatException nfe ) { sleepTime=(-1); } if( sleepTime < 0 || sleepTime > 100 ) sleepTime = 30; //"background" string ( rgb color, hex ) //"foreground" string ( rgb color, hex ) String webColor1 = new String(); String webColor2 = new String(); webColor1 = getParameter( "background" ); webColor2 = getParameter( "foreground" ); if( webColor1 == null ) webColor1 = new String( "a0c0e0" ); if( webColor2 == null ) webColor2 = new String( "703020" ); try{ backColor = new Color( Integer.parseInt( webColor1.substring( 0, 2 ), 16 ), Integer.parseInt( webColor1.substring( 2, 4), 16 ), Integer.parseInt( webColor1.substring( 4 ), 16 ) ); foreColor = new Color( Integer.parseInt( webColor2.substring( 0, 2 ), 16 ), Integer.parseInt( webColor2.substring( 2, 4), 16 ), Integer.parseInt( webColor2.substring( 4 ), 16 ) ); } catch( NumberFormatException nfe ) { backColor = Color.white; foreColor = Color.blue; } }//_________________________________________________________start() stop() run() public void start() { active = true; animate = new Thread( this ); animate.start(); } public void stop() { active = false; //thread dies } public void run() { gc.clearRect( 0, 0, bufWidth, height ); gc.setColor( foreColor ); gc.drawString( text, 0, height*3/4 ); //draw the text only once while( active ) { try { Thread.sleep( sleepTime ); } catch( InterruptedException e ) { System.out.println( e.toString() ); } repaint(); } } //___________________________________________________________paint() update() public void paint( Graphics g ) { g.drawImage( buffer, p, 0, this ); if( p + bufWidth < width ) g.drawImage( buffer, p + bufWidth, 0, this ); if( p + bufWidth * 2 < width ) g.drawImage( buffer, p + bufWidth * 2, 0, this ); p = (p - 3) % bufWidth ; } public void update( Graphics g ) { paint( g ); } //_________________________________________________________MouseListener methods public void mouseClicked( MouseEvent e ) { if( active ) active = false; else start(); } public void mouseReleased( MouseEvent e ) { } public void mouseEntered( MouseEvent e ) { } public void mouseExited( MouseEvent e ) { } public void mousePressed( MouseEvent e ) { } }