home *** CD-ROM | disk | FTP | other *** search
/ Learn Java Now / Learn_Java_Now_Microsoft_1996.iso / MsDev / Samples / Microsoft / JavaCallingCOM / usecom.java < prev    next >
Encoding:
Java Source  |  1996-08-27  |  7.1 KB  |  225 lines

  1. //******************************************************************************
  2. // usecom.java:    Applet
  3. //
  4. //******************************************************************************
  5. import java.applet.*;
  6. import java.awt.*;
  7. import usecomframe;
  8.  
  9. // import the classes from the type library
  10. import comserver.*;
  11.  
  12. // Helper class for holding a bunch of values
  13. class BeepValue
  14. {
  15.     String name;
  16.     int value;
  17. }
  18.  
  19. //==============================================================================
  20. // Main Class for applet usecom
  21. //
  22. //==============================================================================
  23.  
  24. public class usecom extends Applet
  25. {
  26.     // the interface to the COM object
  27.     ICOMBeeper m_beeper;
  28.  
  29.     // user interface components
  30.     List m_list;
  31.     Label m_label;
  32.     Button m_button;
  33.  
  34.     // a list of names and values
  35.     static final int countNames = 6;
  36.     BeepValue m_nameList[];
  37.  
  38.     // STANDALONE APPLICATION SUPPORT
  39.     //     The main() method acts as the applet's entry point when it is run
  40.     // as a standalone application. It is ignored if the applet is run from
  41.     // within an HTML page.
  42.     //--------------------------------------------------------------------------
  43.     public static void main(String args[])
  44.     {
  45.         // Create Toplevel Window to contain applet usecom
  46.         //----------------------------------------------------------------------
  47.         usecomframe frame = new usecomframe("usecom");
  48.  
  49.         // Must show Frame before we size it so insets() will return valid values
  50.         //----------------------------------------------------------------------
  51.         frame.show();
  52.         frame.hide();
  53.         frame.resize(frame.insets().left + frame.insets().right  + 320,
  54.                      frame.insets().top  + frame.insets().bottom + 240);
  55.  
  56.         // The following code starts the applet running within the frame window.
  57.         // It also calls GetParameters() to retrieve parameter values from the
  58.         // command line, and sets m_fStandAlone to true to prevent init() from
  59.         // trying to get them from the HTML page.
  60.         //----------------------------------------------------------------------
  61.         usecom applet_usecom = new usecom();
  62.  
  63.         frame.add("Center", applet_usecom);
  64.         applet_usecom.init();
  65.         applet_usecom.start();
  66.         frame.show();
  67.     }
  68.  
  69.     // usecom Class Constructor
  70.     //--------------------------------------------------------------------------
  71.     public usecom()
  72.     {
  73.     }
  74.  
  75.     // APPLET INFO SUPPORT:
  76.     //        The getAppletInfo() method returns a string describing the applet's
  77.     // author, copyright date, or miscellaneous information.
  78.     //--------------------------------------------------------------------------
  79.     public String getAppletInfo()
  80.     {
  81.         return "Name: usecom\r\n" +
  82.                "Author: Dan Jinguji\r\n" +
  83.                "Created with Microsoft Visual J++ Version 1.0";
  84.     }
  85.  
  86.  
  87.     // The init() method is called by the AWT when an applet is first loaded or
  88.     // reloaded.  Override this method to perform whatever initialization your
  89.     // applet needs, such as initializing data structures, loading images or
  90.     // fonts, creating frame windows, setting the layout manager, or adding UI
  91.     // components.
  92.     //--------------------------------------------------------------------------
  93.     public void init()
  94.     {
  95.         resize(240, 200);
  96.  
  97.         // Create the components for the user interface
  98.         setLayout(null);
  99.         m_label = new Label();
  100.         m_list = new List();
  101.         m_button = new Button();
  102.         add(m_label);
  103.         add(m_list);
  104.         add(m_button);
  105.  
  106.         // Place the components in the applet
  107.         m_list.reshape(50, 50, 120, 80);
  108.         m_label.reshape(50, 130, 100, 15);
  109.         m_button.reshape(60, 160, 100, 30);
  110.  
  111.         // Create the COM object
  112.         m_beeper = new CCOMBeeper();
  113.  
  114.         // Create the list of names and values
  115.         m_nameList = new BeepValue[countNames];
  116.         for(int i = 0; i < countNames; i++)
  117.             m_nameList[i] = new BeepValue();
  118.  
  119.         // Fill the list of names and values using the COM object
  120.         // Set the current sound using the contants from the type library
  121.         m_beeper.putSound(BeeperConstants.Default);
  122.         // ICOMBeeper.getSoundName returns a string for the current sound
  123.         m_nameList[0].name = m_beeper.getSoundName();
  124.         // Get the associated number back as well
  125.         m_nameList[0].value = m_beeper.getSound();
  126.  
  127.         // Repeat for the rest of the constants
  128.         m_beeper.putSound(BeeperConstants.Asterisk);
  129.         m_nameList[1].name = m_beeper.getSoundName();
  130.         m_nameList[1].value = m_beeper.getSound();
  131.  
  132.         m_beeper.putSound(BeeperConstants.Exclamation);
  133.         m_nameList[2].name = m_beeper.getSoundName();
  134.         m_nameList[2].value = m_beeper.getSound();
  135.         
  136.         m_beeper.putSound(BeeperConstants.Hand);
  137.         m_nameList[3].name = m_beeper.getSoundName();
  138.         m_nameList[3].value = m_beeper.getSound();
  139.         
  140.         m_beeper.putSound(BeeperConstants.Question);
  141.         m_nameList[4].name = m_beeper.getSoundName();
  142.         m_nameList[4].value = m_beeper.getSound();
  143.         
  144.         m_beeper.putSound(BeeperConstants.StandardBeep);
  145.         m_nameList[5].name = m_beeper.getSoundName();
  146.         m_nameList[5].value = m_beeper.getSound();
  147.  
  148.         // Use the list of names to populate the list
  149.         for(int i = 0; i < countNames; i ++)
  150.             m_list.addItem(m_nameList[i].name);
  151.         // Adjust the user interface for the components
  152.         m_label.setAlignment(Label.CENTER);
  153.         m_button.setLabel("Play the Sound");
  154.  
  155.         // Set the current sound to the first value
  156.         setBeeperSound(0);
  157.         m_list.select(0);
  158.     }
  159.  
  160.     // Place additional applet clean up code here.  destroy() is called when
  161.     // when you applet is terminating and being unloaded.
  162.     //-------------------------------------------------------------------------
  163.     public void destroy()
  164.     {
  165.     }
  166.  
  167.     // UseCOM Paint Handler
  168.     //--------------------------------------------------------------------------
  169.     public void paint(Graphics g)
  170.     {
  171.         g.drawString("Created with Microsoft Visual J++ Version 1.0", 10, 20);
  172.     }
  173.  
  174.     //        The start() method is called when the page containing the applet
  175.     // first appears on the screen. The AppletWizard's initial implementation
  176.     // of this method starts execution of the applet's thread.
  177.     //--------------------------------------------------------------------------
  178.     public void start()
  179.     {
  180.     }
  181.     
  182.     //        The stop() method is called when the page containing the applet is
  183.     // no longer on the screen. The AppletWizard's initial implementation of
  184.     // this method stops execution of the applet's thread.
  185.     //--------------------------------------------------------------------------
  186.     public void stop()
  187.     {
  188.     }
  189.  
  190.     // Handle events from the components in the applet
  191.     public boolean handleEvent(Event evt)
  192.     {
  193.         // Event from the button
  194.         if(evt.target == m_button)
  195.         {
  196.             // Play the selected sound
  197.             m_beeper.Play();
  198.             return true;
  199.         }
  200.  
  201.         // Event from the list
  202.         if(evt.target == m_list)
  203.         {
  204.             // Set the current sound
  205.             List l = (List)m_list;
  206.             setBeeperSound(l.getSelectedIndex());
  207.             return true;
  208.         }
  209.  
  210.         // Otherwise, pass the event on to super
  211.         return false;
  212.     }
  213.  
  214.     // Helper function to update based on the list selection
  215.     void setBeeperSound(int index)
  216.     {
  217.         // get the value from the list of names and values
  218.         int i = m_nameList[index].value;
  219.         // echo the value in the label component
  220.         m_label.setText("Sound number: " + i);
  221.         // set the current sound
  222.         m_beeper.putSound(i);
  223.     }
  224. }
  225.