home *** CD-ROM | disk | FTP | other *** search
/ Developing for Microsoft …tive Animated Characters / DEV_AGENTA.ISO / Examples / java / hello / HelloJ.java < prev    next >
Encoding:
Java Source  |  1997-08-19  |  2.9 KB  |  137 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 the simplest use of Microsoft Agent from a 
  13. // Java Applet.
  14. //
  15. //==========================================================================
  16.  
  17. // Standard java imports
  18.  
  19. import java.applet.*;
  20. import java.awt.*;
  21.  
  22. // COM and Microsoft Agent imports
  23.  
  24. import com.ms.com.Variant;
  25. import agentsvr.AgentServer;
  26. import agentsvr.IAgent;
  27. import agentsvr.IAgentCharacter;
  28.  
  29. // The HelloAgentJava class (Single threaded)
  30.  
  31. public class HelloJ extends Applet
  32. {
  33.     // Private members necessary for Agent instantiation
  34.  
  35.     private IAgent            m_Agent = null;
  36.     private IAgentCharacter    m_Merlin[] = {null};
  37.     private int                m_MerlinID[] = {-1};
  38.     private int                m_RequestID[] = {0};
  39.     private final String    m_CharacterPath = "\\program files\\microsoft agent\\characters\\merlin.acs";
  40.  
  41.     private final int        FALSE = 0;
  42.     private final int        TRUE = 1;
  43.  
  44.  
  45.     public HelloJ()
  46.     {
  47.         // Constructor left intentionally blank
  48.     }
  49.  
  50.     public String getAppletInfo()
  51.     {
  52.         return "Name: HelloJ\r\n" +
  53.                "Author: anonymous\r\n" +
  54.                "Created with Microsoft Visual J++ Version 1.1";
  55.     }
  56.  
  57.     public void init()
  58.     {
  59.         resize(0, 0);
  60.     }
  61.  
  62.     public void destroy()
  63.     {
  64.     }
  65.  
  66.     public void paint(Graphics g)
  67.     {
  68.     }
  69.  
  70.     public void start()
  71.     {
  72.         // Start the Microsoft Agent Server
  73.  
  74.         m_Agent = (IAgent) new AgentServer();
  75.  
  76.         try
  77.         {
  78.             // The filespec parameter of the Load method is a 
  79.             // COM variant to accept alternate Agent data providers.
  80.             // We want a standard provider so we can just specify
  81.             // the filespec for our character.
  82.  
  83.             Variant characterPath = new Variant();
  84.             characterPath.putString(m_CharacterPath);
  85.  
  86.             // Load the character
  87.  
  88.             m_Agent.Load(characterPath,
  89.                          m_MerlinID,
  90.                          m_RequestID);
  91.             
  92.         }
  93.         catch(com.ms.com.ComException e)
  94.         {
  95.             // Hmmm.  We couldn't load the character so what else
  96.             // can we do?
  97.  
  98.             stop();
  99.             return;
  100.         }
  101.  
  102.         try {
  103.  
  104.             // Get the IAgentCharacter interface for the loaded
  105.             // character by passing it's ID to the Agent server.
  106.  
  107.             m_Agent.GetCharacter(m_MerlinID[0],
  108.                                  m_Merlin);
  109.  
  110.             // Show the character
  111.  
  112.             m_Merlin[0].Show(FALSE, m_RequestID);
  113.  
  114.             // And speak hello
  115.  
  116.             m_Merlin[0].Speak("Hello World!", null, m_RequestID);
  117.  
  118.         }
  119.         catch(com.ms.com.ComException e)
  120.         {
  121.             stop();
  122.         }
  123.     }
  124.     
  125.     public void stop()
  126.     {
  127.         if (m_MerlinID[0] > 0)
  128.         {
  129.             m_Merlin[0] = null;
  130.             m_Agent.Unload(m_MerlinID[0]);
  131.             m_MerlinID[0] = -1;
  132.         }
  133.  
  134.         m_Agent = null;
  135.     }
  136. }
  137.