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 / ANIMTEXT / ANIMTEXT.EXE / AnimText.java < prev   
Encoding:
Java Source  |  1996-04-05  |  10.5 KB  |  393 lines

  1.  
  2.  
  3.  
  4.  
  5. import java.awt.*;
  6.  
  7. import java.util.StringTokenizer;
  8.  
  9.  
  10.  
  11. /**
  12.  
  13.   Another applet littering my learning curve :-)  Loosely based on the NervousText applet
  14.  
  15.   from Sun.
  16.  
  17.  
  18.  
  19.   <APPLET CODE="AnimText" WIDTH=600 HEIGHT=400>
  20.  
  21.   <PARAM NAME=text VALUE="Hello">
  22.  
  23.   <PARAM NAME=font VALUE="TimesRoman">
  24.  
  25.   <PARAM NAME=type VALUE=1>
  26.  
  27.   <PARAM NAME=min VALUE=6>
  28.  
  29.   <PARAM NAME=max VALUE=28>
  30.  
  31.   <PARAM NAME=naptime VALUE=100>
  32.  
  33.   <PARAM NAME=align VALUE=left>
  34.  
  35.   </APPLET>
  36.  
  37.  
  38.  
  39.   Parameters:
  40.  
  41.  
  42.  
  43.   text - the text to display
  44.  
  45.   font - the font to render the text in
  46.  
  47.   style - whether PLAIN, BOLD or ITALIC
  48.  
  49.   step - increments in font size each iteration
  50.  
  51.   type - blink (all chars same rate)
  52.  
  53.          wave ("the wave")
  54.  
  55.          random (random)
  56.  
  57.   align - left, center or right
  58.  
  59.   bgimage - background image URL
  60.  
  61.   bgcolor - background color (as RGB)
  62.  
  63.   fgcolor - foreground color
  64.  
  65.   naptime - time between iterations in millisecs
  66.  
  67.   min  - minimum font size
  68.  
  69.   max  - maximum font size
  70.  
  71.  
  72.  
  73.   @Author: Suresh Srinivasan (suresh@thomtech.com)
  74.  
  75.   @Date: Dec 1995
  76.  
  77.   @Version: 0.1
  78.  
  79.   */
  80.  
  81. public class AnimText extends java.applet.Applet implements Runnable {
  82.  
  83.  
  84.  
  85.     public static final int TYPE_BLINK = 1;
  86.  
  87.     public static final int TYPE_WAVE = 2;
  88.  
  89.     public static final int TYPE_RANDOM = 3;
  90.  
  91.  
  92.  
  93.     public static final int ALIGN_LEFT = 1;
  94.  
  95.     public static final int ALIGN_CENTER = 2;
  96.  
  97.     public static final int ALIGN_RIGHT = 3;
  98.  
  99.  
  100.  
  101.     char textChars[];        /* the text to display as a char array */
  102.  
  103.     Thread thread;
  104.  
  105.     int type;
  106.  
  107.     int style;
  108.  
  109.     int defaultMin=8;
  110.  
  111.     int defaultMax=28;
  112.  
  113.     int max;
  114.  
  115.     int min;
  116.  
  117.     int defaultStep = 2;
  118.  
  119.     int step;
  120.  
  121.     int align;
  122.  
  123.     String rgbDelimiter = ":,.";
  124.  
  125.     StringTokenizer st;
  126.  
  127.     Color fgColor;
  128.  
  129.     Color bgColor;
  130.  
  131.     boolean threadSuspended = false;
  132.  
  133.     static final String defaultString = "Welcome to Java!";
  134.  
  135.     String fontString;
  136.  
  137.     Font fonts[];
  138.  
  139.     int current[];
  140.  
  141.     int direction[];
  142.  
  143.     int charWidth[];            /* width of each character in the preferred font */
  144.  
  145.     int charHeight;            /* height of character */
  146.  
  147.     boolean resized = false;
  148.  
  149.     boolean readyToPaint = true;
  150.  
  151.     int naptime;
  152.  
  153.     int defaultNaptime = 100;
  154.  
  155.     int Width;
  156.  
  157.     int Height;
  158.  
  159.     int defaultWidth = 300;
  160.  
  161.     int defaultHeight = 100;
  162.  
  163.     int maxWidth = 600;
  164.  
  165.     int maxHeight = 400;
  166.  
  167.     int n;
  168.  
  169.     Image offI;
  170.  
  171.     Graphics offG;
  172.  
  173.  
  174.  
  175.     int totalWidth;
  176.  
  177.     int leader = 10; /* leading space */
  178.  
  179.  
  180.  
  181.     public void init() {
  182.  
  183.     String s;
  184.  
  185.     Integer intObj;
  186.  
  187.  
  188.  
  189.     s = getParameter("text");
  190.  
  191.     if (s == null)
  192.  
  193.         s = defaultString;
  194.  
  195.     textChars =  new char[s.length()];
  196.  
  197.     s.getChars(0 , s.length(), textChars, 0);
  198.  
  199.  
  200.  
  201.     s = getParameter("font");
  202.  
  203.     if (s == null)
  204.  
  205.         fontString = "TimesRoman";
  206.  
  207.     else if (s.equalsIgnoreCase("TimesRoman"))
  208.  
  209.         fontString = "TimesRoman";
  210.  
  211.     else if (s.equalsIgnoreCase("Courier"))
  212.  
  213.         fontString = "Courier";
  214.  
  215.     else if (s.equalsIgnoreCase("Helvetica"))
  216.  
  217.         fontString = "Helvetica";
  218.  
  219.     else if (s.equalsIgnoreCase("Dialog"))
  220.  
  221.         fontString = "Dialog";
  222.  
  223.     else
  224.  
  225.         fontString = "TimesRoman";
  226.  
  227.  
  228.  
  229.     s = getParameter("style");
  230.  
  231.     if (s == null)
  232.  
  233.         style = Font.PLAIN;
  234.  
  235.     else if (s.equalsIgnoreCase("PLAIN"))
  236.  
  237.         style = Font.PLAIN;
  238.  
  239.     else if (s.equalsIgnoreCase("BOLD"))
  240.  
  241.         style = Font.BOLD;
  242.  
  243.     else if (s.equalsIgnoreCase("ITALIC"))
  244.  
  245.         style = Font.ITALIC;
  246.  
  247.     else
  248.  
  249.         style = Font.PLAIN;
  250.  
  251.  
  252.  
  253.     s = getParameter("type");
  254.  
  255.     if (s == null)
  256.  
  257.         type = TYPE_WAVE;
  258.  
  259.     else if (s.equalsIgnoreCase("blink"))
  260.  
  261.         type = TYPE_BLINK;
  262.  
  263.     else if (s.equalsIgnoreCase("wave"))
  264.  
  265.         type = TYPE_WAVE;
  266.  
  267.     else if (s.equalsIgnoreCase("random"))
  268.  
  269.         type = TYPE_RANDOM;
  270.  
  271.     else
  272.  
  273.         type = TYPE_WAVE;
  274.  
  275.  
  276.  
  277.     s = getParameter("align");
  278.  
  279.     if (s == null)
  280.  
  281.         align = ALIGN_CENTER;
  282.  
  283.     else if (s.equalsIgnoreCase("left"))
  284.  
  285.         align = ALIGN_LEFT;
  286.  
  287.     else if (s.equalsIgnoreCase("center"))
  288.  
  289.         align = ALIGN_CENTER;
  290.  
  291.     else if (s.equalsIgnoreCase("right"))
  292.  
  293.         align = ALIGN_RIGHT;
  294.  
  295.     else
  296.  
  297.         align = ALIGN_CENTER;
  298.  
  299.  
  300.  
  301.     try {
  302.  
  303.         intObj = new Integer(getParameter("width"));
  304.  
  305.         Width = intObj.intValue();
  306.  
  307.     } catch (Exception e) {
  308.  
  309.         Width = defaultWidth;
  310.  
  311.     }
  312.  
  313.  
  314.  
  315.     try {
  316.  
  317.         intObj = new Integer(getParameter("height"));
  318.  
  319.         Height = intObj.intValue();
  320.  
  321.     } catch (Exception e) {
  322.  
  323.         Height = defaultHeight;
  324.  
  325.     }
  326.  
  327.  
  328.  
  329.     try {
  330.  
  331.         intObj = new Integer(getParameter("min"));
  332.  
  333.         min = intObj.intValue();
  334.  
  335.     } catch (Exception e) {
  336.  
  337.         min = defaultMin;
  338.  
  339.     }
  340.  
  341.  
  342.  
  343.     try {
  344.  
  345.         intObj = new Integer(getParameter("max"));
  346.  
  347.         max = intObj.intValue();
  348.  
  349.     } catch (Exception e) {
  350.  
  351.         max = defaultMax;
  352.  
  353.     }
  354.  
  355.     if (min >= max || min <= 0) {
  356.  
  357.         min = defaultMin;
  358.  
  359.         max = defaultMax;
  360.  
  361.     }
  362.  
  363.  
  364.  
  365.     try {
  366.  
  367.         intObj = new Integer(getParameter("step"));
  368.  
  369.         step = intObj.intValue();
  370.  
  371.     } catch (Exception e) {
  372.  
  373.         step = defaultStep;
  374.  
  375.     }
  376.  
  377.     if (step > (max-min)/2) step = defaultStep;
  378.  
  379.  
  380.  
  381.     try {
  382.  
  383.         intObj = new Integer(getParameter("naptime"));
  384.  
  385.         naptime = intObj.intValue();
  386.  
  387.     } catch (Exception e) {
  388.  
  389.         naptime = defaultNaptime;
  390.  
  391.     }
  392.  
  393.     if (naptime <= 0) naptime = defaultNaptime;
  394.  
  395.  
  396.  
  397.     s = getParameter("fgColor");
  398.  
  399.     if (s != null) st = new StringTokenizer(s, rgbDelimiter);
  400.  
  401.  
  402.  
  403.     if (s == null)
  404.  
  405.         fgColor = Color.black;
  406.  
  407.     else if (s.equalsIgnoreCase("red"))
  408.  
  409.         fgColor = Color.red;
  410.  
  411.     else if (s.equalsIgnoreCase("blue"))
  412.  
  413.         fgColor = Color.blue;
  414.  
  415.     else if (s.equalsIgnoreCase("green"))
  416.  
  417.         fgColor = Color.green;
  418.  
  419.     else if (s.equalsIgnoreCase("yellow"))
  420.  
  421.         fgColor = Color.yellow;
  422.  
  423.     else if (s.equalsIgnoreCase("white"))
  424.  
  425.         fgColor = Color.white;
  426.  
  427.     else if (s.equalsIgnoreCase("orange"))
  428.  
  429.         fgColor = Color.orange;
  430.  
  431.     else if (s.equalsIgnoreCase("cyan"))
  432.  
  433.         fgColor = Color.cyan;
  434.  
  435.     else if (s.equalsIgnoreCase("magenta"))
  436.  
  437.         fgColor = Color.magenta;
  438.  
  439.     else if (st.countTokens() == 3) {
  440.  
  441.         Integer r = new Integer(st.nextToken());
  442.  
  443.         Integer g = new Integer(st.nextToken());
  444.  
  445.         Integer b = new Integer(st.nextToken());
  446.  
  447.         fgColor = new Color(r.intValue(), g.intValue(), b.intValue());
  448.  
  449.     } else
  450.  
  451.         fgColor = Color.black;
  452.  
  453.  
  454.  
  455.     s = getParameter("bgColor");
  456.  
  457.     if (s != null) st = new StringTokenizer(s, rgbDelimiter);
  458.  
  459.  
  460.  
  461.     if (s == null)
  462.  
  463.         bgColor = Color.lightGray;
  464.  
  465.     else if (s.equalsIgnoreCase("red"))
  466.  
  467.         bgColor = Color.red;
  468.  
  469.     else if (s.equalsIgnoreCase("blue"))
  470.  
  471.         bgColor = Color.blue;
  472.  
  473.     else if (s.equalsIgnoreCase("green"))
  474.  
  475.         bgColor = Color.green;
  476.  
  477.     else if (s.equalsIgnoreCase("yellow"))
  478.  
  479.         bgColor = Color.yellow;
  480.  
  481.     else if (s.equalsIgnoreCase("white"))
  482.  
  483.         bgColor = Color.white;
  484.  
  485.     else if (s.equalsIgnoreCase("orange"))
  486.  
  487.         bgColor = Color.orange;
  488.  
  489.     else if (s.equalsIgnoreCase("cyan"))
  490.  
  491.         bgColor = Color.cyan;
  492.  
  493.     else if (s.equalsIgnoreCase("magenta"))
  494.  
  495.         bgColor = Color.magenta;
  496.  
  497.     else if (st.countTokens() == 3) {
  498.  
  499.         Integer r = new Integer(st.nextToken());
  500.  
  501.         Integer g = new Integer(st.nextToken());
  502.  
  503.         Integer b = new Integer(st.nextToken());
  504.  
  505.         bgColor = new Color(r.intValue(), g.intValue(), b.intValue());
  506.  
  507.     } else
  508.  
  509.         bgColor = Color.lightGray; 
  510.  
  511.  
  512.  
  513. /* pre allocate stuff */
  514.  
  515.     n = max-min;
  516.  
  517.     if (n>0) {
  518.  
  519.         fonts = new Font[n];
  520.  
  521.         current = new int[textChars.length];
  522.  
  523.         direction = new int[textChars.length];
  524.  
  525.         charWidth = new int[textChars.length];
  526.  
  527.     }
  528.  
  529.     for (int i=0; i<n; i++) {
  530.  
  531.         fonts[i] = new Font(fontString, style, min+i);          
  532.  
  533.     }
  534.  
  535.     for (int i=0; i<textChars.length; i++) {
  536.  
  537.         switch (type) {
  538.  
  539.         case TYPE_BLINK:
  540.  
  541.         current[i] = 0;
  542.  
  543.         direction[i] = 1;
  544.  
  545.         break;
  546.  
  547.         case TYPE_WAVE:
  548.  
  549.         current[i] = (int)(Math.sin((double)i/(double)textChars.length*Math.PI)*(float)(n-1));
  550.  
  551.         direction[i] = 1;
  552.  
  553.         break; 
  554.  
  555.         case TYPE_RANDOM:
  556.  
  557.             current[i] = (int)(Math.random()*(float)(n));
  558.  
  559.         direction[i] = 1;
  560.  
  561.         break;
  562.  
  563.         default:
  564.  
  565.         }
  566.  
  567.         if (current[i] >= n-1) direction[i] = -1;
  568.  
  569.     }
  570.  
  571.  
  572.  
  573. /* offscreen graphics context */
  574.  
  575.     try {
  576.  
  577.         offI = createImage(maxWidth, maxHeight);
  578.  
  579.         offG = offI.getGraphics();
  580.  
  581.     } catch (Exception e) {
  582.  
  583.         offG = null;
  584.  
  585.     }
  586.  
  587.     }
  588.  
  589.  
  590.  
  591.     public void start() {
  592.  
  593.     if (thread == null) {
  594.  
  595.         thread = new Thread(this);
  596.  
  597.         thread.start();
  598.  
  599.     }
  600.  
  601.     }
  602.  
  603.  
  604.  
  605.     public void run() {
  606.  
  607.     while ((n>0) && (thread != null)) {
  608.  
  609.         repaint();
  610.  
  611.         try { Thread.sleep(naptime); } catch (Exception e) { }
  612.  
  613.         readyToPaint = false;
  614.  
  615.         next();
  616.  
  617.         readyToPaint = true;
  618.  
  619.     }
  620.  
  621.     }
  622.  
  623.  
  624.  
  625. /* next iteration */
  626.  
  627.     public void next() {
  628.  
  629.         for (int i=0; i<textChars.length; i++) {
  630.  
  631.         current[i] += step*direction[i];
  632.  
  633.         if (current[i] >= n-1) {
  634.  
  635.                 current[i] = 2*n-2-current[i];
  636.  
  637.                 direction[i] = -1;
  638.  
  639.             }
  640.  
  641.             if (current[i] <= 0) {
  642.  
  643.                 current[i] = Math.abs(current[i]);
  644.  
  645.                 direction[i] = 1;
  646.  
  647.             }
  648.  
  649.     }
  650.  
  651.     }
  652.  
  653.  
  654.  
  655. /* override the update method to reduce flashing */
  656.  
  657.     public void update(Graphics g) {
  658.  
  659.     if (readyToPaint)
  660.  
  661.         paint(g);
  662.  
  663.     }
  664.  
  665.  
  666.  
  667.     public void paint(Graphics g) {
  668.  
  669.     if (offG != null) {
  670.  
  671.         paintApplet(offG);
  672.  
  673.         g.drawImage(offI, 0, 0, this);
  674.  
  675.     } else {
  676.  
  677.         paintApplet(g);
  678.  
  679.     }
  680.  
  681.     }
  682.  
  683.  
  684.  
  685.     public void paintApplet(Graphics g) {
  686.  
  687.     if (!resized) {
  688.  
  689.         totalWidth = 0;
  690.  
  691.         g.setFont(fonts[n-1]); /* biggest font */
  692.  
  693.         for (int i=0; i<textChars.length; i++) {
  694.  
  695.         charWidth[i] = g.getFontMetrics().charWidth(textChars[i]);
  696.  
  697.         totalWidth += charWidth[i];
  698.  
  699.         }
  700.  
  701.         if (totalWidth>maxWidth) totalWidth = maxWidth;
  702.  
  703.         charHeight = g.getFontMetrics().getHeight();
  704.  
  705.         if (charHeight>maxHeight) charHeight = maxHeight;
  706.  
  707.         resize(Width, Height);
  708.  
  709.         resized = true;
  710.  
  711.     }
  712.  
  713.  
  714.  
  715.     int pos = 0;
  716.  
  717.  
  718.  
  719.     switch (align) {
  720.  
  721.     case ALIGN_LEFT:
  722.  
  723.         pos = leader; break;
  724.  
  725.     case ALIGN_CENTER:
  726.  
  727.         pos = (size().width-totalWidth)/2; break;
  728.  
  729.     case ALIGN_RIGHT:
  730.  
  731.         pos = (size().width-totalWidth-leader); break;
  732.  
  733.     default:
  734.  
  735.     }
  736.  
  737.  
  738.  
  739.     g.setColor(bgColor);
  740.  
  741.     g.fillRect(0, 0, size().width-1, size().height-1);
  742.  
  743.     g.setColor(fgColor);
  744.  
  745.     for(int i=0; i<textChars.length; i++) {
  746.  
  747.         g.setFont(fonts[current[i]]);
  748.  
  749.         g.drawChars(textChars, i, 1, pos, (size().height+charHeight)/2);
  750.  
  751.         pos += charWidth[i];
  752.  
  753.     }
  754.  
  755.     }
  756.  
  757.  
  758.  
  759.     public void stop() {
  760.  
  761.     thread = null;
  762.  
  763.     }
  764.  
  765.  
  766.  
  767.     public boolean mouseDown(Event e, int x, int y) {
  768.  
  769.         if (threadSuspended)
  770.  
  771.             thread.resume();
  772.  
  773.         else
  774.  
  775.             thread.suspend();
  776.  
  777.         threadSuspended = !threadSuspended;
  778.  
  779.     return true;
  780.  
  781.     }
  782.  
  783. }
  784.  
  785.