home *** CD-ROM | disk | FTP | other *** search
/ PC Online 1997 October / PCO1097.ISO / FilesBBS / WIN95 / MSGSCROL.EXE / MsgScroll.java < prev    next >
Encoding:
Java Source  |  1997-09-02  |  8.2 KB  |  343 lines

  1. /************************************************************************************************
  2.  
  3. Displays a scrolling text message. Scrolling can be toggled on and off by clicking the mouse.
  4.  
  5. <applet code="MsgScroll.class" width=w height=h>
  6. <param name="delay" value="n">
  7. <param name="msg" value="Your message here...">
  8. <param name="fgcolor" value="color">
  9. <param name="bgcolor" value="color">
  10. <param name="shadow" value="x,y,color">
  11. <param name="border" value="size,color">
  12. <param name="font" value="name,style,size">
  13. </applet>
  14.  
  15. ************************************************************************************************/
  16.  
  17. import java.awt.*;
  18. import java.util.*;
  19. import java.applet.Applet;
  20.  
  21. public class MsgScroll extends Applet implements Runnable {
  22.  
  23.   // Parameters and defaults.
  24.  
  25.   int    delay     = 25;
  26.   String msg       = "Your message here...";
  27.   Color  fgColor   = Color.black;
  28.   Color  bgColor   = Color.white;
  29.   int    shxOffset = 0;
  30.   int    shyOffset = 0;
  31.   Color  shColor   = Color.lightGray;
  32.   int    bdSize    = 0;
  33.   Color  bdColor;
  34.   String fontName  = "Dialog";
  35.   int    fontStyle = Font.PLAIN;
  36.   int    fontSize  = 12;
  37.  
  38.   // Thread control variables.
  39.  
  40.   Thread  scrollThread;
  41.   boolean suspended = false;
  42.  
  43.   // Font values.
  44.  
  45.   Font font;
  46.   int  msgWidth;
  47.   int  xOffset;
  48.   int  xOffsetMax;
  49.   int  xOffsetMin;
  50.   int  yOffset;
  51.  
  52.   // The off-screen graphic.
  53.  
  54.   Dimension offDimension;
  55.   Image     offImage;
  56.   Graphics  offGraphics;
  57.  
  58.   public void init() {
  59.  
  60.     String s, t;
  61.     StringTokenizer st;
  62.     int n;
  63.     Graphics g;
  64.     FontMetrics fm;
  65.  
  66.     // Get delay time.
  67.  
  68.     try {
  69.       s = getParameter("delay");
  70.       if (s != null)
  71.         if ((n = Integer.parseInt(s)) > 0)
  72.           delay = n;
  73.     }
  74.     catch (Exception e) {}
  75.     
  76.     // Get the message text.
  77.  
  78.     try {
  79.       s = getParameter("msg");
  80.       if (s != null)
  81.         msg = s;
  82.     }
  83.     catch (Exception e) {}
  84.  
  85.     // Get foregorund and background colors.
  86.  
  87.     try {
  88.       s = getParameter("fgcolor");
  89.       if (s != null)
  90.         fgColor = getColorParm(s);
  91.     }
  92.     catch (Exception e) {}
  93.     bdColor = fgColor;
  94.  
  95.     try {
  96.       s = getParameter("bgcolor");
  97.       if (s != null)
  98.         bgColor = getColorParm(s);
  99.     }
  100.     catch (Exception e) {}
  101.  
  102.     // Get shadow offsets and color.
  103.  
  104.     try {
  105.       s = getParameter("shadow");
  106.       if (s != null) {
  107.         st = new StringTokenizer(s, ",");
  108.         if ((n = Integer.parseInt(st.nextToken())) > 0)
  109.           shxOffset = n;
  110.         if ((n = Integer.parseInt(st.nextToken())) > 0)
  111.           shyOffset = n;
  112.         shColor = getColorParm(st.nextToken());
  113.       }
  114.     }
  115.     catch (Exception e) {}
  116.  
  117.     // Get border size and color.
  118.  
  119.     try {
  120.       s = getParameter("border");
  121.       if (s != null) {
  122.         st = new StringTokenizer(s, ",");
  123.         if ((n = Integer.parseInt(st.nextToken())) > 0)
  124.           bdSize = n;
  125.         bdColor = getColorParm(st.nextToken());
  126.       }
  127.     }
  128.     catch (Exception e) {}
  129.  
  130.     // Get the font.
  131.  
  132.     try {
  133.       s = getParameter("font");
  134.  
  135.       // Font name.
  136.  
  137.       st = new StringTokenizer(s, ",");
  138.       t = st.nextToken();
  139.       if (t.equalsIgnoreCase("Courier"))
  140.         fontName = "Courier";
  141.       else if (t.equalsIgnoreCase("Dialog"))
  142.         fontName = "Dialog";
  143.       else if (t.equalsIgnoreCase("Helvetica"))
  144.         fontName = "Helvetica";
  145.       else if (t.equalsIgnoreCase("Symbol"))
  146.         fontName = "Symbol";
  147.       else if (t.equalsIgnoreCase("TimesRoman"))
  148.         fontName = "TimesRoman";
  149.  
  150.       // Font style.
  151.  
  152.       t = st.nextToken();
  153.       if (t.equalsIgnoreCase("plain"))
  154.         fontStyle = Font.PLAIN;
  155.       else if (t.equalsIgnoreCase("bold"))
  156.         fontStyle = Font.BOLD;
  157.       else if (t.equalsIgnoreCase("italic"))
  158.         fontStyle = Font.ITALIC;
  159.       else if (t.equalsIgnoreCase("boldItalic"))
  160.         fontStyle = Font.BOLD + Font.ITALIC;
  161.  
  162.       // Font size.
  163.  
  164.       t = st.nextToken();
  165.       if ((n = Integer.parseInt(t)) > 0)
  166.           fontSize = n;
  167.     }
  168.     catch (Exception e) {}
  169.  
  170.     // Set the font values.
  171.  
  172.     g = getGraphics();
  173.     font = g.getFont();
  174.     g.setFont(font = new Font(fontName, fontStyle, fontSize));
  175.     fm = g.getFontMetrics();
  176.     msgWidth = fm.stringWidth(msg);
  177.     xOffsetMin = -msgWidth;
  178.     xOffsetMax = size().width;
  179.     xOffset = xOffsetMax;
  180.     yOffset = size().height - (size().height - fm.getHeight()) / 2 - fm.getDescent();
  181.   }
  182.  
  183.   private Color getColorParm(String s) {
  184.  
  185.       int r, g, b;
  186.  
  187.       // Check if a pre-defined color is specified.
  188.  
  189.       if (s.equalsIgnoreCase("black"))
  190.         return(Color.black);
  191.       if (s.equalsIgnoreCase("blue"))
  192.         return(Color.blue);
  193.       if (s.equalsIgnoreCase("cyan"))
  194.         return(Color.cyan);
  195.       if (s.equalsIgnoreCase("darkGray"))
  196.         return(Color.darkGray);
  197.       if (s.equalsIgnoreCase("gray"))
  198.         return(Color.gray);
  199.       if (s.equalsIgnoreCase("green"))
  200.         return(Color.green);
  201.       if (s.equalsIgnoreCase("lightGray"))
  202.         return(Color.lightGray);
  203.       if (s.equalsIgnoreCase("magenta"))
  204.         return(Color.magenta);
  205.       if (s.equalsIgnoreCase("orange"))
  206.         return(Color.orange);
  207.       if (s.equalsIgnoreCase("pink"))
  208.         return(Color.pink);
  209.       if (s.equalsIgnoreCase("red"))
  210.         return(Color.red);
  211.       if (s.equalsIgnoreCase("white"))
  212.         return(Color.white);
  213.       if (s.equalsIgnoreCase("yellow"))
  214.         return(Color.yellow);
  215.  
  216.       // If the color is specified in HTML format, build it from the red, green and blue values.
  217.  
  218.       if (s.length() == 7 && s.charAt(0) == '#') {
  219.         r = Integer.parseInt(s.substring(1,3),16);
  220.         g = Integer.parseInt(s.substring(3,5),16);
  221.         b = Integer.parseInt(s.substring(5,7),16);
  222.         return(new Color(r, g, b));
  223.       }
  224.  
  225.       // Default to black.
  226.  
  227.       return(Color.black);
  228.   }
  229.  
  230.   public void start() {
  231.  
  232.     if (scrollThread == null) {
  233.       scrollThread = new Thread(this);
  234.       scrollThread.start();
  235.     }
  236.   }
  237.  
  238.   public void stop() {
  239.  
  240.     if (scrollThread != null) {
  241.       scrollThread.stop();
  242.       scrollThread = null;
  243.     }
  244.   }
  245.  
  246.   public boolean mouseDown(Event e, int x, int y) {
  247.  
  248.     if (suspended)
  249.       start();
  250.     else
  251.       stop();
  252.     suspended = !suspended;
  253.     return true;
  254.   }
  255.  
  256.   public void run() {
  257.  
  258.     long startTime;
  259.  
  260.     // Lower this thread's priority.
  261.  
  262.     Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
  263.  
  264.     // Get the start time.
  265.  
  266.     startTime = System.currentTimeMillis();
  267.  
  268.     // This is the animation loop.
  269.  
  270.     while (Thread.currentThread() == scrollThread) {
  271.  
  272.       // Advance the animation.
  273.  
  274.       if (--xOffset < xOffsetMin )
  275.         xOffset = xOffsetMax;
  276.  
  277.       // Display it.
  278.  
  279.       repaint();
  280.  
  281.       // Delay the thread.
  282.  
  283.       try {
  284.         startTime += delay;
  285.         Thread.sleep(Math.max(0, startTime - System.currentTimeMillis()));
  286.       }
  287.       catch (InterruptedException e) {
  288.         break;
  289.       }
  290.     }
  291.   }
  292.  
  293.   public void paint(Graphics g) {
  294.  
  295.     update(g);
  296.   }
  297.  
  298.   public void update(Graphics g) {
  299.  
  300.     Dimension d = size();
  301.     int i;
  302.  
  303.     // Create the offscreen graphics context, if no good one exists.
  304.  
  305.     if (offGraphics == null || d.width != offDimension.width || d.height != offDimension.height) {
  306.       offDimension = d;
  307.       offImage = createImage(d.width, d.height);
  308.       offGraphics = offImage.getGraphics();
  309.     }
  310.  
  311.     // Fill in background.
  312.  
  313.     offGraphics.setColor(bgColor);
  314.     offGraphics.fillRect(0, 0, d.width, d.height);
  315.  
  316.     // Set the font.
  317.  
  318.     offGraphics.setFont(font);
  319.  
  320.     // Draw shadow.
  321.  
  322.     if (shxOffset != 0 || shyOffset != 0) {
  323.       offGraphics.setColor(shColor);
  324.       offGraphics.drawString(msg, xOffset + shxOffset, yOffset + shyOffset);
  325.     }
  326.  
  327.     // Draw text.
  328.  
  329.     offGraphics.setColor(fgColor);
  330.     offGraphics.drawString(msg, xOffset, yOffset);
  331.  
  332.     // Draw border.
  333.  
  334.     offGraphics.setColor(bdColor);
  335.     for (i = 0; i < bdSize; i++)
  336.       offGraphics.drawRect(i, i, d.width - 2 * i - 1, d.height - 2 * i - 1);
  337.  
  338.     // Paint the image onto the screen.
  339.  
  340.     g.drawImage(offImage, 0, 0, this);
  341.   }
  342. }
  343.