home *** CD-ROM | disk | FTP | other *** search
Java Source | 1996-08-27 | 7.1 KB | 225 lines |
- //******************************************************************************
- // usecom.java: Applet
- //
- //******************************************************************************
- import java.applet.*;
- import java.awt.*;
- import usecomframe;
-
- // import the classes from the type library
- import comserver.*;
-
- // Helper class for holding a bunch of values
- class BeepValue
- {
- String name;
- int value;
- }
-
- //==============================================================================
- // Main Class for applet usecom
- //
- //==============================================================================
-
- public class usecom extends Applet
- {
- // the interface to the COM object
- ICOMBeeper m_beeper;
-
- // user interface components
- List m_list;
- Label m_label;
- Button m_button;
-
- // a list of names and values
- static final int countNames = 6;
- BeepValue m_nameList[];
-
- // STANDALONE APPLICATION SUPPORT
- // The main() method acts as the applet's entry point when it is run
- // as a standalone application. It is ignored if the applet is run from
- // within an HTML page.
- //--------------------------------------------------------------------------
- public static void main(String args[])
- {
- // Create Toplevel Window to contain applet usecom
- //----------------------------------------------------------------------
- usecomframe frame = new usecomframe("usecom");
-
- // Must show Frame before we size it so insets() will return valid values
- //----------------------------------------------------------------------
- frame.show();
- frame.hide();
- frame.resize(frame.insets().left + frame.insets().right + 320,
- frame.insets().top + frame.insets().bottom + 240);
-
- // The following code starts the applet running within the frame window.
- // It also calls GetParameters() to retrieve parameter values from the
- // command line, and sets m_fStandAlone to true to prevent init() from
- // trying to get them from the HTML page.
- //----------------------------------------------------------------------
- usecom applet_usecom = new usecom();
-
- frame.add("Center", applet_usecom);
- applet_usecom.init();
- applet_usecom.start();
- frame.show();
- }
-
- // usecom Class Constructor
- //--------------------------------------------------------------------------
- public usecom()
- {
- }
-
- // APPLET INFO SUPPORT:
- // The getAppletInfo() method returns a string describing the applet's
- // author, copyright date, or miscellaneous information.
- //--------------------------------------------------------------------------
- public String getAppletInfo()
- {
- return "Name: usecom\r\n" +
- "Author: Dan Jinguji\r\n" +
- "Created with Microsoft Visual J++ Version 1.0";
- }
-
-
- // The init() method is called by the AWT when an applet is first loaded or
- // reloaded. Override this method to perform whatever initialization your
- // applet needs, such as initializing data structures, loading images or
- // fonts, creating frame windows, setting the layout manager, or adding UI
- // components.
- //--------------------------------------------------------------------------
- public void init()
- {
- resize(240, 200);
-
- // Create the components for the user interface
- setLayout(null);
- m_label = new Label();
- m_list = new List();
- m_button = new Button();
- add(m_label);
- add(m_list);
- add(m_button);
-
- // Place the components in the applet
- m_list.reshape(50, 50, 120, 80);
- m_label.reshape(50, 130, 100, 15);
- m_button.reshape(60, 160, 100, 30);
-
- // Create the COM object
- m_beeper = new CCOMBeeper();
-
- // Create the list of names and values
- m_nameList = new BeepValue[countNames];
- for(int i = 0; i < countNames; i++)
- m_nameList[i] = new BeepValue();
-
- // Fill the list of names and values using the COM object
- // Set the current sound using the contants from the type library
- m_beeper.putSound(BeeperConstants.Default);
- // ICOMBeeper.getSoundName returns a string for the current sound
- m_nameList[0].name = m_beeper.getSoundName();
- // Get the associated number back as well
- m_nameList[0].value = m_beeper.getSound();
-
- // Repeat for the rest of the constants
- m_beeper.putSound(BeeperConstants.Asterisk);
- m_nameList[1].name = m_beeper.getSoundName();
- m_nameList[1].value = m_beeper.getSound();
-
- m_beeper.putSound(BeeperConstants.Exclamation);
- m_nameList[2].name = m_beeper.getSoundName();
- m_nameList[2].value = m_beeper.getSound();
-
- m_beeper.putSound(BeeperConstants.Hand);
- m_nameList[3].name = m_beeper.getSoundName();
- m_nameList[3].value = m_beeper.getSound();
-
- m_beeper.putSound(BeeperConstants.Question);
- m_nameList[4].name = m_beeper.getSoundName();
- m_nameList[4].value = m_beeper.getSound();
-
- m_beeper.putSound(BeeperConstants.StandardBeep);
- m_nameList[5].name = m_beeper.getSoundName();
- m_nameList[5].value = m_beeper.getSound();
-
- // Use the list of names to populate the list
- for(int i = 0; i < countNames; i ++)
- m_list.addItem(m_nameList[i].name);
- // Adjust the user interface for the components
- m_label.setAlignment(Label.CENTER);
- m_button.setLabel("Play the Sound");
-
- // Set the current sound to the first value
- setBeeperSound(0);
- m_list.select(0);
- }
-
- // Place additional applet clean up code here. destroy() is called when
- // when you applet is terminating and being unloaded.
- //-------------------------------------------------------------------------
- public void destroy()
- {
- }
-
- // UseCOM Paint Handler
- //--------------------------------------------------------------------------
- public void paint(Graphics g)
- {
- g.drawString("Created with Microsoft Visual J++ Version 1.0", 10, 20);
- }
-
- // The start() method is called when the page containing the applet
- // first appears on the screen. The AppletWizard's initial implementation
- // of this method starts execution of the applet's thread.
- //--------------------------------------------------------------------------
- public void start()
- {
- }
-
- // The stop() method is called when the page containing the applet is
- // no longer on the screen. The AppletWizard's initial implementation of
- // this method stops execution of the applet's thread.
- //--------------------------------------------------------------------------
- public void stop()
- {
- }
-
- // Handle events from the components in the applet
- public boolean handleEvent(Event evt)
- {
- // Event from the button
- if(evt.target == m_button)
- {
- // Play the selected sound
- m_beeper.Play();
- return true;
- }
-
- // Event from the list
- if(evt.target == m_list)
- {
- // Set the current sound
- List l = (List)m_list;
- setBeeperSound(l.getSelectedIndex());
- return true;
- }
-
- // Otherwise, pass the event on to super
- return false;
- }
-
- // Helper function to update based on the list selection
- void setBeeperSound(int index)
- {
- // get the value from the list of names and values
- int i = m_nameList[index].value;
- // echo the value in the label component
- m_label.setText("Sound number: " + i);
- // set the current sound
- m_beeper.putSound(i);
- }
- }
-