home *** CD-ROM | disk | FTP | other *** search
/ AOL File Library: 12,000 to 12,999 / 12000.zip / AOLDLs / Online-Tools / Java-Applets / JAVAAPPS.lzh / JAVAAPPS / FAN / FAN.EXE / Fan.java < prev   
Encoding:
Java Source  |  1996-06-09  |  9.2 KB  |  258 lines

  1. /*
  2.  
  3. =========
  4.  
  5.  TEXTFAN 
  6.  
  7. =========
  8.  
  9.  
  10.  
  11. Author: Leon Cho
  12.  
  13. Date: May 1996
  14.  
  15.  
  16.  
  17. Description:
  18.  
  19.   Draws a text string on the screen by "fanning out" from a central point and fades
  20.  
  21.   in text color as it moves.
  22.  
  23.  
  24.  
  25. Syntax:
  26.  
  27.   To use, embed the following in your HTML page:
  28.  
  29.  
  30.  
  31.   <applet code=Fan.class width=600 height=30>
  32.  
  33.   <param name=TEXT VALUE="Insert your text here!">
  34.  
  35.   <param name=SPEED VALUE="4">
  36.  
  37.   <param name=SPACING VALUE="4">
  38.  
  39.   <param name=VSPACING VALUE="20">
  40.  
  41.   <param name=FONT VALUE="Courier">
  42.  
  43.   <param name=FONTSIZE VALUE="22">
  44.  
  45.   </applet>
  46.  
  47.  
  48.  
  49. Parameter description:
  50.  
  51.   TEXT: specifies the text string to draw
  52.  
  53.   SPEED: specifies the distance in pixels to move the text on each frame of animation
  54.  
  55.   SPACING: Specifies the amount of spact to allot for each letter
  56.  
  57.             (NOTE: this number is a multiple of the SPEED parameter which sets
  58.  
  59.                    the basic unit of movement.  For example, if the SPEED parameter
  60.  
  61.                    is set to 3 and the SPACING is set to 4, each letter is alloted 
  62.  
  63.              3*4=12 pixels)
  64.  
  65.   VSPACING: Specifies the number of pixels that space the string from the top of the applet
  66.  
  67.   FONT: Specifies the font to use for drawing the string
  68.  
  69.         (NOTE: You must use a fixed-sized font such as Courier for this to work.
  70.  
  71.                If the font is not fixed, this effect is not aesthetically pleasing)
  72.  
  73.   FONTSIZE: Specifies the size of the font
  74.  
  75. */
  76.  
  77.  
  78.  
  79. import java.awt.Graphics;
  80.  
  81. import java.awt.Color;
  82.  
  83. import java.awt.Font;
  84.  
  85. import java.lang.String;
  86.  
  87. import java.awt.Image;
  88.  
  89. import java.lang.Integer;
  90.  
  91.  
  92.  
  93. public class Fan extends java.applet.Applet implements Runnable {
  94.  
  95.     Thread runner;
  96.  
  97.     
  98.  
  99.     String fontName;
  100.  
  101.     int fontSize;
  102.  
  103.     Font textFont;
  104.  
  105.     int pixelSize;
  106.  
  107.     
  108.  
  109.     char[] textArray;         // Holds the characters of the text string
  110.  
  111.     int[] positionArray;     // Specifies the current position of each character
  112.  
  113.                     //   in the text string
  114.  
  115.     int[] finalPositionArray; // Holds the final position of each
  116.  
  117.                     //   character in the text string
  118.  
  119.     boolean[] movingArray;    // Specifies whether each character in the text
  120.  
  121.                      //   string is currently moving
  122.  
  123.     boolean doneMovingText = false; // Specifies whether all characters are done moving
  124.  
  125.  
  126.  
  127.     Image bufferScreenImg;      // Buffer screen image (double-buffered animation)
  128.  
  129.     Graphics bufferScreenGfx; // Buffer screen graphics (double-buffered animation)
  130.  
  131.  
  132.  
  133.     String textString;        // Holds the text string
  134.  
  135.     int textStringLength;    // Holds the length of the text string
  136.  
  137.     Color textColor;          // Holds the color of the text string
  138.  
  139.     int redVal, greenVal, blueVal;    // Specifies the current color of the text being drawn
  140.  
  141.  
  142.  
  143.     int textStringLeftMidPoint;       // Holds the index of the character to the left
  144.  
  145.                         //   of the midpoint of the string
  146.  
  147.     int textStringRightMidPoint;    // Holds the index of the character to the right
  148.  
  149.                         //   of the midpoint of the string
  150.  
  151.  
  152.  
  153.     int textSpacing;         // Specifes the amount of space alloted for each character
  154.  
  155.                      //   of text (as a multiple of pixelSize)
  156.  
  157.     int appletVPadding;          // Specifies the vertical position in pixels from the top
  158.  
  159.                     //   of the applet from which to draw the text
  160.  
  161.     int appletHorizCenter;    // Holds the center position of the applet in pixels
  162.  
  163.     int appletWidth;        // Holds the width of the applet in pixels
  164.  
  165.     int appletHeight;        // Holds the height of the applet in pixels
  166.  
  167.  
  168.  
  169.     public void init() {
  170.  
  171.         String textString = getParameter("TEXT");
  172.  
  173.       if (textString == null) 
  174.  
  175.           textString = "INSERT YOUR TEXT HERE"; 
  176.  
  177.         textStringLength = textString.length();
  178.  
  179.  
  180.  
  181.       String paramSpeed = getParameter("SPEED");
  182.  
  183.         if (paramSpeed == null) 
  184.  
  185.            pixelSize = 4;
  186.  
  187.         else
  188.  
  189.            pixelSize =  Integer.parseInt(paramSpeed);
  190.  
  191.  
  192.  
  193.         String paramTextSpacing = getParameter("SPACING");
  194.  
  195.         if (paramTextSpacing == null) 
  196.  
  197.            textSpacing = 4 * pixelSize;
  198.  
  199.         else
  200.  
  201.           textSpacing = Integer.parseInt(paramTextSpacing) * pixelSize;
  202.  
  203.  
  204.  
  205.         String paramVSpacing = getParameter("VSPACING");
  206.  
  207.         if (paramTextSpacing == null)
  208.  
  209.            appletVPadding = 20;
  210.  
  211.         else
  212.  
  213.            appletVPadding = Integer.parseInt(paramVSpacing);
  214.  
  215.  
  216.  
  217.         String paramFont = getParameter("FONT");
  218.  
  219.         if (paramFont == null)
  220.  
  221.            fontName = "Courier";
  222.  
  223.         else
  224.  
  225.            fontName = paramFont;
  226.  
  227.  
  228.  
  229.         String paramFontSize = getParameter("FONTSIZE");
  230.  
  231.         if (paramFontSize == null)
  232.  
  233.           fontSize = 22;
  234.  
  235.         else
  236.  
  237.           fontSize = Integer.parseInt(paramFontSize);
  238.  
  239.  
  240.  
  241.         textFont = new Font(fontName,Font.BOLD,fontSize);  // Must use fixed font
  242.  
  243.  
  244.  
  245.         appletWidth = this.size().width;
  246.  
  247.         appletHeight = this.size().height;
  248.  
  249.       appletHorizCenter = appletWidth / 2;
  250.  
  251.  
  252.  
  253.         if ((textStringLength % 2) == 0) {
  254.  
  255.            textStringLeftMidPoint = (textStringLength / 2);
  256.  
  257.            textStringRightMidPoint = (textStringLength / 2) + 1;
  258.  
  259.         } else {
  260.  
  261.            textStringLeftMidPoint = (textStringLength / 2);
  262.  
  263.            textStringRightMidPoint = (this.textStringLength / 2) + 2;
  264.  
  265.         }
  266.  
  267.  
  268.  
  269.       textArray = new char[textStringLength];
  270.  
  271.  
  272.  
  273.       positionArray = new int[textStringLength];
  274.  
  275.       for (int i = 0; i < textStringLength; i++) {
  276.  
  277.           positionArray[i] = this.appletHorizCenter - (textSpacing / 2);
  278.  
  279.         }
  280.  
  281.  
  282.  
  283.       finalPositionArray = new int[textStringLength];
  284.  
  285.         int leftPadding = (appletWidth - (textStringLength * textSpacing)) / 2;
  286.  
  287.         for (int i = 0; i < textStringLength; i++) {
  288.  
  289.            finalPositionArray[i] = (i * textSpacing) + leftPadding;
  290.  
  291.         }
  292.  
  293.         
  294.  
  295.         movingArray = new boolean[textStringLength];
  296.  
  297.         for (int i = 1; i < textStringLength - 1; i++) {
  298.  
  299.           movingArray[i] = false;
  300.  
  301.         }
  302.  
  303.         movingArray[0] = true;
  304.  
  305.         movingArray[textStringLength - 1] = true;        
  306.  
  307.  
  308.  
  309.         textString.getChars(0,textStringLength,textArray,0);
  310.  
  311.  
  312.  
  313.       //Make hidden screen for double buffering of animation
  314.  
  315.       bufferScreenImg = createImage(appletWidth,appletHeight);
  316.  
  317.       bufferScreenGfx = bufferScreenImg.getGraphics();
  318.  
  319.     }
  320.  
  321.  
  322.  
  323.  
  324.  
  325.     public void start() {
  326.  
  327.         if (runner == null); {
  328.  
  329.             runner = new Thread(this);
  330.  
  331.             runner.start();
  332.  
  333.         }
  334.  
  335.     }
  336.  
  337.  
  338.  
  339.     public void stop() {
  340.  
  341.         if (runner != null) {
  342.  
  343.             runner.stop();
  344.  
  345.             runner = null;
  346.  
  347.         }
  348.  
  349.     }
  350.  
  351.  
  352.  
  353.     public void run() {
  354.  
  355.       int appletHorizMax = 500;
  356.  
  357.         int stepCounter = 0;
  358.  
  359.  
  360.  
  361.         setBackground(Color.black);
  362.  
  363.      
  364.  
  365.         while (! doneMovingText) {
  366.  
  367.  
  368.  
  369.         for (int i = 1; i < this.textStringLeftMidPoint; i++) {
  370.  
  371.               if (i * this.textSpacing == stepCounter) {
  372.  
  373.             movingArray[i] = true;
  374.  
  375.             }
  376.  
  377.             }
  378.  
  379.  
  380.  
  381.             for (int i = this.textStringRightMidPoint - 1; i < (this.textStringLength - 1); i++) {
  382.  
  383.               int tempIndex = this.textStringLength - i - 1;
  384.  
  385.               if (tempIndex * this.textSpacing == stepCounter) {
  386.  
  387.                   movingArray[i] = true;
  388.  
  389.               }
  390.  
  391.             }
  392.  
  393.             stepCounter += this.pixelSize;
  394.  
  395.  
  396.  
  397.           for (int i = 0; i < this.textStringLength; i++) {
  398.  
  399.              if (positionArray[i] == finalPositionArray[i]) {
  400.  
  401.              movingArray[i] = false;
  402.  
  403.                }
  404.  
  405.             }
  406.  
  407.  
  408.  
  409.           if ((movingArray[0] == false) && (movingArray[this.textStringLength - 1] == false)) {
  410.  
  411.             this.doneMovingText = true;
  412.  
  413.             }
  414.  
  415.  
  416.  
  417.         for (int i = 0; i < this.textStringLeftMidPoint; i++) {
  418.  
  419.               if (movingArray[i] == true) {
  420.  
  421.                 positionArray[i] -= this.pixelSize;
  422.  
  423.               }
  424.  
  425.             }
  426.  
  427.           
  428.  
  429.         for (int i = this.textStringRightMidPoint - 1; i < this.textStringLength; i++) {
  430.  
  431.               if (movingArray[i] == true) {
  432.  
  433.             positionArray[i] += this.pixelSize;
  434.  
  435.               }
  436.  
  437.           }
  438.  
  439.  
  440.  
  441.           repaint();
  442.  
  443.             try{ Thread.sleep(100); }
  444.  
  445.             catch (InterruptedException e) { }
  446.  
  447.            
  448.  
  449.         }
  450.  
  451.     }
  452.  
  453.  
  454.  
  455.     public void update(Graphics g) {
  456.  
  457.       paint(g);
  458.  
  459.     }
  460.  
  461.  
  462.  
  463.     public void paint(Graphics screenGfx) {
  464.  
  465.         bufferScreenGfx.setColor(Color.black);
  466.  
  467.         bufferScreenGfx.fillRect(0,0,this.appletWidth,this.appletHeight);
  468.  
  469.  
  470.  
  471.       bufferScreenGfx.setFont(textFont);
  472.  
  473.  
  474.  
  475.       for (int i=0; i < this.textStringLength; i++) {
  476.  
  477.         //Draw shadow for text
  478.  
  479.           bufferScreenGfx.setColor(Color.white);
  480.  
  481.           bufferScreenGfx.drawChars(textArray,i,1,positionArray[i] - 1,this.appletVPadding + 1);
  482.  
  483.  
  484.  
  485.         //Draw foreground text
  486.  
  487.         //  Set color of text to fade in (as % of total distance from center that text will move)
  488.  
  489.           float leftPadding = (float)(appletWidth - (textStringLength * textSpacing)) / 2;
  490.  
  491.           redVal = (int)(((Math.abs((float)this.appletHorizCenter - (float)positionArray[i])) / ((float)this.appletHorizCenter - leftPadding)) * 255.0);
  492.  
  493.           
  494.  
  495.         textColor = new Color(redVal,50,0);
  496.  
  497.           
  498.  
  499.          bufferScreenGfx.setColor(textColor);
  500.  
  501.           bufferScreenGfx.drawChars(textArray,i,1,positionArray[i],this.appletVPadding);
  502.  
  503.       }  
  504.  
  505.  
  506.  
  507.       // Draw buffer screen to screen (double-buffered animation)
  508.  
  509.         screenGfx.drawImage(bufferScreenImg,0,0,this);
  510.  
  511.     }
  512.  
  513. }
  514.  
  515.