⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 connect.java

📁 java开发的netbeans插件中的application原型
💻 JAVA
字号:
/*
 * Connect.java
 *
 * Created on 2006年12月10日, 下午2:15
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package cn.edu.tsinghua.thss.talkie.connect;

import cn.edu.tsinghua.thss.talkie.capture.CaptureThread;
import cn.edu.tsinghua.thss.talkie.capture.SerializableImageData;
import java.awt.Dimension;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.UnknownHostException;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import testappforjavacup.testGUI;

/**
 *
 * @author promenade
 */
public class Connect {
    
    DataLink dataLink = null;
    ObjectInputStream input = null;
    ObjectOutputStream output = null;
    JFrame screenFrame = null;
    JLabel screenLabel = null;
    
//    String SERVER_CHAT_FLAG = "SERVER>>>";
//    String CLIENT_CHAT_FLAG = "CLIENT>>>";
    public static final String CHAT_FLAG ="CHAT>>>";
    public static final String COMMAND_FLAG="COMMAND>>>";
    
    /** Creates a new instance of Connect */
    public Connect(String ip) {
        dataLink = new DataLink(ip);
    }
    
    public void close(){
        try {
            this.dataLink.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
    
    
    protected boolean toOpen(){
        try {
            if( dataLink.open() ) {
                this.output = new ObjectOutputStream( dataLink.getOutputStream() );
                gui.appendNewMessage( "Connected to" + this.dataLink.ipAddr + "\n" );
                return true;
            }
        } catch (UnknownHostException e){
            
        } catch (IOException ex) {
            //ex.printStackTrace();
        }
        return false;
    }
    
    protected void toListen(){
        try {
            gui.appendNewMessage( "Waiting for other to connect" + "\n" );
            dataLink.listen();
            gui.appendNewMessage( "somebody connected!" + "\n" );
            this.input = new ObjectInputStream( dataLink.getInputStream() );
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
    
    public ObjectInputStream getInputStream(){
        return this.input;
    }
    
    public ObjectOutputStream getOutputStream(){
        return this.output;
    }
    
    public void sendString( String msg ){
        try {
            this.getOutputStream().writeObject( CHAT_FLAG + msg );
            gui.appendNewMessage( "Send:" + msg + "\n" );
            this.getOutputStream().flush();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
    
    public void sendCommand( String command ){
        try {
            this.getOutputStream().writeObject( COMMAND_FLAG + command );
            gui.appendNewMessage( "System Command:" + command + "\n" );
            this.getOutputStream().flush();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
    }
    
    public void sendImage( BufferedImage image ){
        try {
//            long time = System.currentTimeMillis();
            SerializableImageData sid = new SerializableImageData( image );
            this.getOutputStream().writeObject( sid );
            this.getOutputStream().flush();
//            time = System.currentTimeMillis() -time;
        } catch (IOException ioException) {
            ioException.printStackTrace();
        }
    }
    
    public Object receiveObject(){
        Object obj = null;
        try {
            Object inputObj = this.getInputStream().readObject();
            if( inputObj instanceof String ){
                String msg = (String)inputObj;
                if( msg.substring( 0, CHAT_FLAG.length() ).compareTo( CHAT_FLAG ) == 0 ){
                    msg = msg.substring( CHAT_FLAG.length() );
                    obj = msg;
                    gui.appendNewMessage( "Receive:" + msg + "\n" );
                } else if( msg.substring( 0, COMMAND_FLAG.length() ).compareTo( COMMAND_FLAG ) == 0 ){
                    msg = msg.substring( COMMAND_FLAG.length() );
                    if( msg.compareTo("send my screen") == 0 ){
                        int answer = JOptionPane.showConfirmDialog( this.gui,"Agree to view the screen? ","View Screen",JOptionPane.YES_NO_OPTION );
                        System.out.println(answer);
                        this.sendCommand( (answer==0)?"accept screen":"refuse screen" );
                        if( answer==0 ){
                            screenFrame = new JFrame();
                            screenFrame.setSize(new Dimension(800,600));
                            screenFrame.setVisible(true);
                            
                            screenLabel = new JLabel();
                            screenLabel.setSize(new Dimension(800,600));
                            screenFrame.add( screenLabel );
                        }
                    } else if( msg.compareTo( "accept screen" ) == 0 ){
                        new CaptureThread(this);
                    }else if( msg.compareTo( "refuse screen" ) == 0 ){
                        
                    }
                }
            } else if( inputObj instanceof SerializableImageData ){
                obj = inputObj;
                this.screenLabel.setIcon( new ImageIcon( ((SerializableImageData)inputObj).getImage().getScaledInstance(800,600,Image.SCALE_SMOOTH) ) );
            }
        } catch (ClassNotFoundException classNotFoundException) {
            classNotFoundException.printStackTrace();
        } catch(Exception e){
            e.printStackTrace();
        }
        return obj;
    }
    
    
    testGUI gui;
    public void setView( testGUI gui ){
        this.gui =gui;
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -