home *** CD-ROM | disk | FTP | other *** search
/ Java Programmer's Toolkit / Java Programmer's Toolkit.iso / applets / pythag~1 / banner~1.jav < prev    next >
Encoding:
Text File  |  1995-10-31  |  9.1 KB  |  282 lines

  1. /*----------------------------------------------------------------------*/
  2. /*  Banner -- animates text into a box 
  3.            **based on James Gosling's DancingText.java (in this dir)    */ 
  4. /*----------------------------------------------------------------------*/
  5. /* public variables
  6.            fontName="TimesRoman36bi"
  7.            style=..0f-7 means {none, starburst, stage right shuffle, 
  8.                     stage left shuffle , slide in from right, 
  9.                     reverse order, alternate sides, random}  
  10.            time=4000 ..time it takes to finsih animation 
  11.            sound= the URL of a sound (played after animation)
  12.            delay= time between frames (50 is default)  
  13.            pause= pause before the animation starts
  14.            amplitude=36 ..amplitude of the wave in pixels 
  15.            length=1 .. a multiplier for the wave length
  16.            trans=15 ..translates the wave--use to get the right end 
  17.                     position   
  18.            foreground=java.awt.Color.blue  
  19.            background=java.awt.Color.lightGray  
  20.            box = true ..draw a box around the text 
  21.            centre = true ..centre text           
  22.            y_space = 1 ..vertical spacing more thanfont size().height
  23.            x,y = position in window
  24.            xborder, yborder = blank space around the window (text formating)
  25.                                                                         */
  26. /*----------------------------------------------------------------------*/
  27. /* If you want to do some custom animation for the text I have marked 
  28.      easy spots for customization with "**" in the comments (namely 
  29.      at the initial position, timing function (I like sqrt), and 
  30.      the algorithm (this one is a linear combination of the initial and 
  31.      the final positions with a wave in the y direction)                */
  32. /*----------------------------------------------------------------------*/
  33. /*  I left the original audio code in (commented out) but I can't use it 
  34.     since I don't have a speaker                                        */
  35. /*----------------------------------------------------------------------*/
  36. /*               Jim Morey  -  morey@math.ubc.ca  - Aug 5               */
  37. /*----------------------------------------------------------------------*/
  38.   
  39. import java.io.InputStream;
  40. import java.awt.*;
  41. import java.net.*;
  42. import java.applet.*;
  43.  
  44. /*----------------------------------------------------------------------*/
  45.  
  46. class Banner implements Runnable{
  47.   int x,y,time,delay,pause,style,amplitude,trans,xborder,yborder,y_space=1;
  48.   float length;
  49.   Color foreground = null, background = null;
  50.   String audioName;
  51.   String fontName;
  52.   boolean fetchingAudio, playRequested, ready=false, keep_going, box;
  53.   boolean centre;
  54.  
  55.   private long t0;
  56.   private int x0[],xd[],y0[],yd[],W,H,len,xlen,ylen;
  57.   private Graphics offscreen;
  58.   private Image im;
  59.   private Font thefont, font;
  60.   private java.applet.Applet parent;
  61.   private char text[];
  62.   private AudioClip ad;
  63.  
  64.   /* -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  - */
  65.   Banner(java.applet.Applet parent,int wid, int heig) {
  66.     this.parent = parent;
  67.     time = 1000; 
  68.     foreground = java.awt.Color.black;
  69.     background = java.awt.Color.lightGray;
  70.     delay = 50; 
  71.     amplitude = 0; 
  72.     trans = 0; 
  73.     length = 5.0f;
  74.     pause = 0; 
  75.     style = 1; 
  76.     x = 0; y = 0;
  77.     xborder=0;  yborder=0;
  78.     box = false;
  79.     centre = false;
  80.  
  81.     W = wid;
  82.     H = heig;
  83.  
  84.     im = parent.createImage(W,H);
  85.     offscreen = im.getGraphics();
  86.     offscreen.setColor(background);
  87.     offscreen.fillRect(0, 0, W, H);
  88.   }
  89.   
  90.   /* -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  - */
  91.   public void setup_words(Component comp, String s){
  92.     /* .. format the final postions of the Banner .. */
  93.     if (s == null) s = "nothing to say";
  94.     text = new char[s.length() + 2];
  95.     s.getChars(0, s.length(), text, 0);
  96.     if (fontName == null) fontName = "TimesRoman36i";
  97.  
  98.     int fstyle = 0;
  99.     int size = 0;
  100.     int c = 0;
  101.     int pos = fontName.length();
  102.     while (pos > 0) {
  103.       c = fontName.charAt(--pos);
  104.       switch (c) {
  105.         case 'b':
  106.       fstyle |= Font.BOLD;
  107.           continue;
  108.         case 'i':
  109.       fstyle |= Font.ITALIC;
  110.           continue;
  111.       }
  112.       break;
  113.     }
  114.     int fac = 1;
  115.     while ('0' <= c && c <= '9') {
  116.       size += (c - '0') * fac;
  117.       if (--pos <= 0) break;
  118.       c = fontName.charAt(pos);
  119.       fac = fac * 10;
  120.     }
  121.     if (size <= 0) size = 24;
  122.  
  123.     fontName = fontName.substring(0, pos + 1);
  124.     thefont = new Font(fontName, fstyle, size);
  125.     FontMetrics fm = comp.getFontMetrics(thefont);
  126.     if (thefont == null) thefont = font;
  127.     else font = thefont;
  128.  
  129.     len = text.length;
  130.     x0 = new int[len];
  131.     xd = new int[len];
  132.     y0 = new int[len];
  133.     yd = new int[len];
  134.  
  135.     int x_cur = 0;
  136.     int y_cur = fm.getAscent(); 
  137.  
  138.     int word=0;
  139.     boolean startword=true;
  140.  
  141.     if (box) {
  142.       /* .. make room for the box unless other borders were picked .. */
  143.       if (xborder == 0 && yborder ==0){
  144.         xborder=4;  yborder=4;
  145.       }
  146.     }
  147.   
  148.     /* .. figure out where the text should end up .. */
  149.     int xmax = 0;
  150.     xlen = len;
  151.     ylen = 1;
  152.     for (int i = 0; i < len; i++) {
  153.       xd[i] = x_cur;
  154.       if (text[i] == ' ') {
  155.         startword = true;
  156.         if (x_cur>xmax) xmax = x_cur;
  157.       }
  158.       x_cur += fm.getWidths()[text[i]];
  159.       yd[i] = y_cur;
  160.       if (x_cur>W-2*xborder) {
  161.         y_cur += (fm.getAscent() + y_space);
  162.         x_cur = 0;
  163.         xlen = i;
  164.         i = word-1;
  165.         ylen++;
  166.       }
  167.       if (startword && text[i] != ' ') {
  168.         word = i;
  169.         startword = false;
  170.       }
  171.     }
  172.  
  173.     if (x_cur>xmax) xmax = x_cur;
  174.  
  175.     if (centre){
  176.       int dy = (H-y_cur)/2-yborder;
  177.       int dx = (W-xmax)/2-xborder;
  178.       for (int i = 0; i < len; i++) {
  179.         xd[i] += dx;
  180.         yd[i] += dy;
  181.       }
  182.     }
  183.   }
  184.  
  185.   /* -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  - */
  186.   public void run(){
  187.     Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
  188.  
  189. /*
  190.    .. I can't play with this stuff since I don't have a speaker ..
  191.  
  192.     if (audioName != null && !fetchingAudio) {
  193.       fetchingAudio = true;
  194.       kicker = new Thread(this);
  195.       kicker.start();
  196.       Thread.currentThread().setPriority(Thread.MIN_PRIORITY);
  197.       ad = getAudioData(audioName);
  198.       if (playRequested) play(getCodeBase(), ad);
  199.       return;
  200.     }
  201.     if (ad != null)
  202.       play(getCodeBase(), ad);
  203.     else
  204.       playRequested = true;
  205. */
  206.  
  207.     /* .. get a blank window to draw the text .. */
  208.     offscreen.setFont(thefont); 
  209.     offscreen.setColor(background);
  210.     offscreen.fillRect(0, 0, W, H);
  211.     offscreen.setColor(foreground);
  212.  
  213.     if (box) offscreen.drawRect(1, 1, W-3, H-3);
  214.  
  215.     /* .. **there is a lot of room to have new start positions .. */
  216.     switch (style) {
  217.       default:
  218.       case 0: /* .. still .. */
  219.     for (int i = 0; i < len; i++) { x0[i] = xd[i];  y0[i]=yd[i];}
  220.     break;
  221.       case 1: /* .. below and away  .. */
  222.         for (int i = 0; i < len; i++) { x0[i] = W/2;  y0[i]=H*2;}
  223.         break;
  224.       case 2: /* .. stretch from the right .. */
  225.     for (int i = 0; i < len; i++) { x0[i] = W;  y0[i]=yd[i]; }
  226.     break;
  227.       case 3: /* .. stretch form the left .. */
  228.     for (int i = 0; i < len; i++){ x0[i] = 0;  y0[i]=yd[i]; }
  229.         break;
  230.       case 4: /* .. slide in from the right .. */
  231.     for (int i = 0; i < len; i++){ x0[i] = xd[i] + W;  y0[i]=yd[i]; }
  232.     break;
  233.       case 5: /* .. in 2 dimension this one is not too predictable .. */ 
  234.     for (int i = 0; i < len; i++){ x0[i] = xd[len - i - 1];  y0[i]=yd[i]; }
  235.     break;
  236.       case 6: /* .. stretch from both sides .. */
  237.     for (int i = 0; i < len; i++){ x0[i] = (i & 1) == 0 ? W : 0;  y0[i]=yd[i]; }
  238.     break;
  239.       case 7: /* .. random .. */
  240.     for (int i = 0; i < len; i++){ x0[i] = (int) (W * Math.random());  y0[i]=(int) (H*Math.random()); }
  241.     break;
  242.     }
  243.  
  244.     t0 = System.currentTimeMillis();
  245.     float wave;
  246.  
  247.     keep_going=true;
  248.     while (keep_going) {
  249.       ready = false;
  250.       offscreen.setColor(background);
  251.       offscreen.fillRect(0, 0, W, H);
  252.       offscreen.setColor(foreground);
  253.  
  254.       if (box) offscreen.drawRect(1, 1, W-3, H-3);
  255.  
  256.       long t = System.currentTimeMillis() - t0;
  257.       wave = 1.0f;
  258.       if (t > time) keep_going=false;
  259.       /* .. **lots of possibilities for timing functions  .. */
  260.       else wave = (float) Math.sqrt((double)t/(double)time);
  261.  
  262.       int lim = text.length - 2;
  263.       for (int i = 0; i < lim; i++) {
  264.         /* .. **lots of possibilities for algorthims too .. */
  265.         offscreen.drawChars(text, i, 1  
  266.           , (int) (wave*xd[i] + (1-wave)*x0[i] +xborder)  
  267.           , (int) (wave*yd[i] + (1-wave)*y0[i] +yborder) 
  268.          +(int)(amplitude*Math.sin(3.14f* (xd[i]/length+wave*100+trans)/16)) );
  269.       }
  270.       ready = true;
  271.       parent.repaint();
  272. try {Thread.sleep(delay);} catch (InterruptedException e){}
  273.     }
  274.   }
  275.  
  276.   /* -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  -  - */
  277.   public void paint(Graphics g) {
  278.     if (ready) g.drawImage(im, x, y, null);
  279.   }
  280.  
  281. }
  282.