home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 1996 December / PCWKCD1296.iso / vjplusb / msdev / samples / microsoft / javabeep / javabeep.java < prev    next >
Text File  |  1996-07-12  |  15KB  |  413 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. import javabeepframe;
  20.  
  21. // Import the class files that JavaTLB generates from beeper.dll
  22. import beeper.*;
  23.  
  24. //==============================================================================
  25. // Main Class for applet javabeep
  26. //
  27. //==============================================================================
  28. public class javabeep extends Applet implements Runnable
  29. {
  30.     // Beeper COM Interface variable
  31.     IBeeper m_Beeper=null;
  32.  
  33.     // THREAD SUPPORT:
  34.     //        m_javabeep    is the Thread object for the applet
  35.     //--------------------------------------------------------------------------
  36.     Thread     m_javabeep = null;
  37.  
  38.     // ANIMATION SUPPORT:
  39.     //        m_Graphics        used for storing the applet's Graphics context
  40.     //        m_Images[]        the array of Image objects for the animation
  41.     //        m_nCurrImage    the index of the next image to be displayed
  42.     //        m_ImgWidth        width of each image
  43.     //        m_ImgHeight        height of each image
  44.     //        m_fAllLoaded    indicates whether all images have been loaded
  45.     //        NUM_IMAGES         number of images used in the animation
  46.     //--------------------------------------------------------------------------
  47.     private Graphics m_Graphics;
  48.     private Image     m_Images[];
  49.     private int      m_nCurrImage;
  50.     private int      m_nImgWidth  = 0;
  51.     private int      m_nImgHeight = 0;
  52.     private boolean  m_fAllLoaded = false;
  53.     private final int NUM_IMAGES = 18;
  54.  
  55.     // STANDALONE APPLICATION SUPPORT:
  56.     //        m_fStandAlone will be set to true if applet is run standalone
  57.     //--------------------------------------------------------------------------
  58.     boolean m_fStandAlone = false;
  59.  
  60.     // STANDALONE APPLICATION SUPPORT
  61.     //     The main() method acts as the applet's entry point when it is run
  62.     // as a standalone application. It is ignored if the applet is run from
  63.     // within an HTML page.
  64.     //--------------------------------------------------------------------------
  65.     public static void main(String args[])
  66.     {
  67.         // Create Toplevel Window to contain applet javabeep
  68.         //----------------------------------------------------------------------
  69.         javabeepframe frame = new javabeepframe("javabeep");
  70.  
  71.         // Must show Frame before we size it so insets() will return valid values
  72.         //----------------------------------------------------------------------
  73.         frame.show();
  74.         frame.hide();
  75.         frame.resize(frame.insets().left + frame.insets().right  + 320,
  76.                      frame.insets().top  + frame.insets().bottom + 240);
  77.  
  78.         // The following code starts the applet running within the frame window.
  79.         // It also calls GetParameters() to retrieve parameter values from the
  80.         // command line, and sets m_fStandAlone to true to prevent init() from
  81.         // trying to get them from the HTML page.
  82.         //----------------------------------------------------------------------
  83.         javabeep applet_javabeep = new javabeep();
  84.  
  85.         frame.add("Center", applet_javabeep);
  86.         applet_javabeep.m_fStandAlone = true;
  87.         applet_javabeep.init();
  88.         applet_javabeep.start();
  89.         frame.show();
  90.     }
  91.  
  92.     // javabeep Class Constructor
  93.     //--------------------------------------------------------------------------
  94.     public javabeep()
  95.     {
  96.         // TODO: Add constructor code here
  97.     }
  98.  
  99.     // APPLET INFO SUPPORT:
  100.     //        The getAppletInfo() method returns a string describing the applet's
  101.     // author, copyright date, or miscellaneous information.
  102.     //--------------------------------------------------------------------------
  103.     public String getAppletInfo()
  104.     {
  105.         return "Name: javabeep\r\n" +
  106.                "Author: Microsoft Developer Support\r\n" +
  107.                "Created with Microsoft Visual J++ Version 1.0";
  108.     }
  109.  
  110.  
  111.     // The init() method is called by the AWT when an applet is first loaded or
  112.     // reloaded.  Override this method to perform whatever initialization your
  113.     // applet needs, such as initializing data structures, loading images or
  114.     // fonts, creating frame windows, setting the layout manager, or adding UI
  115.     // components.
  116.     //--------------------------------------------------------------------------
  117.     public void init()
  118.     {
  119.         // If you use a ResourceWizard-generated "control creator" class to
  120.         // arrange controls in your applet, you may want to call its
  121.         // CreateControls() method from within this method. Remove the following
  122.         // call to resize() before adding the call to CreateControls();
  123.         // CreateControls() does its own resizing.
  124.         //----------------------------------------------------------------------
  125.         resize(320, 240);
  126.  
  127.         // TODO: Place additional initialization code here
  128.     }
  129.  
  130.     // Place additional applet clean up code here.  destroy() is called when
  131.     // when you applet is terminating and being unloaded.
  132.     //-------------------------------------------------------------------------
  133.     public void destroy()
  134.     {
  135.         // TODO: Place applet cleanup code here
  136.     }
  137.  
  138.     // ANIMATION SUPPORT
  139.     //        The imageUpdate() method is called repeatedly by the AWT while
  140.     // images are being constructed.  The flags parameter provides information
  141.     // about the status of images under construction.  The AppletWizard's
  142.     // initial implementation of this method checks whether the ALLBITS flag is
  143.     // set.  When the ALLBITS is set, this method knows that an image is
  144.     // completely loaded. When all the images are completely loaded, this
  145.     // method sets the m_fAllLoaded variable to true so that animation can begin.
  146.     //--------------------------------------------------------------------------
  147.     public boolean imageUpdate(Image img, int flags, int x, int y, int w, int h)
  148.     {
  149.         // Nothing to do if images are all loaded
  150.         //----------------------------------------------------------------------
  151.         if (m_fAllLoaded)
  152.             return false;
  153.  
  154.         // Want all bits to be available before painting
  155.         //----------------------------------------------------------------------
  156.         if ((flags & ALLBITS) == 0)
  157.             return true;
  158.  
  159.         // All bits are available, so increment loaded count of fully
  160.         // loaded images, starting animation if all images are loaded
  161.         //----------------------------------------------------------------------
  162.         if (++m_nCurrImage == NUM_IMAGES)
  163.         {
  164.             m_nCurrImage = 0;
  165.             m_fAllLoaded = true;
  166.         }                
  167.  
  168.         return false;
  169.     }
  170.  
  171.     // ANIMATION SUPPORT:
  172.     //        Draws the next image, if all images are currently loaded
  173.     //--------------------------------------------------------------------------
  174.     private void displayImage(Graphics g)
  175.     {
  176.         if (!m_fAllLoaded)
  177.             return;
  178.  
  179.         // Draw Image in center of applet
  180.         //----------------------------------------------------------------------
  181.         g.drawImage(m_Images[m_nCurrImage], 
  182.                    (size().width - m_nImgWidth)   / 2,
  183.                    (size().height - m_nImgHeight) / 2, null);
  184.         
  185.         // Draw a click me label
  186.         FontMetrics fm = getFontMetrics(getFont());
  187.         String message = new String("Click me with the mouse!");
  188.         g.drawString(message,
  189.                    (size().width-fm.stringWidth(message))/2,                
  190.                    3*(size().height)/4);
  191.     }
  192.  
  193.     // javabeep Paint Handler
  194.     //--------------------------------------------------------------------------
  195.     public void paint(Graphics g)
  196.     {
  197.         // ANIMATION SUPPORT:
  198.         //        The following code displays a status message until all the
  199.         // images are loaded. Then it calls displayImage to display the current
  200.         // image.
  201.         //----------------------------------------------------------------------
  202.         if (m_fAllLoaded)
  203.         {
  204.             Rectangle r = g.getClipRect();
  205.             
  206.             g.clearRect(r.x, r.y, r.width, r.height);
  207.             displayImage(g);
  208.         }
  209.         else
  210.             g.drawString("Loading images...", 10, 20);
  211.  
  212.         // TODO: Place additional applet Paint code here
  213.     }
  214.  
  215.     //        The start() method is called when the page containing the applet
  216.     // first appears on the screen. The AppletWizard's initial implementation
  217.     // of this method starts execution of the applet's thread.
  218.     //--------------------------------------------------------------------------
  219.     public void start()
  220.     {
  221.         if (m_javabeep == null)
  222.         {
  223.             m_javabeep = new Thread(this);
  224.             m_javabeep.start();
  225.         }
  226.         // TODO: Place additional applet start code here
  227.     }
  228.     
  229.     //        The stop() method is called when the page containing the applet is
  230.     // no longer on the screen. The AppletWizard's initial implementation of
  231.     // this method stops execution of the applet's thread.
  232.     //--------------------------------------------------------------------------
  233.     public void stop()
  234.     {
  235.         if (m_javabeep != null)
  236.         {
  237.             m_javabeep.stop();
  238.             m_javabeep = null;
  239.         }
  240.  
  241.         // TODO: Place additional applet stop code here
  242.     }
  243.  
  244.     // THREAD SUPPORT
  245.     //        The run() method is called when the applet's thread is started. If
  246.     // your applet performs any ongoing activities without waiting for user
  247.     // input, the code for implementing that behavior typically goes here. For
  248.     // example, for an applet that performs animation, the run() method controls
  249.     // the display of images.
  250.     //--------------------------------------------------------------------------
  251.     public void run()
  252.     {
  253.         repaint();
  254.  
  255.         m_Graphics = getGraphics();
  256.         m_nCurrImage   = 0;
  257.         m_Images   = new Image[NUM_IMAGES];
  258.  
  259.         // Load in all the images
  260.         //----------------------------------------------------------------------
  261.         String strImage;
  262.  
  263.         // For each image in the animation, this method first constructs a
  264.         // string containing the path to the image file; then it begins loading
  265.         // the image into the m_Images array.  Note that the call to getImage
  266.         // will return before the image is completely loaded.
  267.         //----------------------------------------------------------------------
  268.         for (int i = 1; i <= NUM_IMAGES; i++)
  269.         {
  270.             // Build path to next image
  271.             //------------------------------------------------------------------
  272.             strImage = "images/img00" + ((i < 10) ? "0" : "") + i + ".gif";
  273.             if (m_fStandAlone)
  274.                 m_Images[i-1] = Toolkit.getDefaultToolkit().getImage(strImage);
  275.             else
  276.                 m_Images[i-1] = getImage(getDocumentBase(), strImage);
  277.  
  278.             // Get width and height of one image.
  279.             // Assuming all images are same width and height
  280.             //------------------------------------------------------------------
  281.             if (m_nImgWidth == 0)
  282.             {
  283.                 try
  284.                 {
  285.                     // The getWidth() and getHeight() methods of the Image class
  286.                     // return -1 if the dimensions are not yet known. The
  287.                     // following code keeps calling getWidth() and getHeight()
  288.                     // until they return actual values.
  289.                     // NOTE: This is only executed once in this loop, since we
  290.                     //       are assuming all images are the same width and
  291.                     //       height.  However, since we do not want to duplicate
  292.                     //       the above image load code, the code resides in the
  293.                     //       loop.
  294.                     //----------------------------------------------------------
  295.                     while ((m_nImgWidth = m_Images[i-1].getWidth(null)) < 0)
  296.                         Thread.sleep(1);
  297.  
  298.                     while ((m_nImgHeight = m_Images[i-1].getHeight(null)) < 0)
  299.                         Thread.sleep(1);                        
  300.                 }
  301.                 catch (InterruptedException e)
  302.                 {
  303.                     // TODO: Place exception-handling code here in case an
  304.                     //       InterruptedException is thrown by Thread.sleep(),
  305.                     //         meaning that another thread has interrupted this one
  306.                 }
  307.             }
  308.  
  309.             // Force image to fully load
  310.             //------------------------------------------------------------------
  311.             m_Graphics.drawImage(m_Images[i-1], -1000, -1000, this);
  312.         }
  313.  
  314.         // Wait until all images are fully loaded
  315.         //----------------------------------------------------------------------
  316.         while (!m_fAllLoaded)
  317.         {
  318.             try
  319.             {
  320.                 Thread.sleep(10);
  321.             }
  322.             catch (InterruptedException e)
  323.             {
  324.                 // TODO: Place exception-handling code here in case an
  325.                 //       InterruptedException is thrown by Thread.sleep(),
  326.                 //         meaning that another thread has interrupted this one
  327.             }
  328.         }
  329.         
  330.         repaint();
  331.  
  332.         while (true)
  333.         {
  334.             try
  335.             {
  336.                 // Draw next image in animation
  337.                 //--------------------------------------------------------------
  338.                 displayImage(m_Graphics);
  339.                 m_nCurrImage++;
  340.                 if (m_nCurrImage == NUM_IMAGES)
  341.                     m_nCurrImage = 0;
  342.  
  343.                 // TODO:  Add additional thread-specific code here
  344.                 Thread.sleep(50);
  345.             }
  346.             catch (InterruptedException e)
  347.             {
  348.                 // TODO: Place exception-handling code here in case an
  349.                 //       InterruptedException is thrown by Thread.sleep(),
  350.                 //         meaning that another thread has interrupted this one
  351.                 stop();
  352.             }
  353.         }
  354.     }
  355.  
  356.     // MOUSE SUPPORT:
  357.     //        The mouseDown() method is called if the mouse button is pressed
  358.     // while the mouse cursor is over the applet's portion of the screen.
  359.     //--------------------------------------------------------------------------
  360.     public boolean mouseDown(Event evt, int x, int y)
  361.     {
  362.         String BeeperString = new String();
  363.  
  364.         // Check if the Beeper object exists, and create it if necessary
  365.         if(m_Beeper==null)
  366.             m_Beeper = (IBeeper) new Beeper();
  367.         
  368.         // Call Beeper object 'Beep' method. Note that the Beep object has
  369.         // been written such that on the sixth call to Beep(), the Beep object
  370.         // will return an OLE error code which gets thrown as a Java exception.
  371.         // This sample catches the exception, Releases the Beeper object, 
  372.         // creates a new Beeper object, and continues
  373.         try
  374.         {
  375.             m_Beeper.Beep();
  376.         }
  377.         catch(com.ms.com.ComException e)
  378.         {
  379.             // Release the Beeper object by setting m_Beeper=null
  380.             m_Beeper=null;
  381.             // Create a new Beeper object
  382.             m_Beeper = (IBeeper) new Beeper();
  383.             m_Beeper.Beep();
  384.         }
  385.  
  386.         // Call the Beepers getCount and getItem methods
  387.         for(int i=1;i<=m_Beeper.getCount();i++)
  388.         {
  389.             // Build a message string from the strings that getItem returns
  390.             BeeperString+=m_Beeper.getItem(i)+" ";        
  391.         }
  392.  
  393.         // Display the string
  394.         m_Graphics.drawString(BeeperString,x,y);
  395.  
  396.         return true;
  397.     }
  398.  
  399.     // MOUSE SUPPORT:
  400.     //        The mouseUp() method is called if the mouse button is released
  401.     // while the mouse cursor is over the applet's portion of the screen.
  402.     //--------------------------------------------------------------------------
  403.     public boolean mouseUp(Event evt, int x, int y)
  404.     {
  405.         // TODO: Place applet mouseUp code here
  406.         return true;
  407.     }
  408.  
  409.  
  410.     // TODO: Place additional applet code here
  411.  
  412. }
  413.