home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgLangD.iso / Java++ / VJ / SAMPLES / MICROSOFT / CABANDSIGN / JAVABEEP.JAVA < prev    next >
Encoding:
Java Source  |  1997-01-21  |  11.5 KB  |  332 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. //    and javabeep.start methods
  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.     }
  97.  
  98.     // APPLET INFO SUPPORT:
  99.     //        The getAppletInfo() method returns a string describing the applet's
  100.     // author, copyright date, or miscellaneous information.
  101.     //--------------------------------------------------------------------------
  102.     public String getAppletInfo()
  103.     {
  104.         return "Name: javabeep\r\n" +
  105.                "Author: Stephen Horne\r\n" +
  106.                "Created with Microsoft Visual J++";
  107.     }
  108.  
  109.  
  110.     // The init() method is called by the AWT when an applet is first loaded or
  111.     // reloaded.  Override this method to perform whatever initialization your
  112.     // applet needs, such as initializing data structures, loading images or
  113.     // fonts, creating frame windows, setting the layout manager, or adding UI
  114.     // components.
  115.     //--------------------------------------------------------------------------
  116.     public void init()
  117.     {
  118.         // If you use a ResourceWizard-generated "control creator" class to
  119.         // arrange controls in your applet, you may want to call its
  120.         // CreateControls() method from within this method. Remove the following
  121.         // call to resize() before adding the call to CreateControls();
  122.         // CreateControls() does its own resizing.
  123.         //----------------------------------------------------------------------
  124.         resize(320, 240);
  125.  
  126.     }
  127.  
  128.     // Place additional applet clean up code here.  destroy() is called when
  129.     // when you applet is terminating and being unloaded.
  130.     //-------------------------------------------------------------------------
  131.     public void destroy()
  132.     {
  133.     }
  134.  
  135.     // ANIMATION SUPPORT:
  136.     //        Draws the next image, if all images are currently loaded
  137.     //--------------------------------------------------------------------------
  138.     private void displayImage(Graphics g)
  139.     {
  140.         if (!m_fAllLoaded)
  141.             return;
  142.  
  143.         // Draw Image in center of applet
  144.         //----------------------------------------------------------------------
  145.         g.drawImage(m_Images[m_nCurrImage],
  146.                    (size().width - m_nImgWidth)   / 2,
  147.                    (size().height - m_nImgHeight) / 2, null);
  148.  
  149.         // Draw a click me label
  150.         FontMetrics fm = getFontMetrics(getFont());
  151.         String message = new String("Click me with the mouse!");
  152.         g.drawString(message,(size().width-fm.stringWidth(message))/2,                
  153.                      3*(size().height)/4);
  154.     }
  155.  
  156.     // javabeep Paint Handler
  157.     //--------------------------------------------------------------------------
  158.     public void paint(Graphics g)
  159.     {
  160.         // ANIMATION SUPPORT:
  161.         //        The following code displays a status message until all the
  162.         // images are loaded. Then it calls displayImage to display the current
  163.         // image.
  164.         //----------------------------------------------------------------------
  165.         if (m_fAllLoaded)
  166.         {
  167.             Rectangle r = g.getClipRect();
  168.             
  169.             g.clearRect(r.x, r.y, r.width, r.height);
  170.             displayImage(g);
  171.         }
  172.         else
  173.             g.drawString("Loading images...", 10, 20);
  174.  
  175.     }
  176.  
  177.     //        The start() method is called when the page containing the applet
  178.     // first appears on the screen. The AppletWizard's initial implementation
  179.     // of this method starts execution of the applet's thread.
  180.     //--------------------------------------------------------------------------
  181.     public void start()
  182.     {
  183.         if (m_javabeep == null)
  184.         {
  185.             m_javabeep = new Thread(this);
  186.             m_javabeep.start();
  187.         }
  188.  
  189.         // Create the Beeper object
  190.         m_Beeper = (IBeeper) new Beeper();
  191.     }
  192.     
  193.     //        The stop() method is called when the page containing the applet is
  194.     // no longer on the screen. The AppletWizard's initial implementation of
  195.     // this method stops execution of the applet's thread.
  196.     //--------------------------------------------------------------------------
  197.     public void stop()
  198.     {
  199.         if (m_javabeep != null)
  200.         {
  201.             m_javabeep.stop();
  202.             m_javabeep = null;
  203.         }
  204.  
  205.     }
  206.  
  207.     // THREAD SUPPORT
  208.     //        The run() method is called when the applet's thread is started. If
  209.     // your applet performs any ongoing activities without waiting for user
  210.     // input, the code for implementing that behavior typically goes here. For
  211.     // example, for an applet that performs animation, the run() method controls
  212.     // the display of images.
  213.     //--------------------------------------------------------------------------
  214.     public void run()
  215.     {
  216.         m_nCurrImage = 0;
  217.         
  218.         // If re-entering the page, then the images have already been loaded.
  219.         // m_fAllLoaded == TRUE.
  220.         //----------------------------------------------------------------------
  221.         if (!m_fAllLoaded)
  222.         {
  223.             repaint();
  224.             m_Graphics = getGraphics();
  225.             m_Images   = new Image[NUM_IMAGES];
  226.  
  227.             // Load in all the images
  228.             //------------------------------------------------------------------
  229.             MediaTracker tracker = new MediaTracker(this);
  230.             String strImage;
  231.  
  232.             // For each image in the animation, this method first constructs a
  233.             // string containing the path to the image file; then it begins
  234.             // loading the image into the m_Images array.  Note that the call to
  235.             // getImage will return before the image is completely loaded.
  236.             //------------------------------------------------------------------
  237.             for (int i = 1; i <= NUM_IMAGES; i++)
  238.             {
  239.                 // Build path to next image
  240.                 //--------------------------------------------------------------
  241.                 strImage = "images/img00" + ((i < 10) ? "0" : "") + i + ".gif";
  242.                 if (m_fStandAlone)
  243.                     m_Images[i-1] = Toolkit.getDefaultToolkit().getImage(strImage);
  244.                 else
  245.                     m_Images[i-1] = getImage(getDocumentBase(), strImage);
  246.  
  247.                 tracker.addImage(m_Images[i-1], 0);
  248.             }
  249.  
  250.             // Wait until all images are fully loaded
  251.             //------------------------------------------------------------------
  252.             try
  253.             {
  254.                 tracker.waitForAll();
  255.                 m_fAllLoaded = !tracker.isErrorAny();
  256.             }
  257.             catch (InterruptedException e)
  258.             {
  259.             }
  260.             
  261.             if (!m_fAllLoaded)
  262.             {
  263.                 stop();
  264.                 m_Graphics.drawString("Error loading images!", 10, 40);
  265.                 return;
  266.             }
  267.             
  268.  
  269.             // Assuming all images are same width and height.
  270.             //--------------------------------------------------------------
  271.             m_nImgWidth  = m_Images[0].getWidth(this);
  272.             m_nImgHeight = m_Images[0].getHeight(this);
  273.         }        
  274.         repaint();
  275.  
  276.         while (true)
  277.         {
  278.             try
  279.             {
  280.                 // Draw next image in animation
  281.                 //--------------------------------------------------------------
  282.                 displayImage(m_Graphics);
  283.                 m_nCurrImage++;
  284.                 if (m_nCurrImage == NUM_IMAGES)
  285.                     m_nCurrImage = 0;
  286.  
  287.                 Thread.sleep(50);
  288.             }
  289.             catch (InterruptedException e)
  290.             {
  291.                 stop();
  292.             }
  293.         }
  294.     }
  295.  
  296.     // MOUSE SUPPORT:
  297.     //        The mouseDown() method is called if the mouse button is pressed
  298.     // while the mouse cursor is over the applet's portion of the screen.
  299.     //--------------------------------------------------------------------------
  300.     public boolean mouseDown(Event evt, int x, int y)
  301.     {
  302.         String BeeperString = new String();
  303.  
  304.         // Call Beeper object 'Beep' method. Note that the Beep object has
  305.         // been written such that on the sixth call to Beep(), the Beep object
  306.         // will return an OLE error code which gets thrown as a Java exception.
  307.         // This sample catches the exception and prints an error message
  308.         try
  309.         {
  310.             m_Beeper.Beep();
  311.  
  312.             // Call the Beepers getCount and getItem methods
  313.             for(int i=1;i<=m_Beeper.getCount();i++)
  314.             {
  315.                 // Build a message string from the strings that getItem returns
  316.                 BeeperString+=m_Beeper.getItem(i)+" ";        
  317.             }
  318.         }
  319.         catch(com.ms.com.ComException e)
  320.         {
  321.             // Get the error string
  322.             BeeperString = "Error from Beeper.Beep: handled in mouseDown method";
  323.         }
  324.  
  325.         // Display the string
  326.         m_Graphics.drawString(BeeperString,x,y);
  327.  
  328.         return true;
  329.     }
  330.  
  331. }
  332.