home *** CD-ROM | disk | FTP | other *** search
/ Computer Shopper 201 / DPCS1104.ISO / data1.cab / Internet_Extensions / Java / SerifMarquee / TextEvent.class (.txt) < prev   
Encoding:
Java Class File  |  2004-08-11  |  4.2 KB  |  148 lines

  1. import java.awt.Component;
  2. import java.awt.Font;
  3. import java.awt.FontMetrics;
  4. import java.awt.Graphics;
  5. import java.awt.Image;
  6. import java.awt.Rectangle;
  7. import java.util.StringTokenizer;
  8.  
  9. public final class TextEvent extends AnimationEvent {
  10.    private int m_textWidth;
  11.    private int m_textHeight;
  12.    private int m_textDescent;
  13.    private int m_textAscent;
  14.    private Font m_font;
  15.    private String m_text;
  16.    private int m_position = 0;
  17.    private int m_direction = 0;
  18.    private int m_yPos;
  19.  
  20.    public void drawFrame(Graphics g) {
  21.       int x = 0;
  22.       if (this.m_direction == 0) {
  23.          x = (int)((long)(-this.m_textWidth) + super.m_xOffset / 16L);
  24.       } else {
  25.          x = (int)((long)super.m_animation.m_size.width - super.m_xOffset / 16L);
  26.       }
  27.  
  28.       g.setColor(super.m_colour);
  29.       g.setFont(this.m_font);
  30.       g.drawString(this.m_text, x, this.m_yPos);
  31.    }
  32.  
  33.    public void initFromParams(String params) {
  34.       super.initFromParams(params);
  35.       StringTokenizer st = new StringTokenizer(params, ";");
  36.       String szToken = st.nextToken();
  37.       int nColon = szToken.indexOf(58);
  38.       String fontName = "ARIAL";
  39.       int fontSize = 72;
  40.       int fontStyle = 0;
  41.       if (nColon != -1) {
  42.          String szName = szToken.substring(0, nColon).toUpperCase();
  43.          String szValue = szToken.substring(nColon + 1);
  44.          if (!szName.equals("TYPE") || !szValue.equals("TEXT")) {
  45.             return;
  46.          }
  47.       }
  48.  
  49.       while(st.hasMoreTokens()) {
  50.          szToken = st.nextToken();
  51.          nColon = szToken.indexOf(58);
  52.          if (nColon != -1) {
  53.             String var17 = szToken.substring(0, nColon).toUpperCase();
  54.             String var18 = szToken.substring(nColon + 1);
  55.             if (var17.equals("TEXT")) {
  56.                this.m_text = var18;
  57.             } else if (var17.equals("FONT")) {
  58.                fontName = var18;
  59.             } else if (var17.equals("STYLE")) {
  60.                if (var18.equals("BOLD")) {
  61.                   ++fontStyle;
  62.                } else if (var18.equals("ITALIC")) {
  63.                   fontStyle += 2;
  64.                } else if (var18.equals("BOLDITALIC")) {
  65.                   fontStyle += 3;
  66.                }
  67.             } else if (var17.equals("SIZE")) {
  68.                fontSize = Integer.valueOf(var18);
  69.             } else if (var17.equals("POSITION")) {
  70.                if (var18.equals("TOP")) {
  71.                   this.m_position = 0;
  72.                } else if (var18.equals("MIDDLE")) {
  73.                   this.m_position = 1;
  74.                } else if (var18.equals("BOTTOM")) {
  75.                   this.m_position = 2;
  76.                }
  77.             } else if (var17.equals("DIRECTION")) {
  78.                if (var18.equals("RIGHTWARDS")) {
  79.                   this.m_direction = 0;
  80.                } else if (var18.equals("LEFTWARDS")) {
  81.                   this.m_direction = 1;
  82.                }
  83.             }
  84.          }
  85.       }
  86.  
  87.       this.m_font = new Font(fontName, fontStyle, fontSize);
  88.       Image imTemp = super.m_component.createImage(1, 1);
  89.       Graphics grTemp = imTemp.getGraphics();
  90.       grTemp.setFont(this.m_font);
  91.       FontMetrics fm = grTemp.getFontMetrics();
  92.       this.m_textWidth = fm.stringWidth(this.m_text);
  93.       this.m_textAscent = fm.getMaxAscent();
  94.       this.m_textDescent = fm.getMaxDescent();
  95.       if (this.m_position == 0) {
  96.          this.m_yPos = this.m_textAscent;
  97.       } else if (this.m_position == 1) {
  98.          this.m_yPos = this.m_textAscent + (super.m_animation.m_size.height - this.m_textAscent - this.m_textDescent) / 2;
  99.       } else if (this.m_position == 2) {
  100.          this.m_yPos = super.m_animation.m_size.height - this.m_textDescent;
  101.       }
  102.  
  103.       grTemp.dispose();
  104.    }
  105.  
  106.    public TextEvent(Component component, Animation anim) {
  107.       super(component, anim);
  108.    }
  109.  
  110.    public String getUrl(int mx, int my) {
  111.       int x = 0;
  112.       int y = 0;
  113.       if (this.m_direction == 0) {
  114.          x = (int)((long)(-this.m_textWidth) + super.m_xOffset / 16L);
  115.       } else {
  116.          x = (int)((long)super.m_animation.m_size.width - super.m_xOffset / 16L);
  117.       }
  118.  
  119.       if (this.m_position == 0) {
  120.          y = 0;
  121.       } else if (this.m_position == 1) {
  122.          y = (super.m_animation.m_size.height - this.m_textAscent - this.m_textDescent) / 2;
  123.       } else if (this.m_position == 2) {
  124.          y = super.m_animation.m_size.height - this.m_textAscent - this.m_textDescent;
  125.       }
  126.  
  127.       Rectangle rect = new Rectangle(x, y, this.m_textWidth, this.m_textAscent + this.m_textDescent);
  128.       return rect.inside(mx, my) ? super.m_url : null;
  129.    }
  130.  
  131.    public void incFrame() {
  132.       super.incFrame();
  133.       int x = 0;
  134.       if (this.m_direction == 0) {
  135.          x = (int)((long)(-this.m_textWidth) + super.m_xOffset / 16L);
  136.          if (x > super.m_animation.m_size.width) {
  137.             super.m_xOffset = 0L;
  138.          }
  139.       } else {
  140.          x = (int)((long)super.m_animation.m_size.width - super.m_xOffset / 16L);
  141.          if (x < -this.m_textWidth) {
  142.             super.m_xOffset = 0L;
  143.          }
  144.       }
  145.  
  146.    }
  147. }
  148.