Source of Ticker.java


  1: 
  2: import java.awt.*;
  3: import java.util.*;
  4: import java.net.*;
  5: import java.io.*;
  6: 
  7: public class Ticker extends DBAnimationApplet {
  8: 
  9:   public void initQuotes() {} 
 10: 
 11:   public void initAnimator() {
 12:     String att = getParameter("delay");
 13:     if (att != null) {
 14:       setDelay(Integer.parseInt(att));
 15:     }
 16:     watch = getParameter("watch");
 17:     if (att != null) {
 18:       StringTokenizer tk = new StringTokenizer(watch); 
 19:       Vector v = new Vector(); 
 20:       while (tk.hasMoreTokens()) {
 21:         v.addElement(tk.nextToken()); 
 22:       }
 23:       n = v.size(); 
 24:       symbol = new String[n]; 
 25:       quote = new String[n]; 
 26:       for (int i = 0; i < n; i++) {
 27:         symbol[i] = (String) v.elementAt(i);  
 28:         quote[i] = "0"; 
 29:       }      
 30:     }
 31:     url = getDocumentBase();
 32:     initQuotes(); 
 33:     updateQuotes(); 
 34:     text1 = text2; 
 35:     x = d.width;
 36:     y = font.getSize();
 37:   }
 38: 
 39:   public void paintFrame(Graphics g) {
 40:     g.setColor(Color.black);             
 41:     g.fillRect(0,0,d.width,d.height);    
 42:   
 43:     // set the font and color, and draw the text
 44:     g.setFont(font);                     
 45:     g.setColor(Color.green);             
 46:     g.drawString(text1 + text2, x, y);            
 47:   
 48:     // get the font metrics to  determine the length of the text
 49:     FontMetrics fm = g.getFontMetrics(); 
 50:     int length = fm.stringWidth(text1);   
 51:   
 52:     // adjust the position of text for the next frame
 53:     x -= offset;                         
 54:   
 55:     // if the text is completely off to the left end
 56:     // move the position back to the right end
 57:     if (x < -length) {                    
 58:       x = 0;   
 59:       text1 = text2; 
 60:       updateQuotes();       
 61:     }                   
 62:   }
 63: 
 64:   protected synchronized void updateQuotes() {
 65:     StringBuffer sb = new StringBuffer(); 
 66:     for (int i = 0; i < n; i++) {
 67:       sb.append(symbol[i] + " " + quote[i] + "    "); 
 68:     }
 69:     text2 = sb.toString();
 70:   }
 71: 
 72:   protected String text1, text2;         
 73:   protected Font font = 
 74:     new java.awt.Font("Helvetica", Font.BOLD, 24);  
 75:   protected int speed = 100;     
 76:   protected int offset = 1;      
 77:   protected int x, y;            
 78:   protected int n; 
 79:   protected String symbol[], quote[]; 
 80:   protected URL url; 
 81:   protected String watch; 
 82: }