home *** CD-ROM | disk | FTP | other *** search
Java Source | 1997-09-02 | 8.2 KB | 343 lines |
- /************************************************************************************************
-
- Displays a scrolling text message. Scrolling can be toggled on and off by clicking the mouse.
-
- <applet code="MsgScroll.class" width=w height=h>
- <param name="delay" value="n">
- <param name="msg" value="Your message here...">
- <param name="fgcolor" value="color">
- <param name="bgcolor" value="color">
- <param name="shadow" value="x,y,color">
- <param name="border" value="size,color">
- <param name="font" value="name,style,size">
- </applet>
-
- ************************************************************************************************/
-
- import java.awt.*;
- import java.util.*;
- import java.applet.Applet;
-
- public class MsgScroll extends Applet implements Runnable {
-
- // Parameters and defaults.
-
- int delay = 25;
- String msg = "Your message here...";
- Color fgColor = Color.black;
- Color bgColor = Color.white;
- int shxOffset = 0;
- int shyOffset = 0;
- Color shColor = Color.lightGray;
- int bdSize = 0;
- Color bdColor;
- String fontName = "Dialog";
- int fontStyle = Font.PLAIN;
- int fontSize = 12;
-
- // Thread control variables.
-
- Thread scrollThread;
- boolean suspended = false;
-
- // Font values.
-
- Font font;
- int msgWidth;
- int xOffset;
- int xOffsetMax;
- int xOffsetMin;
- int yOffset;
-
- // The off-screen graphic.
-
- Dimension offDimension;
- Image offImage;
- Graphics offGraphics;
-
- public void init() {
-
- String s, t;
- StringTokenizer st;
- int n;
- Graphics g;
- FontMetrics fm;
-
- // Get delay time.
-
- try {
- s = getParameter("delay");
- if (s != null)
- if ((n = Integer.parseInt(s)) > 0)
- delay = n;
- }
- catch (Exception e) {}
-
- // Get the message text.
-
- try {
- s = getParameter("msg");
- if (s != null)
- msg = s;
- }
- catch (Exception e) {}
-
- // Get foregorund and background colors.
-
- try {
- s = getParameter("fgcolor");
- if (s != null)
- fgColor = getColorParm(s);
- }
- catch (Exception e) {}
- bdColor = fgColor;
-
- try {
- s = getParameter("bgcolor");
- if (s != null)
- bgColor = getColorParm(s);
- }
- catch (Exception e) {}
-
- // Get shadow offsets and color.
-
- try {
- s = getParameter("shadow");
- if (s != null) {
- st = new StringTokenizer(s, ",");
- if ((n = Integer.parseInt(st.nextToken())) > 0)
- shxOffset = n;
- if ((n = Integer.parseInt(st.nextToken())) > 0)
- shyOffset = n;
- shColor = getColorParm(st.nextToken());
- }
- }
- catch (Exception e) {}
-
- // Get border size and color.
-
- try {
- s = getParameter("border");
- if (s != null) {
- st = new StringTokenizer(s, ",");
- if ((n = Integer.parseInt(st.nextToken())) > 0)
- bdSize = n;
- bdColor = getColorParm(st.nextToken());
- }
- }
- catch (Exception e) {}
-
- // Get the font.
-
- try {
- s = getParameter("font");
-
- // Font name.
-
- st = new StringTokenizer(s, ",");
- t = st.nextToken();
- if (t.equalsIgnoreCase("Courier"))
- fontName = "Courier";
- else if (t.equalsIgnoreCase("Dialog"))
- fontName = "Dialog";
- else if (t.equalsIgnoreCase("Helvetica"))
- fontName = "Helvetica";
- else if (t.equalsIgnoreCase("Symbol"))
- fontName = "Symbol";
- else if (t.equalsIgnoreCase("TimesRoman"))
- fontName = "TimesRoman";
-
- // Font style.
-
- t = st.nextToken();
- if (t.equalsIgnoreCase("plain"))
- fontStyle = Font.PLAIN;
- else if (t.equalsIgnoreCase("bold"))
- fontStyle = Font.BOLD;
- else if (t.equalsIgnoreCase("italic"))
- fontStyle = Font.ITALIC;
- else if (t.equalsIgnoreCase("boldItalic"))
- fontStyle = Font.BOLD + Font.ITALIC;
-
- // Font size.
-
- t = st.nextToken();
- if ((n = Integer.parseInt(t)) > 0)
- fontSize = n;
- }
- catch (Exception e) {}
-
- // Set the font values.
-
- g = getGraphics();
- font = g.getFont();
- g.setFont(font = new Font(fontName, fontStyle, fontSize));
- fm = g.getFontMetrics();
- msgWidth = fm.stringWidth(msg);
- xOffsetMin = -msgWidth;
- xOffsetMax = size().width;
- xOffset = xOffsetMax;
- yOffset = size().height - (size().height - fm.getHeight()) / 2 - fm.getDescent();
- }
-
- private Color getColorParm(String s) {
-
- int r, g, b;
-
- // Check if a pre-defined color is specified.
-
- if (s.equalsIgnoreCase("black"))
- return(Color.black);
- if (s.equalsIgnoreCase("blue"))
- return(Color.blue);
- if (s.equalsIgnoreCase("cyan"))
- return(Color.cyan);
- if (s.equalsIgnoreCase("darkGray"))
- return(Color.darkGray);
- if (s.equalsIgnoreCase("gray"))
- return(Color.gray);
- if (s.equalsIgnoreCase("green"))
- return(Color.green);
- if (s.equalsIgnoreCase("lightGray"))
- return(Color.lightGray);
- if (s.equalsIgnoreCase("magenta"))
- return(Color.magenta);
- if (s.equalsIgnoreCase("orange"))
- return(Color.orange);
- if (s.equalsIgnoreCase("pink"))
- return(Color.pink);
- if (s.equalsIgnoreCase("red"))
- return(Color.red);
- if (s.equalsIgnoreCase("white"))
- return(Color.white);
- if (s.equalsIgnoreCase("yellow"))
- return(Color.yellow);
-
- // If the color is specified in HTML format, build it from the red, green and blue values.
-
- if (s.length() == 7 && s.charAt(0) == '#') {
- r = Integer.parseInt(s.substring(1,3),16);
- g = Integer.parseInt(s.substring(3,5),16);
- b = Integer.parseInt(s.substring(5,7),16);
- return(new Color(r, g, b));
- }
-
- // Default to black.
-
- return(Color.black);
- }
-
- public void start() {
-
- if (scrollThread == null) {
- scrollThread = new Thread(this);
- scrollThread.start();
- }
- }
-
- public void stop() {
-
- if (scrollThread != null) {
- scrollThread.stop();
- scrollThread = null;
- }
- }
-
- public boolean mouseDown(Event e, int x, int y) {
-
- if (suspended)
- start();
- else
- stop();
- suspended = !suspended;
- return true;
- }
-
- public void run() {
-
- long startTime;
-
- // Lower this thread's priority.
-
- Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
-
- // Get the start time.
-
- startTime = System.currentTimeMillis();
-
- // This is the animation loop.
-
- while (Thread.currentThread() == scrollThread) {
-
- // Advance the animation.
-
- if (--xOffset < xOffsetMin )
- xOffset = xOffsetMax;
-
- // Display it.
-
- repaint();
-
- // Delay the thread.
-
- try {
- startTime += delay;
- Thread.sleep(Math.max(0, startTime - System.currentTimeMillis()));
- }
- catch (InterruptedException e) {
- break;
- }
- }
- }
-
- public void paint(Graphics g) {
-
- update(g);
- }
-
- public void update(Graphics g) {
-
- Dimension d = size();
- int i;
-
- // Create the offscreen graphics context, if no good one exists.
-
- if (offGraphics == null || d.width != offDimension.width || d.height != offDimension.height) {
- offDimension = d;
- offImage = createImage(d.width, d.height);
- offGraphics = offImage.getGraphics();
- }
-
- // Fill in background.
-
- offGraphics.setColor(bgColor);
- offGraphics.fillRect(0, 0, d.width, d.height);
-
- // Set the font.
-
- offGraphics.setFont(font);
-
- // Draw shadow.
-
- if (shxOffset != 0 || shyOffset != 0) {
- offGraphics.setColor(shColor);
- offGraphics.drawString(msg, xOffset + shxOffset, yOffset + shyOffset);
- }
-
- // Draw text.
-
- offGraphics.setColor(fgColor);
- offGraphics.drawString(msg, xOffset, yOffset);
-
- // Draw border.
-
- offGraphics.setColor(bdColor);
- for (i = 0; i < bdSize; i++)
- offGraphics.drawRect(i, i, d.width - 2 * i - 1, d.height - 2 * i - 1);
-
- // Paint the image onto the screen.
-
- g.drawImage(offImage, 0, 0, this);
- }
- }
-