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 / WHO / WHO.EXE / WaveText.java next >
Encoding:
Java Source  |  1996-07-16  |  2.6 KB  |  94 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 WaveText extends java.applet.Applet
  14.  
  15.     implements Runnable {
  16.  
  17.   String str = null;
  18.  
  19.   int direction = 1; // 1 is clockwise, -1 is counterclockwise
  20.  
  21.   int horizontalRadius = 10;
  22.  
  23.   int verticalRadius = 10;
  24.  
  25.   Thread runner = null;
  26.  
  27.   char theChars[];
  28.  
  29.   int phase = 0;
  30.  
  31.   Image offScreenImage;
  32.  
  33.   Graphics offScreenG;
  34.  
  35.  
  36.  
  37. public void init() {
  38.  
  39.   String paramStr = null;
  40.  
  41.   str = getParameter("text");
  42.  
  43.   paramStr = getParameter("direction");
  44.  
  45.   setBackground(Color.black);
  46.  
  47.   if (paramStr != null)
  48.  
  49.     direction = Integer.parseInt(paramStr);
  50.  
  51.   paramStr = getParameter("horizontalRadius");
  52.  
  53.   if (paramStr != null)
  54.  
  55.     horizontalRadius = Integer.parseInt(paramStr);
  56.  
  57.   paramStr = getParameter("verticalRadius");
  58.  
  59.   if (paramStr != null)
  60.  
  61.     verticalRadius = Integer.parseInt(paramStr);
  62.  
  63.   setFont(new Font("TimesRoman",Font.BOLD,36));
  64.  
  65.   if (str == null) {
  66.  
  67.     str = "Museum of Java Applets";
  68.  
  69.   }
  70.  
  71.   resize(30+25*str.length()+2*horizontalRadius,80+2*verticalRadius);
  72.  
  73.  
  74.  
  75.   theChars =  new char [str.length()];
  76.  
  77.   str.getChars(0,str.length(),theChars,0);
  78.  
  79.   offScreenImage = createImage(this.size().width,this.size().height);
  80.  
  81.   offScreenG = offScreenImage.getGraphics();
  82.  
  83.   offScreenG.setFont(new Font("TimesRoman",Font.BOLD,36));
  84.  
  85.  }
  86.  
  87.  
  88.  
  89. public void start() {
  90.  
  91.   if(runner == null) {
  92.  
  93.     runner = new Thread(this);
  94.  
  95.     runner.start();
  96.  
  97.   }
  98.  
  99. }
  100.  
  101.  
  102.  
  103. public void stop() {
  104.  
  105.   if (runner != null) {
  106.  
  107.     runner.stop();
  108.  
  109.     runner = null;
  110.  
  111.   }
  112.  
  113. }
  114.  
  115.  
  116.  
  117. public void run() {
  118.  
  119.   while (runner != null) {
  120.  
  121.     try {
  122.  
  123.       Thread.sleep(120);
  124.  
  125.     }
  126.  
  127.     catch (InterruptedException e) { }
  128.  
  129.     repaint();
  130.  
  131.   }
  132.  
  133. }
  134.  
  135.  
  136.  
  137.   public void update(Graphics g) {
  138.  
  139.     int x, y;
  140.  
  141.     double angle;
  142.  
  143.     offScreenG.setColor(Color.black);
  144.  
  145.     offScreenG.fillRect(0,0,this.size().width,this.size().height);
  146.  
  147.     phase+=direction;
  148.  
  149.     phase%=8;
  150.  
  151.     for(int i=0;i<str.length();i++) {
  152.  
  153.         angle = ((phase-i*direction)%8)/4.0*Math.PI;
  154.  
  155.       x = 20+25*i+(int) (Math.cos(angle)*horizontalRadius); // Horizontal motion
  156.  
  157.       y = 60+  (int) (Math.sin(angle)*verticalRadius); // Vertical motion
  158.  
  159.       if (i==0 || theChars[i-1]==' ')  // Each word starts in blue
  160.  
  161.         offScreenG.setColor(Color.blue);
  162.  
  163.       else
  164.  
  165.         offScreenG.setColor(Color.red);  // Each word continues in red
  166.  
  167.       offScreenG.drawChars(theChars,i,1,x,y);
  168.  
  169.     }
  170.  
  171.     paint(g);
  172.  
  173.   }
  174.  
  175.  
  176.  
  177.   public void paint(Graphics g) {
  178.  
  179.     g.drawImage(offScreenImage,0,0,this);
  180.  
  181.   }
  182.  
  183. }
  184.  
  185.  
  186.  
  187.