home *** CD-ROM | disk | FTP | other *** search
/ Developing for Microsoft …tive Animated Characters / DEV_AGENTA.ISO / Examples / java / hello3 / Hello3J.java < prev    next >
Encoding:
Java Source  |  1997-08-20  |  5.1 KB  |  217 lines

  1. //==========================================================================
  2. //
  3. //  THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY
  4. //  KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  5. //  IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A PARTICULAR
  6. //  PURPOSE.
  7. //
  8. //  Copyright (C) 1997 Microsoft Corporation.  All Rights Reserved.
  9. //
  10. //--------------------------------------------------------------------------
  11. //
  12. // This sample demonstrates loading and showing 2 Microsoft Agent characters
  13. // and synchronizing their actions.  It also demonstrates the use of the 
  14. // RequestComplete notification and an Agent notify sink.
  15. //
  16. //==========================================================================
  17.  
  18. // Standard java imports
  19.  
  20. import java.applet.*;
  21. import java.awt.*;
  22.  
  23. // COM and Microsoft Agent imports
  24.  
  25. import com.ms.com.Variant;
  26. import agentsvr.AgentServer;
  27. import agentsvr.IAgent;
  28. import agentsvr.IAgentCharacter;
  29.  
  30. // The Hello3J class (Single threaded)
  31.  
  32. public class Hello3J extends Applet
  33. {
  34.     // Private members necessary for Agent instantiation.  Change the
  35.     // location of the characters to where they are stored on your system.
  36.  
  37.     private IAgent            m_Agent = null;
  38.     private int                m_WaitID[] = {-1};
  39.     private int                m_RequestID[] = {0};
  40.     private IAgentCharacter    m_Merlin[] = {null};
  41.     private int                m_MerlinID[] = {-1};
  42.     private IAgentCharacter    m_Genie[] = {null};
  43.     private int                m_GenieID[] = {-1};
  44.     private final String    m_MerlinPath = "\\program files\\microsoft agent\\characters\\merlin.acs";
  45.     private final String    m_GeniePath = "\\program files\\microsoft agent\\characters\\genie.acs";
  46.  
  47.     // Declare an Agent Notify Sink so that we can get
  48.     // notification callbacks from the Agent server.
  49.  
  50.     private Notify            m_Sink = null;
  51.     private int                m_SinkID[] = {-1};
  52.  
  53.     private final int        FALSE = 0;
  54.     private final int        TRUE = 1;
  55.  
  56.  
  57.     public Hello3J()
  58.     {
  59.         // Constructor left intentionally blank
  60.     }
  61.  
  62.     public String getAppletInfo()
  63.     {
  64.         return "Name: Hello3J\r\n" +
  65.                "Author: anonymous\r\n" +
  66.                "Created with Microsoft Visual J++ Version 1.1";
  67.     }
  68.  
  69.     public void init()
  70.     {
  71.         resize(0, 0);
  72.     }
  73.  
  74.     public void destroy()
  75.     {
  76.     }
  77.  
  78.     public void paint(Graphics g)
  79.     {
  80.     }
  81.  
  82.     public void start()
  83.     {
  84.         // Start the Microsoft Agent Server
  85.  
  86.         m_Agent = (IAgent) new AgentServer();
  87.  
  88.         // Create and register a notify sink
  89.  
  90.         m_Sink = new Notify(this);
  91.  
  92.         m_Agent.Register(m_Sink, m_SinkID);
  93.  
  94.         try
  95.         {
  96.             // The filespec parameter of the Load method is a 
  97.             // COM variant to accept alternate Agent data providers.
  98.             // We want a standard provider so we can just specify
  99.             // the filespec for our character.
  100.  
  101.             Variant characterPath = new Variant();
  102.             characterPath.putString(m_MerlinPath);
  103.  
  104.             // Load the characters
  105.  
  106.             m_Agent.Load(characterPath,
  107.                          m_MerlinID,
  108.                          m_RequestID);
  109.  
  110.             characterPath.putString(m_GeniePath);
  111.  
  112.             m_Agent.Load(characterPath,
  113.                          m_GenieID,
  114.                          m_RequestID);
  115.             
  116.         }
  117.         catch(com.ms.com.ComException e)
  118.         {
  119.             // Hmmm.  We couldn't load a character so what else
  120.             // can we do?
  121.  
  122.             stop();
  123.             return;
  124.         }
  125.  
  126.         try {
  127.  
  128.             // Get the IAgentCharacter interface for the loaded
  129.             // characters by passing IDs to the Agent server.
  130.  
  131.             m_Agent.GetCharacter(m_MerlinID[0],
  132.                                  m_Merlin);
  133.  
  134.             m_Agent.GetCharacter(m_GenieID[0],
  135.                                  m_Genie);
  136.  
  137.             // Position the characters
  138.  
  139.             m_Genie[0].MoveTo((short)0, (short)100, 0, m_RequestID);
  140.             m_Merlin[0].MoveTo((short)200, (short)100, 0, m_RequestID);
  141.  
  142.             // Show them
  143.  
  144.             m_Genie[0].Show(FALSE, m_RequestID);
  145.             m_Merlin[0].Show(FALSE, m_RequestID);
  146.  
  147.             // Have Genie gesture towards Merlin
  148.  
  149.             m_Genie[0].GestureAt((short)150, (short)100, m_RequestID);
  150.  
  151.             // And have Genie say hello to Merlin
  152.  
  153.             m_Genie[0].Speak("Hi there Merlin.", null, m_WaitID);
  154.  
  155.             // Make Merlin wait for Genie to finish speaking
  156.  
  157.             m_Merlin[0].Wait(m_WaitID[0], m_RequestID);
  158.  
  159.             // Have Merlin gesture towards Genie
  160.  
  161.             m_Merlin[0].GestureAt((short)0, (short)100, m_RequestID);
  162.  
  163.             // And then speak back to Genie.  Use a request id owned by the
  164.             // sink object.  When we get a request complete notification, the
  165.             // sink will call back into the applet.
  166.  
  167.             m_Merlin[0].Speak("Hi Genie.  See you later", null, m_Sink.m_DoneID);
  168.  
  169.         }
  170.         catch(com.ms.com.ComException e)
  171.         {
  172.             stop();
  173.         }
  174.     }
  175.     
  176.     public void stop()
  177.     {
  178.         // Unload the characters
  179.  
  180.         if (m_MerlinID[0] > 0)
  181.         {
  182.             m_Merlin[0] = null;
  183.             m_Agent.Unload(m_MerlinID[0]);
  184.             m_MerlinID[0] = -1;
  185.         }
  186.  
  187.         if (m_GenieID[0] > 0)
  188.         {
  189.             m_Genie[0] = null;
  190.             m_Agent.Unload(m_GenieID[0]);
  191.             m_GenieID[0] = -1;
  192.         }
  193.  
  194.         // Unregister the notify sink, we're done with the
  195.         // Agent server.
  196.  
  197.         if (m_SinkID[0] > 0)
  198.         {
  199.             m_Agent.Unregister(m_SinkID[0]);
  200.             m_SinkID[0] = -1;
  201.         }
  202.  
  203.         m_Sink = null;
  204.         m_Agent = null;
  205.     }
  206.  
  207.     public void HideCharacters()
  208.     {
  209.         // This method is called by the notification sink when the
  210.         // last Agent request has completed.  We'll just hide the
  211.         // characters.
  212.  
  213.         m_Merlin[0].Hide(FALSE, m_RequestID);
  214.         m_Genie[0].Hide(FALSE, m_RequestID);
  215.     }
  216. }
  217.