import java.awt.*; import java.awt.event.*; import java.applet.*; import java.util.*; class ScrollingText { String actualText; String attrib; int mode, backDelay; public static final int RIGHT_LEFT = 0; public static final int LEFT_RIGHT = 1; public static final int UP_DOWN = 2; public static final int DOWN_UP = 3; public static final int RIGHT_BACK = 4; public static final int LEFT_BACK = 5; public static final int UP_BACK = 6; public static final int DOWN_BACK = 7; public static final int RIGHT_CONT = 8; public static final int LEFT_CONT = 9; public static final int UP_CONT = 10; public static final int DOWN_CONT = 11; public ScrollingText(String paramText) { char c; attrib = ""; if(paramText.charAt(0) == '{') { c = '{'; int i; for(i = 1; (c = paramText.charAt(i)) != '}'; ++i) attrib+=new Character(c).toString(); if(i < paramText.length()) actualText = paramText.substring(i + 1); else actualText = ""; mode = convertMode(attrib); } else { mode = RIGHT_LEFT; actualText = paramText; } } private int convertMode(String str) { int ret; String s = str.substring(0, 2); if(s.equals("rl")) ret = RIGHT_LEFT; else if(s.equals("lr")) ret = LEFT_RIGHT; else if(s.equals("ud")) ret = UP_DOWN; else if(s.equals("du")) ret = DOWN_UP; else if(s.equals("rb")) ret = RIGHT_BACK; else if(s.equals("lb")) ret = LEFT_BACK; else if(s.equals("ub")) ret = UP_BACK; else if(s.equals("db")) ret = DOWN_BACK; else if(s.equals("lc")) ret = LEFT_CONT; else if(s.equals("rc")) ret = RIGHT_CONT; else if(s.equals("uc")) ret = UP_CONT; else if(s.equals("dc")) ret = DOWN_CONT; else ret = -1; if((str.charAt(1) == 'b' || str.charAt(1) == 'c') && str.length() > 2) { try { backDelay = Integer.parseInt(str.substring(2)); } catch (Exception e) { backDelay = 2000; } } else backDelay = 2000; return ret; } public int getMode() { return mode; } public String getActualText() { return actualText; } public int getDelay() { return backDelay; } } public class SuperScroller extends Applet implements Runnable, MouseListener { Vector textItems; volatile Thread runner ; Image offImage; Graphics g2; int x = 0, y = 0; int fontSize; int fStyle = 0; int delay; int currentStr = 0; int currentMode = ScrollingText.RIGHT_LEFT; Color bgColor = Color.white, fgColor = Color.blue; Font font; FontMetrics fMetrics; int strWid = 0; String scroll; Dimension size; boolean running = true, forward = true, stopped = false; boolean done = false, begin = true; public void init() { textItems = new Vector(1); String temp = ""; int n; for(n = 0; n <= 14 ; ++n) { temp = getParameter("text" + new Integer(n).toString()); if (temp == null) continue; temp.trim(); ScrollingText st = new ScrollingText(temp); if(st.getMode() >= 0) textItems.addElement(st); } String delStr = getParameter("delay"); if(delStr != null) try { delay = Integer.parseInt(delStr); } catch (Exception e) { delay = 20; } else delay = 20; String bgc = getParameter("bgcolor"); if(bgc == null) bgColor = Color.white; else try { int c = Integer.parseInt(bgc, 16); bgColor = new Color(c); } catch (Exception e) { bgColor = Color.white; } String fgc = getParameter("fgcolor"); if(fgc == null) fgColor = Color.blue; else try { int d = Integer.parseInt(fgc, 16); fgColor = new Color(d); } catch (Exception e) { fgColor = Color.blue; } String fs = getParameter("style"); if(fs != null) fStyle = parseStyle(fs); else fStyle = Font.PLAIN; String fsize = getParameter ("fontsize"); if(fsize != null) try { fontSize = Integer.parseInt(fsize); } catch (Exception e) { fontSize = 16; } else fontSize = 16; String fName = getParameter("font"); if(fName == null) font = new Font("Courier", fStyle, fontSize); else font = new Font(fName, fStyle, fontSize); Toolkit toolKit = Toolkit.getDefaultToolkit(); fMetrics = toolKit.getFontMetrics(font); size = getSize(); y = (size.height) / 2; scroll = ((ScrollingText)textItems.elementAt(0)).getActualText(); strWid = fMetrics.stringWidth(scroll); addMouseListener(this); } public void start() { setBackground(bgColor); offImage = createImage(size.width, size.height); g2 = offImage.getGraphics(); g2.setFont(font); g2.setColor(fgColor); runner = new Thread(this); runner.start(); } public synchronized void stop() { runner = null; notify(); } public void paint(Graphics g) { update(g); } public synchronized void update (Graphics g) { g2.clearRect(0, 0, size.width, size.height); g2.setColor(fgColor); g2.drawString(scroll, x, y); g.drawImage(offImage, 0, 0, this); } public void run() { Thread curThr = Thread.currentThread(); while(true) { try { runner.sleep(delay); if(!running) synchronized(this) { while(!running && curThr == runner) wait(); } } catch (Exception e) {} if (done) { done = false; int s = textItems.size(); currentStr = (currentStr >= s - 1) ? 0 : currentStr + 1; ScrollingText st = (ScrollingText) textItems.elementAt(currentStr); currentMode = st.getMode(); while(currentMode < 0) { ++currentStr; st = (ScrollingText) textItems.elementAt(currentStr); currentMode = st.getMode(); } scroll = st.getActualText(); strWid = fMetrics.stringWidth(scroll); begin = true; } scrollAction(); repaint(); } } private int parseStyle(String s) { int l = s.length(); int pos, ret = 0; for(pos = 0 ; pos < l ; ++pos) { switch (s.charAt(pos)) { case 'b': ret+=Font.BOLD; break; case 'i': ret+=Font.ITALIC; break; } } return (ret == 0) ? Font.PLAIN : ret; } public void mouseReleased(MouseEvent e) {} public void mouseEntered(MouseEvent e) {} public void mouseExited(MouseEvent e) {} public void mouseClicked(MouseEvent e) {} public synchronized void mousePressed(MouseEvent e) { e.consume(); running = !running; if(running) notify(); } private void scrollAction() { ScrollingText st = (ScrollingText) textItems.elementAt(currentStr); switch (currentMode) { case ScrollingText.RIGHT_LEFT: if(begin) { x = size.width; y = (size.height + fMetrics.getHeight()) / 2; begin = false; } if (x < 0 - strWid) done = true; else --x; break; case ScrollingText.LEFT_RIGHT: if(begin) { x = -strWid; y = (size.height + fMetrics.getHeight()) / 2; begin = false; } if(x > size.width) done = true; else ++x; break; case ScrollingText.DOWN_UP: if(begin) { x = (size.width - strWid) / 2; y = size.height + fMetrics.getHeight(); begin = false; } if (y < 0) done = true; else --y; break; case ScrollingText.UP_DOWN: if(begin) { x = (size.width - strWid) / 2; y = 0; begin = false; } if (y > size.height + fMetrics.getHeight()) done = true; else ++y; break; case ScrollingText.RIGHT_BACK: if(begin) { forward = true; y = (size.height + fMetrics.getHeight()) / 2; x = size.width; begin = false; } if (forward) --x; else ++x; if(x < ((size.width - strWid) / 2)) { try { runner.sleep (st.getDelay()); } catch (Exception e) {;} forward = false; } if (!forward && x > size.width) done = true; break; case ScrollingText.LEFT_BACK: if(begin) { forward = true; y = (size.height + fMetrics.getHeight()) / 2; x = -strWid; begin = false; } if (forward) ++x; else --x; if(x > ((size.width - strWid) / 2)) { try { runner.sleep (st.getDelay()); } catch (Exception e) {;} forward = false; } if (!forward && x < -strWid ) done = true; break; case ScrollingText.UP_BACK: if(begin) { x = (size.width - strWid) / 2; y = 0; forward = true; begin = false; } if(forward) ++y; else --y; if(y > (size.height + fMetrics.getHeight()) / 2) { try { runner.sleep (st.getDelay()); } catch (Exception e) {;} forward = false; } if(!forward && y < 0) done = true; break; case ScrollingText.DOWN_BACK: if(begin) { x = (size.width - strWid) / 2; y = size.height + fMetrics.getHeight(); forward = true; begin = false; } if(forward) --y; else ++y; if(y < (size.height + fMetrics.getHeight()) / 2) { try { runner.sleep (st.getDelay()); } catch (Exception e) {;} forward = false; } if(!forward && y > size.height + fMetrics.getHeight()) done = true; break; case ScrollingText.LEFT_CONT: if(begin) { x = -strWid; y = (size.height + fMetrics.getHeight()) / 2; begin = false; stopped = false; } if(x > size.width) done = true; else ++x; if(x >= (size.width - strWid) / 2 && !stopped) { try { runner.sleep(st.getDelay()); } catch (Exception e) {;} stopped = true; } break; case ScrollingText.RIGHT_CONT: if(begin) { x = size.width; y = (size.height + fMetrics.getHeight()) / 2; begin = false; stopped = false; } if(x < -strWid) done = true; else --x; if(x <= (size.width - strWid) / 2 && !stopped) { try { runner.sleep(st.getDelay()); } catch (Exception e) {;} stopped = true; } break; case ScrollingText.DOWN_CONT: if(begin) { x = (size.width - strWid) / 2; y = size.height + fMetrics.getHeight(); begin = false; stopped = false; } if(y < 0) done = true; else --y; if(y <= (size.height + fMetrics.getHeight()) / 2 && !stopped) { try { runner.sleep(st.getDelay()); } catch (Exception e) {;} stopped = true; } break; case ScrollingText.UP_CONT: if(begin) { x = (size.width - strWid) / 2; y = 0; begin = false; stopped = false; } if(y > size.height + fMetrics.getHeight()) done = true; else ++y; if(y >= (size.height + fMetrics.getHeight()) / 2 && !stopped) { try { runner.sleep(st.getDelay()); } catch (Exception e) {;} stopped = true; } break; } } }