home *** CD-ROM | disk | FTP | other *** search
Wrap
import java.applet.Applet; import java.applet.AudioClip; import java.awt.Color; import java.awt.Component; import java.awt.Event; import java.awt.Font; import java.awt.FontMetrics; import java.awt.Graphics; import java.awt.Image; import java.io.IOException; import java.io.InputStream; import java.net.MalformedURLException; import java.net.URL; import java.util.Hashtable; import java.util.NoSuchElementException; import java.util.StringTokenizer; public class TextScrollerApplet extends Applet implements Runnable { private static final int RUNNING = 1; private static final int SUSPENDED = 2; private static int defaultTimeout = 4; private static int defaultScrollDistance = 5; private static String defaultScrollingText = "Scrolling Text brought to you by Gregory S. Pogue (http://www.inforamp.net/~pogue/gregory/)"; private static String defaultFontName = "TimesRoman"; private static int defaultFontStyle = 1; private static int defaultFontSize = 36; private static String defaultVerticleAlignment = "CENTER"; private static Color defaultFontColor; private static Color defaultBackgroundColor; private static Hashtable myColorCache; private static String appletInfoString; private static String[][] appletParamList; private Thread _kicker; private int _kickerState = 1; private String _scrollingText; private AudioClip _audioClip; private boolean _audioLoop = false; private Image _frame; private Graphics _offscreen; private int _waitTimeout; private int _scrollDistance; private int _anchorX; private int _anchorY; private Color _fontColor; private Color _backgroundColor; public void init() { Object var2 = null; int var4 = 0; String var1 = ((Applet)this).getParameter("speed"); try { this._waitTimeout = 1000 / (var1 == null ? defaultTimeout : Integer.valueOf(var1)); } catch (Exception var13) { this._waitTimeout = defaultTimeout; } var1 = ((Applet)this).getParameter("distance"); try { this._scrollDistance = var1 == null ? defaultScrollDistance : Integer.valueOf(var1); } catch (NumberFormatException var12) { this._scrollDistance = defaultScrollDistance; } var1 = ((Applet)this).getParameter("remoteText"); if (var1 != null) { try { var25 = new URL(((Applet)this).getDocumentBase(), var1); } catch (MalformedURLException var11) { var25 = null; } if (var25 != null) { this._scrollingText = this.getRemoteString(var25); } } if (this._scrollingText == null) { var1 = ((Applet)this).getParameter("text"); this._scrollingText = var1 == null ? defaultScrollingText : var1; } var1 = ((Applet)this).getParameter("font"); String var3 = var1 == null ? defaultFontName : var1; var1 = ((Applet)this).getParameter("size"); int var5; try { var5 = var1 == null ? defaultFontSize : Integer.valueOf(var1); } catch (NumberFormatException var10) { var5 = defaultFontSize; } var1 = ((Applet)this).getParameter("style"); if (var1 != null) { var1 = var1.toUpperCase(); var4 += var1.indexOf("BOLD") < 0 ? 0 : 1; var4 += var1.indexOf("ITALIC") < 0 ? 0 : 2; var4 += var1.indexOf("PLAIN") < 0 ? 0 : 0; } if (var1 == null || var4 == 0) { var4 = defaultFontStyle; } Font var6 = new Font(var3, var4, var5); FontMetrics var7 = ((Component)this).getFontMetrics(var6); var1 = ((Applet)this).getParameter("textColor"); if (var1 != null) { this._fontColor = this.colorFromString(var1); } if (this._fontColor == null) { this._fontColor = defaultFontColor; } var1 = ((Applet)this).getParameter("backgroundColor"); if (var1 != null) { this._backgroundColor = this.colorFromString(var1); } if (this._backgroundColor == null) { this._backgroundColor = defaultBackgroundColor; } ((Component)this).setBackground(this._backgroundColor); var1 = ((Applet)this).getParameter("alignment"); String var8 = (var1 == null ? defaultVerticleAlignment : var1).toUpperCase(); if (var8.equals("TOP")) { this._anchorY = var7.getHeight() - var7.getDescent(); } else if (var8.equals("BOTTOM")) { this._anchorY = ((Component)this).size().height - var7.getDescent(); } else { this._anchorY = (((Component)this).size().height - var7.getHeight()) / 2 + var7.getLeading() + var7.getAscent(); } var1 = ((Applet)this).getParameter("audioClip"); if (var1 != null) { try { this._audioClip = ((Applet)this).getAudioClip(new URL(((Applet)this).getDocumentBase(), var1)); } catch (MalformedURLException var9) { this._audioClip = null; } if (this._audioClip != null) { var1 = ((Applet)this).getParameter("audioLoop"); if (var1 != null && var1.equalsIgnoreCase("ON")) { this._audioLoop = true; this._audioClip.loop(); } else { this._audioLoop = false; this._audioClip.play(); } } } this._frame = ((Component)this).createImage(var7.stringWidth(this._scrollingText) + this._scrollDistance * 30, ((Component)this).size().height); this._offscreen = this._frame.getGraphics(); this._offscreen.setFont(var6); this._offscreen.setColor(this._backgroundColor); this._offscreen.fillRect(0, 0, this._frame.getWidth(this), this._frame.getHeight(this)); this._offscreen.setColor(this._fontColor); this._offscreen.drawString(this._scrollingText, 0, this._anchorY); } public void start() { if (this._kicker == null && this._scrollingText != null) { this._kicker = new Thread(this); this._anchorX = ((Component)this).size().width; if (this._audioClip != null) { if (this._audioLoop) { this._audioClip.loop(); } else { this._audioClip.play(); } } this._kicker.start(); } } public void stop() { if (this._kicker != null) { this._kicker.stop(); this._kicker = null; } if (this._audioClip != null) { this._audioClip.stop(); } } public synchronized boolean mouseDown(Event var1, int var2, int var3) { if (this._kicker != null) { if (this._kickerState == 1) { this._kicker.suspend(); this._kickerState = 2; if (this._audioClip != null) { this._audioClip.stop(); } } else { this._kicker.resume(); this._kickerState = 1; if (this._audioClip != null) { if (this._audioLoop) { this._audioClip.loop(); } else { this._audioClip.play(); } } } } return true; } public void run() { while(true) { try { Thread.currentThread(); Thread.sleep((long)this._waitTimeout); } catch (InterruptedException var1) { } this.scrollText(this._scrollDistance); } } private synchronized void scrollText(int var1) { this._anchorX = this._anchorX < (this._frame.getWidth(this) + this._scrollDistance) * -1 ? ((Component)this).size().width : this._anchorX - this._scrollDistance; ((Component)this).repaint(); } public void update(Graphics var1) { this.paint(var1); } public void paint(Graphics var1) { var1.drawImage(this._frame, this._anchorX, 0, this); } protected String getRemoteString(URL var1) { StringBuffer var3 = new StringBuffer(); int var4 = 0; String var5 = null; InputStream var2; try { var2 = var1.openStream(); } catch (Exception var7) { var2 = null; } if (var2 != null) { try { while(var4 >= 0) { var4 = var2.read(); if (var4 >= 0) { var3.append((char)var4); } } var5 = var3.toString(); var2.close(); } catch (IOException var8) { var5 = null; try { var2.close(); } catch (IOException var6) { } } } return var5; } protected Color colorFromString(String var1) { String var3 = var1.toLowerCase(); Color var2 = (Color)myColorCache.get(var3); if (var2 == null) { StringTokenizer var4 = new StringTokenizer(var3, ",./;:-"); try { int var5 = Integer.valueOf(var4.nextToken()); int var6 = Integer.valueOf(var4.nextToken()); int var7 = Integer.valueOf(var4.nextToken()); var2 = new Color(var5, var6, var7); } catch (NoSuchElementException var8) { } catch (NumberFormatException var9) { } } return var2; } public String getAppletInfo() { return appletInfoString; } public String[][] getParameterInfo() { return appletParamList; } static { defaultFontColor = Color.blue; defaultBackgroundColor = Color.white; myColorCache = new Hashtable(); appletInfoString = "TextScrollerApplet, Version 3.00, 18/02/1996, Gregory S. Pogue [pogue@inforamp.net]"; appletParamList = new String[][]{{"speed", "1-1000", "a relative speed adjustment (the larger the number, the faster the text)"}, {"distance", "1-100", "the number of pixels to move the text between each frame"}, {"remoteText", "url", "a url of a file containing the text to scroll (relative or absolute)"}, {"text", "string", "the text to be scrolled (remoteText will override this, unless there is an error reading from the url)"}, {"font", "font", "the name of the font to use for the text"}, {"size", "6-100", "the point size of the font"}, {"style", "BOLD ITALIC PLAIN", "the style of the font (can be a combination)"}, {"textColor", "name or r,g,b", "color of the text (can be specified as Red or 255,0,0)"}, {"backgroundColor", "name or r,g,b", "color of the background (can be specified as Red or 255,0,0)"}, {"alignment", "TOP BOTTOM CENTER", "alignment of the text vertically within the applet"}, {"audioClip", "url", "a url of a file containing audio to play"}, {"audioLoop", "YES NO", "an indicator which allows an audio file to be played continuously"}}; myColorCache.put("red", Color.red); myColorCache.put("green", Color.green); myColorCache.put("blue", Color.blue); myColorCache.put("cyan", Color.cyan); myColorCache.put("magenta", Color.magenta); myColorCache.put("yellow", Color.yellow); myColorCache.put("orange", Color.orange); myColorCache.put("pink", Color.pink); myColorCache.put("white", Color.white); myColorCache.put("lightgray", Color.lightGray); myColorCache.put("gray", Color.gray); myColorCache.put("darkgray", Color.darkGray); myColorCache.put("black", Color.black); } }