home *** CD-ROM | disk | FTP | other *** search
/ Chip 1999 September / Chip_1999-09_cd.bin / internet / JSLib / applets / 0107 / JTAnimText.java < prev    next >
Text File  |  1998-08-22  |  11KB  |  744 lines

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