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 / JITTERTE / JITTERTE.EXE / JitterText.java < prev   
Encoding:
Java Source  |  1996-04-06  |  6.2 KB  |  201 lines

  1. /*  JitterText
  2.  
  3.  
  4.  
  5.  
  6.  
  7.     revised by W.Giel 2 Mar 96
  8.  
  9.  
  10.     Revised init(), revised paint(),added update() w/buffered image,
  11.  
  12.  
  13.     added color and speed params, added code to optimize font height.
  14.  
  15.  
  16.  
  17.  
  18.  
  19.     revised by W.Giel 8 Mar 96
  20.  
  21.  
  22.     Added random colors
  23.  
  24.  
  25.  
  26.  
  27.  
  28.     based on NervousText Applet
  29.  
  30.  
  31.     by  Daniel Wyszynski 
  32.  
  33.  
  34.         Center for Applied Large-Scale Computing (CALC) 
  35.  
  36.  
  37.         04-12-95
  38.  
  39.  
  40.         
  41.  
  42.  
  43.     revised by kwalrath 5-9-95
  44.  
  45.  
  46.  
  47.  
  48.  
  49.  
  50.  
  51.  
  52.     Parameters: TEXTCOLOR - string - Hex RGB triplet - default: #000000 (black)
  53.  
  54.  
  55.                 BGCOLOR   - string - Hex RGB triplet - default: #C0C0C0 (light gray)
  56.  
  57.  
  58.                 TEXT      - string - message - degault: "Java Power!"
  59.  
  60.  
  61.                 SPEED     - int    - delay in millisecond - default: 200
  62.  
  63.  
  64.                 RANDOMCOLOR    - int    - non-zero=true;
  65.  
  66.  
  67.  
  68.  
  69.  
  70.  
  71.  
  72.  
  73. */
  74.  
  75.  
  76.  
  77.  
  78.  
  79. import java.awt.*;
  80.  
  81.  
  82. import java.applet.*;
  83.  
  84.  
  85. import java.lang.*;
  86.  
  87.  
  88.  
  89.  
  90.  
  91. public class JitterText extends java.applet.Applet implements Runnable
  92.  
  93.  
  94. {
  95.  
  96.  
  97.     static final int MAXCLORS=10;
  98.  
  99.  
  100.     String s = null;
  101.  
  102.  
  103.     Thread jitterThread = null;
  104.  
  105.  
  106.  
  107.  
  108.  
  109.     boolean threadSuspended = false; //added by kwalrath
  110.  
  111.  
  112.  
  113.  
  114.  
  115.     ////////////////////
  116.  
  117.  
  118.     // Added by W.Giel
  119.  
  120.  
  121.     ////////////////////
  122.  
  123.  
  124.     int fontHeight;
  125.  
  126.  
  127.     Color textColor=null;
  128.  
  129.  
  130.     Color bgColor;
  131.  
  132.  
  133.     int speed=200;
  134.  
  135.  
  136.     FontMetrics fm;
  137.  
  138.  
  139.     int baseline;
  140.  
  141.  
  142.     Image offScrImage;
  143.  
  144.  
  145.     Graphics offScrGC;
  146.  
  147.  
  148.     boolean normal=false;
  149.  
  150.  
  151.     Font font;
  152.  
  153.  
  154.     Color randomColors[]=new Color[10];
  155.  
  156.  
  157.     boolean randomColor=false;
  158.  
  159.  
  160.  
  161.  
  162.  
  163.     /////////////////////
  164.  
  165.  
  166.     // Revised by W.Giel
  167.  
  168.  
  169.     /////////////////////
  170.  
  171.  
  172.     public void init()
  173.  
  174.  
  175.     {
  176.  
  177.  
  178.         String param;
  179.  
  180.  
  181.         Graphics g=getGraphics();
  182.  
  183.  
  184.         fontHeight=size().height - 10;
  185.  
  186.  
  187.  
  188.  
  189.  
  190.         offScrImage = createImage(size().width,size().height);
  191.  
  192.  
  193.         offScrGC = offScrImage.getGraphics();
  194.  
  195.  
  196.  
  197.  
  198.  
  199.         s = getParameter("text");
  200.  
  201.  
  202.         if (s == null) s = "Java Power!";
  203.  
  204.  
  205.  
  206.  
  207.  
  208.         int maxlen=size().width - (s.length()+1)* 5 - 10;
  209.  
  210.  
  211.         do{
  212.  
  213.  
  214.             g.setFont(new Font("TimesRoman",Font.BOLD,fontHeight));
  215.  
  216.  
  217.             fm=g.getFontMetrics();
  218.  
  219.  
  220.             if(fm.stringWidth(s)>maxlen)fontHeight--;
  221.  
  222.  
  223.         }while(fm.stringWidth(s)>maxlen);
  224.  
  225.  
  226.         baseline= size().height - fm.getMaxDescent();
  227.  
  228.  
  229.         font=new Font("TimesRoman",Font.BOLD,fontHeight);
  230.  
  231.  
  232.  
  233.  
  234.  
  235.         if(null == (param=getParameter("TEXTCOLOR")))
  236.  
  237.  
  238.             textColor=Color.black;
  239.  
  240.  
  241.         else textColor=parseColorString(param);
  242.  
  243.  
  244.  
  245.  
  246.  
  247.         if(null == (param=getParameter("BGCOLOR")))
  248.  
  249.  
  250.             bgColor=Color.lightGray;
  251.  
  252.  
  253.         else bgColor=parseColorString(param);
  254.  
  255.  
  256.         setBackground(bgColor);
  257.  
  258.  
  259.  
  260.  
  261.  
  262.         if(null != (param=getParameter("SPEED")))
  263.  
  264.  
  265.             speed=Integer.valueOf(param).intValue();
  266.  
  267.  
  268.         if(0 == speed)speed=200;
  269.  
  270.  
  271.     
  272.  
  273.  
  274.         if(null != (param=getParameter("RANDOMCOLOR")))
  275.  
  276.  
  277.             randomColor=(0==Integer.valueOf(param).intValue())? false : true;
  278.  
  279.  
  280.     
  281.  
  282.  
  283.     
  284.  
  285.  
  286.         randomColors[0]=Color.magenta;
  287.  
  288.  
  289.         randomColors[1]=Color.orange;
  290.  
  291.  
  292.         randomColors[2]=Color.red;
  293.  
  294.  
  295.         randomColors[3]=Color.white;
  296.  
  297.  
  298.         randomColors[4]=Color.yellow;
  299.  
  300.  
  301.         randomColors[5]=Color.blue;
  302.  
  303.  
  304.         randomColors[6]=Color.cyan;
  305.  
  306.  
  307.         randomColors[7]=Color.green;
  308.  
  309.  
  310.         randomColors[8]=Color.pink;
  311.  
  312.  
  313.         randomColors[9]=Color.gray;
  314.  
  315.  
  316.  
  317.  
  318.  
  319.         jitterThread=new Thread(this);
  320.  
  321.  
  322.     }
  323.  
  324.  
  325.  
  326.  
  327.  
  328.     public void start()
  329.  
  330.  
  331.     {
  332.  
  333.  
  334.         if(null != jitterThread) jitterThread.start();
  335.  
  336.  
  337.     }
  338.  
  339.  
  340.  
  341.  
  342.  
  343.     public void stop()
  344.  
  345.  
  346.     {
  347.  
  348.  
  349.         if(null != jitterThread) jitterThread.stop();
  350.  
  351.  
  352.         jitterThread = null;
  353.  
  354.  
  355.     }
  356.  
  357.  
  358.  
  359.  
  360.  
  361.     public void run()
  362.  
  363.  
  364.     {
  365.  
  366.  
  367.         while (jitterThread != null) {
  368.  
  369.  
  370.             try {Thread.sleep(200);} catch (InterruptedException e){}
  371.  
  372.  
  373.                 repaint();
  374.  
  375.  
  376.         }
  377.  
  378.  
  379.         System.exit(0);  
  380.  
  381.  
  382.      }
  383.  
  384.  
  385.  
  386.  
  387.  
  388.  
  389.  
  390.  
  391.     public boolean mouseDown(java.awt.Event evt, int x, int y)
  392.  
  393.  
  394.     {
  395.  
  396.  
  397.         if (threadSuspended) {
  398.  
  399.  
  400.             jitterThread.resume();
  401.  
  402.  
  403.             normal=false;
  404.  
  405.  
  406.         }
  407.  
  408.  
  409.         else {
  410.  
  411.  
  412.             normal=true;
  413.  
  414.  
  415.             repaint();
  416.  
  417.  
  418.             jitterThread.suspend();
  419.  
  420.  
  421.         }
  422.  
  423.  
  424.         threadSuspended = !threadSuspended;
  425.  
  426.  
  427.         return true;
  428.  
  429.  
  430.     }
  431.  
  432.  
  433.  
  434.  
  435.  
  436.  
  437.  
  438.  
  439.     ///////////////////
  440.  
  441.  
  442.     // Added by W.Giel
  443.  
  444.  
  445.     ///////////////////
  446.  
  447.  
  448.     public void paint(Graphics g)
  449.  
  450.  
  451.     {
  452.  
  453.  
  454.         if(normal){
  455.  
  456.  
  457.             g.setColor(bgColor);
  458.  
  459.  
  460.             g.fillRect(0,0,size().width,size().height);
  461.  
  462.  
  463.             g.setColor(textColor);
  464.  
  465.  
  466.                g.setFont(font);
  467.  
  468.  
  469.             g.drawString(s,(size().width-fm.stringWidth(s))/2, baseline);               
  470.  
  471.  
  472.         }
  473.  
  474.  
  475.     }
  476.  
  477.  
  478.             
  479.  
  480.  
  481.     
  482.  
  483.  
  484.     public void update(Graphics g)
  485.  
  486.  
  487.     {
  488.  
  489.  
  490.       Color color;
  491.  
  492.  
  493.     
  494.  
  495.  
  496.         offScrGC.setColor(bgColor);
  497.  
  498.  
  499.         offScrGC.fillRect(0,0,size().width,size().height);
  500.  
  501.  
  502.         offScrGC.setColor(textColor);
  503.  
  504.  
  505.         offScrGC.setFont(font);
  506.  
  507.  
  508.         if(!normal){
  509.  
  510.  
  511.             int x_coord=0;
  512.  
  513.  
  514.             for(int i=0;i<s.length();i++){
  515.  
  516.  
  517.                 if(randomColor){
  518.  
  519.  
  520.                     while(bgColor==(color=randomColors[Math.min(9,(int)(Math.random()*10))]));
  521.  
  522.  
  523.                     offScrGC.setColor(color);
  524.  
  525.  
  526.                 }
  527.  
  528.  
  529.                 x_coord += (int)(Math.random()*10);
  530.  
  531.  
  532.                 int y_coord = baseline - (int)(Math.random()*10);
  533.  
  534.  
  535.                 String substr=s.substring(i,i+1);
  536.  
  537.  
  538.                 offScrGC.drawString(substr,x_coord,y_coord);
  539.  
  540.  
  541.                 x_coord += fm.stringWidth(substr);
  542.  
  543.  
  544.             }
  545.  
  546.  
  547.         }
  548.  
  549.  
  550.         else offScrGC.drawString(s,(size().width-fm.stringWidth(s))/2, baseline);
  551.  
  552.  
  553.         
  554.  
  555.  
  556.         g.drawImage(offScrImage,0,0,this);
  557.  
  558.  
  559.     }
  560.  
  561.  
  562.  
  563.  
  564.  
  565.     private Color parseColorString(String colorString)
  566.  
  567.  
  568.     {
  569.  
  570.  
  571.         if(colorString.length()==6){
  572.  
  573.  
  574.             int R = Integer.valueOf(colorString.substring(0,2),16).intValue();
  575.  
  576.  
  577.             int G = Integer.valueOf(colorString.substring(2,4),16).intValue();
  578.  
  579.  
  580.             int B = Integer.valueOf(colorString.substring(4,6),16).intValue();
  581.  
  582.  
  583.             return new Color(R,G,B);
  584.  
  585.  
  586.         }
  587.  
  588.  
  589.         else return Color.lightGray;
  590.  
  591.  
  592.     }
  593.  
  594.  
  595. }
  596.  
  597.  
  598.  
  599.  
  600.  
  601.