home *** CD-ROM | disk | FTP | other *** search
/ Learn Java Now / Learn_Java_Now_Microsoft_1996.iso / MsDev / Samples / Microsoft / CabAndSign / firstcab.cab / javabeep.class (.txt)
Encoding:
Java Class File  |  1996-08-26  |  5.3 KB  |  161 lines

  1. import beeper.Beeper;
  2. import beeper.IBeeper;
  3. import com.ms.com.ComException;
  4. import java.applet.Applet;
  5. import java.awt.Component;
  6. import java.awt.Container;
  7. import java.awt.Event;
  8. import java.awt.FontMetrics;
  9. import java.awt.Graphics;
  10. import java.awt.Image;
  11. import java.awt.MediaTracker;
  12. import java.awt.Rectangle;
  13. import java.awt.Toolkit;
  14. import java.awt.Window;
  15. import java.awt.image.ImageObserver;
  16.  
  17. public class javabeep extends Applet implements Runnable {
  18.    IBeeper m_Beeper;
  19.    Thread m_javabeep;
  20.    private Graphics m_Graphics;
  21.    private Image[] m_Images;
  22.    private int m_nCurrImage;
  23.    private int m_nImgWidth;
  24.    private int m_nImgHeight;
  25.    private boolean m_fAllLoaded;
  26.    private final int NUM_IMAGES = 18;
  27.    boolean m_fStandAlone;
  28.  
  29.    public void start() {
  30.       if (this.m_javabeep == null) {
  31.          this.m_javabeep = new Thread(this);
  32.          this.m_javabeep.start();
  33.       }
  34.  
  35.       this.m_Beeper = (IBeeper)(new Beeper());
  36.    }
  37.  
  38.    public void stop() {
  39.       if (this.m_javabeep != null) {
  40.          this.m_javabeep.stop();
  41.          this.m_javabeep = null;
  42.       }
  43.  
  44.    }
  45.  
  46.    public boolean mouseDown(Event evt, int x, int y) {
  47.       String BeeperString = new String();
  48.  
  49.       try {
  50.          this.m_Beeper.Beep();
  51.  
  52.          for(int i = 1; i <= this.m_Beeper.getCount(); ++i) {
  53.             BeeperString = BeeperString + this.m_Beeper.getItem(i) + " ";
  54.          }
  55.       } catch (ComException var7) {
  56.          BeeperString = "Error from Beeper.Beep: handled in mouseDown method";
  57.       }
  58.  
  59.       this.m_Graphics.drawString(BeeperString, x, y);
  60.       return true;
  61.    }
  62.  
  63.    private void displayImage(Graphics g) {
  64.       if (this.m_fAllLoaded) {
  65.          g.drawImage(this.m_Images[this.m_nCurrImage], (((Component)this).size().width - this.m_nImgWidth) / 2, (((Component)this).size().height - this.m_nImgHeight) / 2, (ImageObserver)null);
  66.          FontMetrics fm = ((Component)this).getFontMetrics(((Component)this).getFont());
  67.          String message = new String("Click me with the mouse!");
  68.          g.drawString(message, (((Component)this).size().width - fm.stringWidth(message)) / 2, 3 * ((Component)this).size().height / 4);
  69.       }
  70.    }
  71.  
  72.    public String getAppletInfo() {
  73.       return "Name: javabeep\r\n" + "Author: Steve Horn\r\n" + "Created with Microsoft Visual J++ Version 1.0";
  74.    }
  75.  
  76.    public void run() {
  77.       this.m_nCurrImage = 0;
  78.       if (!this.m_fAllLoaded) {
  79.          ((Component)this).repaint();
  80.          this.m_Graphics = ((Component)this).getGraphics();
  81.          this.m_Images = new Image[18];
  82.          MediaTracker tracker = new MediaTracker(this);
  83.          int i = 1;
  84.  
  85.          do {
  86.             String strImage = "images/img00" + (i < 10 ? "0" : "") + i + ".gif";
  87.             if (this.m_fStandAlone) {
  88.                this.m_Images[i - 1] = Toolkit.getDefaultToolkit().getImage(strImage);
  89.             } else {
  90.                this.m_Images[i - 1] = ((Applet)this).getImage(((Applet)this).getDocumentBase(), strImage);
  91.             }
  92.  
  93.             tracker.addImage(this.m_Images[i - 1], 0);
  94.             ++i;
  95.          } while(i <= 18);
  96.  
  97.          try {
  98.             tracker.waitForAll();
  99.             this.m_fAllLoaded = !tracker.isErrorAny();
  100.          } catch (InterruptedException var5) {
  101.          }
  102.  
  103.          if (!this.m_fAllLoaded) {
  104.             this.stop();
  105.             this.m_Graphics.drawString("Error loading images!", 10, 40);
  106.             return;
  107.          }
  108.  
  109.          this.m_nImgWidth = this.m_Images[0].getWidth(this);
  110.          this.m_nImgHeight = this.m_Images[0].getHeight(this);
  111.       }
  112.  
  113.       ((Component)this).repaint();
  114.  
  115.       while(true) {
  116.          try {
  117.             this.displayImage(this.m_Graphics);
  118.             ++this.m_nCurrImage;
  119.             if (this.m_nCurrImage == 18) {
  120.                this.m_nCurrImage = 0;
  121.             }
  122.  
  123.             Thread.sleep(50L);
  124.          } catch (InterruptedException var6) {
  125.             this.stop();
  126.          }
  127.       }
  128.    }
  129.  
  130.    public void destroy() {
  131.    }
  132.  
  133.    public static void main(String[] args) {
  134.       javabeepFrame frame = new javabeepFrame("javabeep");
  135.       ((Window)frame).show();
  136.       ((Component)frame).hide();
  137.       ((Component)frame).resize(((Container)frame).insets().left + ((Container)frame).insets().right + 320, ((Container)frame).insets().top + ((Container)frame).insets().bottom + 240);
  138.       javabeep applet_javabeep = new javabeep();
  139.       ((Container)frame).add("Center", applet_javabeep);
  140.       applet_javabeep.m_fStandAlone = true;
  141.       applet_javabeep.init();
  142.       applet_javabeep.start();
  143.       ((Window)frame).show();
  144.    }
  145.  
  146.    public void init() {
  147.       ((Applet)this).resize(320, 240);
  148.    }
  149.  
  150.    public void paint(Graphics g) {
  151.       if (this.m_fAllLoaded) {
  152.          Rectangle r = g.getClipRect();
  153.          g.clearRect(r.x, r.y, r.width, r.height);
  154.          this.displayImage(g);
  155.       } else {
  156.          g.drawString("Loading images...", 10, 20);
  157.       }
  158.  
  159.    }
  160. }
  161.