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 / RAINB / RAINB.EXE / RainbowText.java < prev   
Encoding:
Java Source  |  1996-05-29  |  2.2 KB  |  101 lines

  1. /*  Glenn Richard, Center for High Pressure
  2.  
  3.     Research, SUNY, Stony Brook.
  4.  
  5.     May, 1996
  6.  
  7. */
  8.  
  9. import java.awt.*;
  10.  
  11.  
  12.  
  13. public class RainbowText extends java.applet.Applet
  14.  
  15.     implements Runnable {
  16.  
  17.   String str = null;
  18.  
  19.   int strlen;
  20.  
  21.   Thread runner = null;
  22.  
  23.   char theChars[];
  24.  
  25.   int charOffsets[];
  26.  
  27.   Color colors[];
  28.  
  29.   int phase = 0;
  30.  
  31.   Image offScreenImage;
  32.  
  33.   Graphics offScreenG;
  34.  
  35.   Font f;
  36.  
  37.   FontMetrics fm;
  38.  
  39.  
  40.  
  41. public void init() {
  42.  
  43.   String paramStr = null;
  44.  
  45.   float h;
  46.  
  47.   int xPos=20;
  48.  
  49.   str = getParameter("text");
  50.  
  51.   if (str == null) {
  52.  
  53.     str = "Museum of Java Applets";
  54.  
  55.   }
  56.  
  57.   f=new Font("TimesRoman",Font.BOLD,36);
  58.  
  59.   fm=getFontMetrics(f);
  60.  
  61.   resize(40+fm.stringWidth(str),40);
  62.  
  63.  
  64.  
  65.   setBackground(Color.black);
  66.  
  67.   strlen = str.length();
  68.  
  69.  
  70.  
  71.   theChars =  new char [strlen];
  72.  
  73.   charOffsets = new int [strlen];
  74.  
  75.   str.getChars(0,strlen,theChars,0);
  76.  
  77.   colors = new Color[strlen];
  78.  
  79.   for (int i = 0; i < strlen; i++) {
  80.  
  81.     h = ((float)i)/((float)strlen);
  82.  
  83.     colors[i] = new Color(Color.HSBtoRGB(h,1.0f,1.0f));
  84.  
  85.     charOffsets[i] = xPos;
  86.  
  87.     xPos+=fm.charWidth(theChars[i]);
  88.  
  89.   }
  90.  
  91.   offScreenImage = createImage(this.size().width,this.size().height);
  92.  
  93.   offScreenG = offScreenImage.getGraphics();
  94.  
  95.   offScreenG.setFont(f);
  96.  
  97.  
  98.  
  99. }
  100.  
  101.  
  102.  
  103. public void start() {
  104.  
  105.   if(runner == null) {
  106.  
  107.     runner = new Thread(this);
  108.  
  109.     runner.start();
  110.  
  111.   }
  112.  
  113. }
  114.  
  115.  
  116.  
  117. public void stop() {
  118.  
  119.   if (runner != null) {
  120.  
  121.     runner.stop();
  122.  
  123.     runner = null;
  124.  
  125.   }
  126.  
  127. }
  128.  
  129.  
  130.  
  131. public void run() {
  132.  
  133.   while (runner != null) {
  134.  
  135.     try {
  136.  
  137.       Thread.sleep(100);
  138.  
  139.     }
  140.  
  141.      catch (InterruptedException e) { }
  142.  
  143.      repaint();
  144.  
  145.     }
  146.  
  147.   }
  148.  
  149.  
  150.  
  151.  
  152.  
  153.   public void update(Graphics g) {
  154.  
  155.     int x, y;
  156.  
  157.     offScreenG.setColor(Color.black);
  158.  
  159.     offScreenG.fillRect(0,0,this.size().width,this.size().height);
  160.  
  161.     phase++;
  162.  
  163.     phase%=str.length();
  164.  
  165.     for(int i=0;i<strlen;i++)
  166.  
  167.      {
  168.  
  169.        x = charOffsets[i];
  170.  
  171.        offScreenG.setColor(colors[(phase+i)%strlen]);
  172.  
  173.        offScreenG.drawChars(theChars,i,1,x,30);
  174.  
  175.      }
  176.  
  177.     paint(g);
  178.  
  179.  
  180.  
  181.   }
  182.  
  183.  
  184.  
  185.   public void paint(Graphics g) {
  186.  
  187.      g.drawImage(offScreenImage,0,0,this);
  188.  
  189.  
  190.  
  191.   }
  192.  
  193.  
  194.  
  195.  
  196.  
  197. }
  198.  
  199.  
  200.  
  201.