home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgLangD.iso / Java++ / VJ / SAMPLES / MICROSOFT / JAVABEEP / JAVABEEP.JAVA < prev   
Encoding:
Java Source  |  1997-01-21  |  9.9 KB  |  293 lines

  1. //******************************************************************************
  2. // javabeep.java:    Applet
  3. //
  4. //    This applet is a simple demonstration of calling a COM object in a
  5. //    Java program, utilizing the ActiveX runtime for Java. The COM object 
  6. //  is the Beeper sample from the ActiveX Template Library (ATL). ATL is 
  7. //    a Template based Visual C++ 4.x add on that contains a custom AppWizard 
  8. //    and various header files to generate a skeleton ActiveX Control. For 
  9. //    more information on ATL, including the source code to the beeper.dll 
  10. //    COM object, please visit the Microsoft web site at:
  11. //
  12. //                    http://www.microsoft.com/visualc/v42/atl
  13. //
  14. //    Most of the code of interest can be found in the javabeep.mouseDown() 
  15. //    method
  16. //******************************************************************************
  17. import java.applet.*;
  18. import java.awt.*;
  19.  
  20. // Import the class files that JavaTLB generates from beeper.dll
  21. import beeper.*;
  22.  
  23. //==============================================================================
  24. // Main Class for applet javabeep
  25. //
  26. //==============================================================================
  27. public class javabeep extends Applet implements Runnable
  28. {
  29.     // Beeper COM Interface variable
  30.     IBeeper m_Beeper=null;
  31.  
  32.     // THREAD SUPPORT:
  33.     //        m_javabeep    is the Thread object for the applet
  34.     //--------------------------------------------------------------------------
  35.     Thread     m_javabeep = null;
  36.  
  37.     // ANIMATION SUPPORT:
  38.     //        m_Graphics        used for storing the applet's Graphics context
  39.     //        m_Images[]        the array of Image objects for the animation
  40.     //        m_nCurrImage    the index of the next image to be displayed
  41.     //        m_ImgWidth        width of each image
  42.     //        m_ImgHeight        height of each image
  43.     //        m_fAllLoaded    indicates whether all images have been loaded
  44.     //        NUM_IMAGES         number of images used in the animation
  45.     //--------------------------------------------------------------------------
  46.     private Graphics m_Graphics;
  47.     private Image     m_Images[];
  48.     private int      m_nCurrImage;
  49.     private int      m_nImgWidth  = 0;
  50.     private int      m_nImgHeight = 0;
  51.     private boolean  m_fAllLoaded = false;
  52.     private final int NUM_IMAGES = 18;
  53.  
  54.     // javabeep Class Constructor
  55.     //--------------------------------------------------------------------------
  56.     public javabeep()
  57.     {
  58.     }
  59.  
  60.     // APPLET INFO SUPPORT:
  61.     //        The getAppletInfo() method returns a string describing the applet's
  62.     // author, copyright date, or miscellaneous information.
  63.     //--------------------------------------------------------------------------
  64.     public String getAppletInfo()
  65.     {
  66.         return "Name: javabeep\r\n" +
  67.                "Author: Microsoft Developer Support\r\n" +
  68.                "Created with Microsoft Visual J++";
  69.     }
  70.  
  71.  
  72.     // The init() method is called by the AWT when an applet is first loaded or
  73.     // reloaded.  Override this method to perform whatever initialization your
  74.     // applet needs, such as initializing data structures, loading images or
  75.     // fonts, creating frame windows, setting the layout manager, or adding UI
  76.     // components.
  77.     //--------------------------------------------------------------------------
  78.     public void init()
  79.     {
  80.         // If you use a ResourceWizard-generated "control creator" class to
  81.         // arrange controls in your applet, you may want to call its
  82.         // CreateControls() method from within this method. Remove the following
  83.         // call to resize() before adding the call to CreateControls();
  84.         // CreateControls() does its own resizing.
  85.         //----------------------------------------------------------------------
  86.         resize(320, 240);
  87.     }
  88.  
  89.     // Place additional applet clean up code here.  destroy() is called when
  90.     // when you applet is terminating and being unloaded.
  91.     //-------------------------------------------------------------------------
  92.     public void destroy()
  93.     {
  94.     }
  95.  
  96.     // ANIMATION SUPPORT:
  97.     //        Draws the next image, if all images are currently loaded
  98.     //--------------------------------------------------------------------------
  99.     private void displayImage(Graphics g)
  100.     {
  101.         if (!m_fAllLoaded)
  102.             return;
  103.  
  104.         // Draw Image in center of applet
  105.         //----------------------------------------------------------------------
  106.         g.drawImage(m_Images[m_nCurrImage],
  107.                    (size().width - m_nImgWidth)   / 2,
  108.                    (size().height - m_nImgHeight) / 2, null);
  109.  
  110.         // Draw a click me label
  111.         FontMetrics fm = getFontMetrics(getFont());
  112.         String message = new String("Click me with the mouse!");
  113.         g.drawString(message,(size().width-fm.stringWidth(message))/2,                
  114.                      3*(size().height)/4);
  115.     }
  116.  
  117.     // javabeep Paint Handler
  118.     //--------------------------------------------------------------------------
  119.     public void paint(Graphics g)
  120.     {
  121.         // ANIMATION SUPPORT:
  122.         //        The following code displays a status message until all the
  123.         // images are loaded. Then it calls displayImage to display the current
  124.         // image.
  125.         //----------------------------------------------------------------------
  126.         if (m_fAllLoaded)
  127.         {
  128.             Rectangle r = g.getClipRect();
  129.             
  130.             g.clearRect(r.x, r.y, r.width, r.height);
  131.             displayImage(g);
  132.         }
  133.         else
  134.             g.drawString("Loading images...", 10, 20);
  135.     }
  136.  
  137.     //        The start() method is called when the page containing the applet
  138.     // first appears on the screen. The AppletWizard's initial implementation
  139.     // of this method starts execution of the applet's thread.
  140.     //--------------------------------------------------------------------------
  141.     public void start()
  142.     {
  143.         if (m_javabeep == null)
  144.         {
  145.             m_javabeep = new Thread(this);
  146.             m_javabeep.start();
  147.         }
  148.     }
  149.     
  150.     //        The stop() method is called when the page containing the applet is
  151.     // no longer on the screen. The AppletWizard's initial implementation of
  152.     // this method stops execution of the applet's thread.
  153.     //--------------------------------------------------------------------------
  154.     public void stop()
  155.     {
  156.         if (m_javabeep != null)
  157.         {
  158.             m_javabeep.stop();
  159.             m_javabeep = null;
  160.         }
  161.     }
  162.  
  163.     // THREAD SUPPORT
  164.     //        The run() method is called when the applet's thread is started. If
  165.     // your applet performs any ongoing activities without waiting for user
  166.     // input, the code for implementing that behavior typically goes here. For
  167.     // example, for an applet that performs animation, the run() method controls
  168.     // the display of images.
  169.     //--------------------------------------------------------------------------
  170.     public void run()
  171.     {
  172.         m_nCurrImage = 0;
  173.         
  174.         // If re-entering the page, then the images have already been loaded.
  175.         // m_fAllLoaded == TRUE.
  176.         //----------------------------------------------------------------------
  177.         if (!m_fAllLoaded)
  178.         {
  179.             repaint();
  180.             m_Graphics = getGraphics();
  181.             m_Images   = new Image[NUM_IMAGES];
  182.  
  183.             // Load in all the images
  184.             //------------------------------------------------------------------
  185.             MediaTracker tracker = new MediaTracker(this);
  186.             String strImage;
  187.  
  188.             // For each image in the animation, this method first constructs a
  189.             // string containing the path to the image file; then it begins
  190.             // loading the image into the m_Images array.  Note that the call to
  191.             // getImage will return before the image is completely loaded.
  192.             //------------------------------------------------------------------
  193.             for (int i = 1; i <= NUM_IMAGES; i++)
  194.             {
  195.                 // Build path to next image
  196.                 //--------------------------------------------------------------
  197.                 strImage = "images/img00" + ((i < 10) ? "0" : "") + i + ".gif";
  198.                    m_Images[i-1] = getImage(getDocumentBase(), strImage);
  199.  
  200.                 tracker.addImage(m_Images[i-1], 0);
  201.             }
  202.  
  203.             // Wait until all images are fully loaded
  204.             //------------------------------------------------------------------
  205.             try
  206.             {
  207.                 tracker.waitForAll();
  208.                 m_fAllLoaded = !tracker.isErrorAny();
  209.             }
  210.             catch (InterruptedException e)
  211.             {
  212.             }
  213.             
  214.             if (!m_fAllLoaded)
  215.             {
  216.                 stop();
  217.                 m_Graphics.drawString("Error loading images!", 10, 40);
  218.                 return;
  219.             }
  220.             
  221.  
  222.             // Assuming all images are same width and height.
  223.             //--------------------------------------------------------------
  224.             m_nImgWidth  = m_Images[0].getWidth(this);
  225.             m_nImgHeight = m_Images[0].getHeight(this);
  226.         }        
  227.         repaint();
  228.  
  229.         while (true)
  230.         {
  231.             try
  232.             {
  233.                 // Draw next image in animation
  234.                 //--------------------------------------------------------------
  235.                 displayImage(m_Graphics);
  236.                 m_nCurrImage++;
  237.                 if (m_nCurrImage == NUM_IMAGES)
  238.                     m_nCurrImage = 0;
  239.  
  240.                 Thread.sleep(50);
  241.             }
  242.             catch (InterruptedException e)
  243.             {
  244.                 stop();
  245.             }
  246.         }
  247.     }
  248.  
  249.     // MOUSE SUPPORT:
  250.     //        The mouseDown() method is called if the mouse button is pressed
  251.     // while the mouse cursor is over the applet's portion of the screen.
  252.     //--------------------------------------------------------------------------
  253.     public boolean mouseDown(Event evt, int x, int y)
  254.     {
  255.         String BeeperString = new String();
  256.  
  257.         // Check if the Beeper object exists, and create it if necessary
  258.         if(m_Beeper==null)
  259.             m_Beeper = (IBeeper) new Beeper();
  260.         
  261.         // Call Beeper object 'Beep' method. Note that the Beep object has
  262.         // been written such that on the sixth call to Beep(), the Beep object
  263.         // will return an OLE error code which gets thrown as a Java exception.
  264.         // This sample catches the exception, Releases the Beeper object, 
  265.         // creates a new Beeper object, and continues
  266.         try
  267.         {
  268.             m_Beeper.Beep();
  269.         }
  270.         catch(com.ms.com.ComException e)
  271.         {
  272.             // Release the Beeper object by setting m_Beeper=null
  273.             m_Beeper=null;
  274.             // Create a new Beeper object
  275.             m_Beeper = (IBeeper) new Beeper();
  276.             m_Beeper.Beep();
  277.         }
  278.  
  279.         // Call the Beepers getCount and getItem methods
  280.         for(int i=1;i<=m_Beeper.getCount();i++)
  281.         {
  282.             // Build a message string from the strings that getItem returns
  283.             BeeperString+=m_Beeper.getItem(i)+" ";        
  284.         }
  285.  
  286.         // Display the string
  287.         m_Graphics.drawString(BeeperString,x,y);
  288.  
  289.         return true;
  290.     }
  291.  
  292. }
  293.