home *** CD-ROM | disk | FTP | other *** search
/ Learn Java Now / Learn_Java_Now_Microsoft_1996.iso / JavaNow / Code / Chap14 / Marquee / Marquee.class (.txt) next >
Encoding:
Java Class File  |  1996-07-29  |  3.6 KB  |  121 lines

  1. import java.applet.Applet;
  2. import java.awt.Component;
  3. import java.awt.Font;
  4. import java.awt.FontMetrics;
  5. import java.awt.Graphics;
  6.  
  7. public class Marquee extends Applet implements Runnable {
  8.    Thread m_Marquee;
  9.    String m_string = "";
  10.    String m_font = "Courier";
  11.    String m_style = "PLAIN";
  12.    int m_size = 12;
  13.    int m_fps = 10;
  14.    final String PARAM_string = "string";
  15.    final String PARAM_font = "font";
  16.    final String PARAM_style = "style";
  17.    final String PARAM_size = "size";
  18.    final String PARAM_fps = "fps";
  19.    private int m_nOffset;
  20.    private int m_nMax;
  21.    private int m_nMin;
  22.  
  23.    public void start() {
  24.       if (this.m_Marquee == null) {
  25.          this.m_Marquee = new Thread(this);
  26.          this.m_Marquee.start();
  27.       }
  28.  
  29.    }
  30.  
  31.    public String[][] getParameterInfo() {
  32.       String[][] info = new String[][]{{"string", "String", "String to output in marquee"}, {"font", "String", "Font to use"}, {"style", "String", "PLAIN, BOLD, or ITALIC"}, {"size", "int", "Font size [pts]"}, {"fps", "int", "Frame rate"}};
  33.       return info;
  34.    }
  35.  
  36.    public void stop() {
  37.       if (this.m_Marquee != null) {
  38.          this.m_Marquee.stop();
  39.          this.m_Marquee = null;
  40.       }
  41.  
  42.    }
  43.  
  44.    public void finalize() {
  45.    }
  46.  
  47.    public String getAppletInfo() {
  48.       return "Name: Marquee\r\n" + "Author: Stephen R. Davis\r\n" + "Created for Learn Java Now (c)";
  49.    }
  50.  
  51.    public void run() {
  52.       while(true) {
  53.          int nSleepValue = 1000 / this.m_fps;
  54.  
  55.          try {
  56.             ((Component)this).repaint();
  57.             Thread.sleep((long)nSleepValue);
  58.          } catch (InterruptedException var4) {
  59.             this.stop();
  60.          }
  61.       }
  62.    }
  63.  
  64.    public void destroy() {
  65.    }
  66.  
  67.    public void init() {
  68.       String param = ((Applet)this).getParameter("string");
  69.       if (param != null) {
  70.          this.m_string = param;
  71.       }
  72.  
  73.       param = ((Applet)this).getParameter("font");
  74.       if (param != null) {
  75.          this.m_font = param;
  76.       }
  77.  
  78.       param = ((Applet)this).getParameter("style");
  79.       if (param != null) {
  80.          this.m_style = param;
  81.       }
  82.  
  83.       param = ((Applet)this).getParameter("size");
  84.       if (param != null) {
  85.          this.m_size = Integer.parseInt(param);
  86.       }
  87.  
  88.       param = ((Applet)this).getParameter("fps");
  89.       if (param != null) {
  90.          this.m_fps = Integer.parseInt(param);
  91.       }
  92.  
  93.       int nStyle = 0;
  94.       if (this.m_style.equalsIgnoreCase("BOLD")) {
  95.          nStyle = 1;
  96.       }
  97.  
  98.       if (this.m_style.equalsIgnoreCase("ITALIC")) {
  99.          nStyle = 2;
  100.       }
  101.  
  102.       Font font = new Font(this.m_font, nStyle, this.m_size);
  103.       ((Component)this).setFont(font);
  104.       FontMetrics fm = ((Component)this).getFontMetrics(font);
  105.       this.m_nMin = -fm.stringWidth(this.m_string);
  106.       this.m_nMax = ((Component)this).size().width;
  107.       this.m_nOffset = this.m_nMax;
  108.    }
  109.  
  110.    public void paint(Graphics g) {
  111.       int nVertOffset = ((Component)this).size().height;
  112.       nVertOffset = (nVertOffset - this.m_size) / 2;
  113.       g.drawString(this.m_string, this.m_nOffset, nVertOffset + this.m_size);
  114.       this.m_nOffset += -1;
  115.       if (this.m_nOffset < this.m_nMin) {
  116.          this.m_nOffset = this.m_nMax;
  117.       }
  118.  
  119.    }
  120. }
  121.