home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 39 / IOPROG_39.ISO / SOFT / sdkjava40.exe / data1.cab / fg_Samples / Samples / DirectX / dplay / SendBox.java < prev    next >
Encoding:
Java Source  |  2000-05-04  |  2.1 KB  |  103 lines

  1. // 
  2. //  (C) Copyright 1995 - 1999 Microsoft Corporation.  All rights reserved.
  3. // 
  4. //  File:            SendBox.java
  5. //  Description:                Broadcasts text data
  6. //
  7. import com.ms.com.*;
  8. import com.ms.directX.*;
  9. import com.ms.ui.*;
  10. import java.awt.*;
  11.  
  12. class SendBox extends TextArea implements DirectXConstants {
  13.  
  14.     int                m_FromId=0;
  15.     DirectPlay2        m_dp=null;
  16.     String            m_FromName;
  17.     
  18.     int                SIZE_INCREMENT        = 300;
  19.     int                m_MaxMsgSize        = SIZE_INCREMENT;
  20.     byte            m_Msg[]                = new byte[m_MaxMsgSize];
  21.     int                m_nCount;
  22.     DpChat            m_ChatInfo;
  23.  
  24.  
  25.     //=-----------------------------
  26.     // SendBox
  27.     //=-----------------------------        
  28.     SendBox(DirectPlay2 dp, int id, String sName,DpChat chatinfo){
  29.         super();    
  30.         m_FromId=id;
  31.         m_dp=dp;
  32.         m_FromName=sName;
  33.         m_ChatInfo=chatinfo;
  34.  
  35.     }
  36.  
  37.     //=-----------------------------
  38.     // cleanup
  39.     //=-----------------------------        
  40.     void cleanup(){
  41.         m_ChatInfo=null;
  42.         m_dp=null;
  43.         System.gc();
  44.     }
  45.  
  46.  
  47.  
  48.     //=-----------------------------
  49.     // SendChatMessage
  50.     //=-----------------------------        
  51.     void SendChatMessage()
  52.         {
  53.             
  54.  
  55.         String szEditText=this.getText();
  56.         String szOut=m_FromName+": "+szEditText.trim()+"\n";
  57.  
  58.         m_ChatInfo.setReceiveInfo(szOut);
  59.         int outLength=szOut.length();
  60.     
  61.         //- take care of very long strings
  62.         if (outLength>m_MaxMsgSize){
  63.             m_MaxMsgSize += SIZE_INCREMENT;
  64.             m_Msg= new byte[m_MaxMsgSize];
  65.         }
  66.  
  67.         //- copy the message into our buffer
  68.         for (int i=0;i<outLength;i++){
  69.             m_Msg[i]=(byte)szOut.charAt(i);
  70.         }
  71.  
  72.         //- clear our buffer
  73.         this.setText("");
  74.  
  75.         int dataSize=szOut.length();                
  76.         int flags=0;
  77.         
  78.         
  79.         try {
  80.             m_dp.send(m_FromId,  0,  DPSEND_GUARANTEED, m_Msg, dataSize);                        
  81.         }
  82.         catch (ComException e){
  83.             int hr = e.getHResult();
  84.             System.out.println("Send faulted" +hr);
  85.         }
  86.     }
  87.  
  88.     //=-----------------------------
  89.     // handleEvent
  90.     //=-----------------------------        
  91.     public boolean handleEvent(Event evt){
  92.         if (evt.id==Event.KEY_PRESS){
  93.             char chr=(char)evt.key;
  94.             if (chr=='\n'){            
  95.                 SendChatMessage();
  96.             }
  97.             
  98.         }
  99.         return super.handleEvent(evt);
  100.     }
  101. }
  102.  
  103.