1 36 37 40 41 import java.awt.Graphics ; 42 import java.awt.Color ; 43 import java.awt.Font ; 44 import java.awt.FontMetrics ; 45 import java.util.StringTokenizer ; 46 47 54 class TickerArea extends ImageMapArea { 55 56 String tickertext; 57 Color tickercolor; 58 Font tickerfont; 59 int speed; 61 int tickerx; 62 int tickery; 63 int tickerlen; 64 long lasttick; 65 66 public void handleArg(String s) { 67 StringTokenizer st = new StringTokenizer (s, ","); 68 69 tickertext = st.nextToken(); 70 tickercolor = Color.black; 71 speed = 100; 72 String fontname = "Serif"; 73 74 if (st.hasMoreTokens()) { 75 fontname = st.nextToken(); 76 if (st.hasMoreTokens()) { 77 String str = st.nextToken(); 78 if (str.startsWith("#")) { 79 str = str.substring(1); 80 } 81 try { 82 int colorval = Integer.parseInt(str, 16); 83 tickercolor = new Color ((colorval >> 16) & 0xff, 84 (colorval >> 8) & 0xff, 85 (colorval >> 0) & 0xff); 86 } catch (Exception e) { 87 tickercolor = Color.black; 88 } 89 if (st.hasMoreTokens()) { 90 str = st.nextToken(); 91 try { 92 speed = Integer.parseInt(str); 93 } catch (Exception e) { 94 speed = 100; 95 } 96 } 97 } 98 } 99 100 FontMetrics fm; 101 int size; 102 int nextsize = H; 103 do { 104 size = nextsize; 105 tickerfont = new Font (fontname, Font.PLAIN, size); 106 fm = parent.getFontMetrics(tickerfont); 107 nextsize = (size * 9) / 10; 108 } while (fm.getHeight() > H && size > 0); 109 tickerlen = fm.stringWidth(tickertext); 110 tickery = fm.getAscent(); 111 } 112 113 public void getMedia() { 114 tickerx = 0; 115 repaint(); 116 lasttick = System.currentTimeMillis(); 117 } 118 119 public boolean animate() { 120 long curtick = System.currentTimeMillis(); 121 tickerx -= ((speed * (curtick - lasttick)) / 1000); 122 if (tickerx > W || tickerx + tickerlen < 0) { 123 tickerx = W; 124 } 125 repaint(); 126 lasttick = curtick; 127 return true; 128 } 129 130 public void highlight(Graphics g) { 131 g.setColor(tickercolor); 132 g.setFont(tickerfont); 133 g.drawString(tickertext, X+tickerx, Y+tickery); 134 } 135 } 136 137 | Popular Tags |