home *** CD-ROM | disk | FTP | other *** search
/ PC Online 1997 October / PCO1097.ISO / FilesBBS / FREI / DTEXT.EXE / SOURCE / DancingText.java
Encoding:
Java Source  |  1996-04-17  |  11.2 KB  |  351 lines

  1. /*
  2.  *  Author : Stanley Poon
  3.  *  Version: 1.0, 12 Feb 1996
  4.  *
  5.  *  Base on:
  6.  *    Daniel Wyszynski 
  7.  *    Center for Applied Large-Scale Computing (CALC) 
  8.  *    04-12-95 
  9.  *    Test of text animation.
  10.  *    kwalrath: Changed string; added myThread suspension. 5-9-95
  11.  */
  12. import java.applet.Applet;
  13. import java.awt.*;
  14. import java.lang.*;
  15. import java.util.*;
  16. import java.net.URL;
  17. import java.net.MalformedURLException;
  18.  
  19.  
  20. class StringInfo extends Applet {
  21.     char separated[];  // store string as character array
  22.     int  charWidth[];  // width of each character
  23.     int  length;       // length of string
  24.     int  width;        // width of window to fit the dancing string
  25.     int  height;       // height of window to fit the dancing string
  26.     int  ascent;       // ascent of font
  27.  
  28.  
  29.     public void putInfo(String str, Font font, int charSpace, int xOff, int yOff) {
  30.  
  31.         length = str.length();  // cache string length
  32.  
  33.         FontMetrics fontMet = getFontMetrics(font);
  34.          
  35.         // Values return from FontMetrics is not very accurate in PC,
  36.         // *1.02 to height & width to make sure it does cover the whole string.
  37.         width  = fontMet.stringWidth(str) + charSpace * length + xOff;
  38.         width  = (int)(width * 1.02);
  39.         height = fontMet.getHeight() + yOff;
  40.         height = (int)(height * 1.02);
  41.         ascent = fontMet.getAscent();
  42.  
  43.         // store string into character array for easy access
  44.       separated = new char[length];
  45.         str.getChars(0, length, separated, 0);
  46.  
  47.         // cache width of each character
  48.         charWidth = new int[length];
  49.       for (int i = 0; i < length; i++) {
  50.             charWidth[i] = fontMet.charWidth(separated[i]) + charSpace;
  51.         }
  52.     }  // putInfo()
  53.  
  54. }  // StringInfo
  55.  
  56.  
  57.  
  58. class ColorInfo extends Applet {
  59.     Color baseColor, shadowColor, textColor[];
  60.  
  61.     // Returns Color object form a string
  62.     // string contain either red,green,blue or hhhhhh
  63.     // no error checking to miminize applet size
  64.     public Color getColor(String str) {
  65.         if (str.indexOf(',') > 0) {
  66.             StringTokenizer t = new StringTokenizer(str, ",");
  67.             int r = Integer.parseInt(t.nextToken());
  68.             int g = Integer.parseInt(t.nextToken());
  69.             int b = Integer.parseInt(t.nextToken());
  70.             return new Color(r, g, b);
  71.         } else
  72.             return new Color(Integer.parseInt(str,16));
  73.     }  // getColor()
  74.  
  75.  
  76.     // Calculate the color value for each character
  77.     // store them in textColor[]
  78.     public void calcColor(String s, int length) {
  79.         textColor = new Color[length];  // create the color array
  80.  
  81.         // Put every color value inputted by the user in the correct position of the array
  82.         int i, j;
  83.         if (s != null) {
  84.             for (StringTokenizer t = new StringTokenizer(s,"|"); t.hasMoreTokens(); ) {
  85.                 String str = t.nextToken();
  86.                 if ((i = str.indexOf('-')) > 0) {
  87.                     j = Integer.parseInt(str.substring(0, i));
  88.                     if (j < length)
  89.                         textColor[j] = getColor(str.substring(i + 1));
  90.                 } else 
  91.                     textColor[0] = getColor(str);  // single color
  92.             }
  93.         }
  94.         else textColor[0] = Color.black;  // user didn't input textColor parameter, use default - black
  95.       
  96.         // apply linear gradient between known color value
  97.         int c0 = -1, c1 = -1, diff;
  98.         for (i = 0; i < length; i++) {
  99.             if (textColor[i] != null) {
  100.                 if (i == 0) c0 = 0;
  101.                 else {
  102.                     c1 = i;
  103.                     if (c0 == -1) {  // users didn't specify color value for the first char.
  104.                         c0 = 0;
  105.                         textColor[c0] = textColor[c1];  // copy first known color to front
  106.                     }
  107.                 }
  108.             }
  109.             // end of string reach and no color value found there?
  110.             if ((i == (length - 1)) && (textColor[i] == null)) {
  111.                 c1 = i;  // same color for last known color to the end of string
  112.                 textColor[c1] = textColor[c0];  // copy last known color to end
  113.             }
  114.  
  115.             if ((c1 != -1) && ((diff = c1 - c0) > 0)) {  // found empty region in color array
  116.                 int rStart, gStart, bStart, rDiff, gDiff, bDiff;
  117.  
  118.                 rStart = textColor[c0].getRed();
  119.                 gStart = textColor[c0].getGreen();
  120.                 bStart = textColor[c0].getBlue();
  121.  
  122.                 // calculate color difference on each step
  123.                 rDiff = (int)((textColor[c1].getRed() - rStart) / diff);
  124.                 gDiff = (int)((textColor[c1].getGreen() - gStart) / diff);
  125.                 bDiff = (int)((textColor[c1].getBlue() - bStart) / diff);
  126.  
  127.                 // generate and store the new color
  128.                 for (j = c0 + 1; j < c1; j++) {
  129.                     rStart += rDiff;
  130.                     gStart += gDiff;
  131.                     bStart += bDiff;
  132.                     textColor[j] = new Color(rStart, gStart, bStart);
  133.                 }
  134.                 c0 = c1;
  135.                 c1 = -1;
  136.             }  // if  
  137.         }  // for
  138.     }  // calcColor()
  139.  
  140. }  // ColorInfo
  141.  
  142.  
  143.  
  144. public class DancingText extends Applet implements Runnable {
  145.     StringInfo strInfo = new StringInfo();
  146.     ColorInfo  color = new ColorInfo();
  147.     Image      offscreen;
  148.     Graphics   offgraphics;
  149.     Thread     myThread = null;
  150.     Font       font;
  151.     boolean    shadow, mouse;
  152.     int        charSpace, maxXOff, maxYOff, shaXOff = 0, shaYOff = 0, speed;
  153.     URL        url;
  154.     Dimension  dim;
  155.     String     target;
  156.  
  157.     public void init() {
  158.         String s;
  159.  
  160.         int fontStyle = 0;
  161.         if ((s = getParameter("fontStyle")) != null) {
  162.           for (StringTokenizer t = new StringTokenizer(s, "|"); t.hasMoreTokens(); ) {
  163.                 String str = t.nextToken();
  164.                 if (str.equals("PLAIN"))
  165.                     fontStyle += Font.PLAIN;
  166.                 else if (str.equals("BOLD"))
  167.                     fontStyle += Font.BOLD;
  168.                 else if (str.equals("ITALIC"))
  169.                     fontStyle += Font.ITALIC;
  170.             }
  171.         } else
  172.             fontStyle = Font.PLAIN;  // default
  173.  
  174.         int fontSize;
  175.         if ((s = getParameter("fontSize")) != null)
  176.             fontSize = Integer.parseInt(s);
  177.         else
  178.             fontSize = 36;  // default
  179.  
  180.         String fontName;
  181.         if ((s = getParameter("fontName")) != null)
  182.             fontName = s;
  183.         else
  184.             fontName = "TimesRoman";  // default
  185.  
  186.         font = new Font(fontName, fontStyle, fontSize);
  187.  
  188.         if ((s = getParameter("charSpace")) != null)
  189.             charSpace = Integer.parseInt(s);
  190.         else
  191.             charSpace = 2;  // default
  192.  
  193.         if ((s = getParameter("maxXOffset")) != null)
  194.             maxXOff = Integer.parseInt(s);
  195.         else
  196.             maxXOff = 2;  // default
  197.  
  198.         if ((s = getParameter("maxYOffset")) != null)
  199.             maxYOff = Integer.parseInt(s);
  200.         else
  201.             maxYOff = 4;  // default
  202.  
  203.         if ((s = getParameter("shadowXOffset")) != null)
  204.             shaXOff = Integer.parseInt(s);
  205.         else
  206.             shaXOff = 3;  // default
  207.  
  208.         if ((s = getParameter("shadowYOffset")) != null)
  209.             shaYOff = Integer.parseInt(s);
  210.         else
  211.             shaYOff = 3;  // default
  212.  
  213.         if ((s = getParameter("text")) == null) s = "DancingText";
  214.         strInfo.putInfo(s, font, charSpace, maxXOff + Math.abs(shaXOff), maxYOff + Math.abs(shaYOff));
  215.  
  216.         resize(strInfo.width, strInfo.height);
  217.  
  218.         if ((s = getParameter("shadow")) != null)
  219.             shadow = new Boolean(s).booleanValue();
  220.         else
  221.             shadow = true;  // default
  222.  
  223.         if (shadow && ((s = getParameter("shadowColor")) != null))
  224.             color.shadowColor = color.getColor(s);
  225.         else
  226.             color.shadowColor = new Color(128, 128, 128);  // default
  227.  
  228.         s = getParameter("textColor");
  229.         color.calcColor(s, strInfo.length);
  230.       
  231.         if ((s = getParameter("baseColor")) != null)
  232.             color.baseColor = color.getColor(s);
  233.         else
  234.             color.baseColor = getBackground();  // default
  235.  
  236.         if ((s = getParameter("speed")) != null)
  237.             speed = Integer.parseInt(s);
  238.         else
  239.             speed = 100;  // default
  240.  
  241.         if ((s = getParameter("target")) != null)
  242.             target = s;
  243.  
  244.         if ((s = getParameter("URL")) != null) {
  245.             try { 
  246.                 url = new URL(s);
  247.             } catch (MalformedURLException e) {
  248.                 System.out.println("Malformed URL: Check Applet parameter - URL");
  249.             }
  250.         }
  251.  
  252.         dim = size();
  253.  
  254.         offscreen = createImage(dim.width, dim.height);
  255.         offgraphics = offscreen.getGraphics();
  256.         offgraphics.setFont(font);
  257.     }  // init()
  258.  
  259.  
  260.     public void start() {
  261.         if (myThread == null) {
  262.             myThread = new Thread(this);
  263.             myThread.start();
  264.         }
  265.     }  // start()
  266.  
  267.  
  268.     public void stop() {
  269.         myThread.stop();
  270.         myThread = null;
  271.     }  // stop()
  272.  
  273.  
  274.     public void run() {
  275.         while (myThread != null) {
  276.             try {
  277.                 Thread.sleep(speed);
  278.             } catch (InterruptedException e) {}
  279.             repaint();
  280.         }
  281.     }  // run()
  282.  
  283.  
  284.     public void update(Graphics g) {
  285.         offgraphics.setColor(color.baseColor);
  286.         offgraphics.fillRect(0, 0, dim.width, dim.height);
  287.  
  288.         int x_off = (shaXOff < 0) ? (-shaXOff + 4) : 4;
  289.         int y_off = 1 + strInfo.ascent - ((shaYOff < 0) ? shaYOff : 0);
  290.  
  291.         for (int i = 0; i < strInfo.length; i++) {
  292.             int x = x_off + (int)(Math.random() * maxXOff);
  293.             int y = y_off + (int)(Math.random() * maxYOff);
  294.             if (shadow) {
  295.                 offgraphics.setColor(color.shadowColor);
  296.                 offgraphics.drawChars(strInfo.separated, i, 1, x + shaXOff, y + shaYOff);
  297.             }
  298.             offgraphics.setColor(color.textColor[i]);
  299.             offgraphics.drawChars(strInfo.separated, i, 1, x, y);
  300.             x_off += strInfo.charWidth[i];
  301.       }
  302.  
  303.       g.drawImage(offscreen, 0, 0, null);
  304.     }  // update()
  305.  
  306.  
  307.     public void paint(Graphics g) {
  308.       g.drawImage(offscreen, 0, 0, null);
  309.     }  // paint()
  310.  
  311.  
  312.     public boolean mouseEnter(Event e, int x, int y){
  313.         if (url != null)
  314.             showStatus(url.toString());
  315.  
  316.         return true;
  317.     }  // mouseEnter()
  318.  
  319.  
  320.     public boolean mouseExit(Event e, int x, int y){
  321.         if(url != null)
  322.             showStatus("");
  323.  
  324.         return true;
  325.     }  // mouseExit()
  326.  
  327.  
  328.     public boolean mouseDown(java.awt.Event evt, int x, int y) {
  329.         if (url != null) {
  330.             if (target != null)
  331.                 getAppletContext().showDocument(url, target);
  332.             else
  333.                 getAppletContext().showDocument(url);
  334.  
  335.             showStatus("Opening document, please wait.");
  336.         }
  337.  
  338.         return true;
  339.     }  // mouseDown()
  340.  
  341.  
  342.     public String getAppletInfo() {
  343.         return "DancingText 1.0 by Stanley Poon - 12 Feb 1996";
  344.     }
  345.  
  346. }  // DancingText
  347.  
  348.  
  349.  
  350. // DancingText.java
  351.