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

📄 simclient.java

📁 实现了功能比较齐全的画板
💻 JAVA
字号:
package net;

import java.net.*;
import java.awt.*;
import java.io.*;
import main.SimPaint;
import panel.SimPaintPanel;
import shape.SimShape;

public class SimClient {

    public Socket serverSocket;
    public ObjectOutputStream oos;
    private final SimPaint aMain;

    public SimClient(SimPaint apaint) {
        aMain = apaint;
    }

    public void sendPaint(SimPaintPanel paint) {
        if (serverSocket == null) {
            return;
        }
        try {
            oos.writeObject(new String("paint"));
            oos.writeObject(new SimPaintPanel(paint));
            oos.flush();
        } catch (IOException e) {
        }
    }

    public void close() {
        if (serverSocket == null) {
            return;
        }
        try {
            oos.writeObject(new String("close"));
            oos.flush();
            oos.close();
        } catch (IOException e) {
        }
    }

    public boolean connect(String ip, int port) {
        if (serverSocket != null) {
            close();
        }
        try {
            serverSocket = new Socket(ip, port);
            oos = new ObjectOutputStream(serverSocket.getOutputStream());
            aMain.paintFrame.sendPaintAll();
            new CListening(aMain, serverSocket).start();
        } catch (UnknownHostException e) {
            return false;
        } catch (IOException e) {
            return false;
        }
        return true;
    }

    public void sendShape(String panelID, SimShape shape, int index) {
        if (serverSocket == null) {
            return;
        }
        try {
            oos.writeObject(new String("shapeUpdate"));
            oos.writeObject(new String(panelID));
            oos.writeObject(new SimShape(shape));
            oos.write(index);
            oos.flush();
        } catch (IOException e) {
        }
    }

    public void sendMessage(String address, String message) {
        if (serverSocket == null) {
            return;
        }
        try {
            oos.writeObject(new String("chat"));
            oos.writeObject(new String(address));
            oos.writeObject(new String(message));
            oos.flush();
        } catch (IOException e) {
        }
    }

    public void sendShape(String panelID, SimShape shape) {
        if (serverSocket == null) {
            return;
        }
        try {
            oos.writeObject(new String("shapeAdd"));
            oos.writeObject(new String(panelID));
            oos.writeObject(new SimShape(shape));
            oos.flush();
        } catch (IOException e) {
        }
    }

    public void sendSize(String panelID, Dimension size) {
        if (serverSocket == null) {
            return;
        }
        try {
            oos.writeObject(new String("size"));
            oos.writeObject(new String(panelID));
            oos.writeObject(new Dimension(size));
            oos.flush();
        } catch (IOException e) {
        }
    }

    public void sendShape(String panelID) {
        if (serverSocket == null) {
            return;
        }
        try {
            oos.writeObject(new String("shapeClear"));
            oos.writeObject(new String(panelID));
            oos.flush();
        } catch (IOException e) {
        }
    }

    public void sendShape(String panelID, int index) {
        if (serverSocket == null) {
            return;
        }
        try {
            oos.writeObject(new String("shapeDelete"));
            oos.writeObject(new String(panelID));
            oos.write(index);
            oos.flush();
        } catch (IOException e) {
        }
    }
}

class CListening extends Thread {

    private final SimPaint aMain;
    private final Socket server;

    public CListening(SimPaint apaint, Socket socket) {
        aMain = apaint;
        server = socket;
    }

    @Override
    public void run() {
        try {
            String line;
            ObjectInputStream ois = new ObjectInputStream(server.getInputStream());
            line = (String) ois.readObject();
            while (true) {
                if (line.equals("paint")) {
                    SimPaintPanel newPaintPanel = (SimPaintPanel) ois.readObject();
                    newPaintPanel.init(true);
                    aMain.paintFrame.addImageTab('[' + newPaintPanel.getID() + ']',
                            newPaintPanel);
                } else if (line.equals("chat")) {
                    String address = (String) ois.readObject();
                    String message = (String) ois.readObject();
                    aMain.addMessage(address, message);
                } else if (line.equals("shapeClear")) {
                    String id = (String) ois.readObject();
                    aMain.paintFrame.shapeOperation(id);
                } else if (line.equals("size")) {
                    String id = (String) ois.readObject();
                    Dimension size = (Dimension) ois.readObject();
                    aMain.paintFrame.setCanvasSize(id, size);
                } else if (line.equals("shapeUpdate")) {
                    String id = (String) ois.readObject();
                    SimShape shape = (SimShape) ois.readObject();
                    int index = ois.read();
                    aMain.paintFrame.shapeOperation(id, shape, index);
                } else if (line.equals("shapeAdd")) {
                    String id = (String) ois.readObject();
                    SimShape shape = (SimShape) ois.readObject();
                    aMain.paintFrame.shapeOperation(id, shape);
                } else if (line.equals("shapeDelete")) {
                    String id = (String) ois.readObject();
                    int index = ois.read();
                    aMain.paintFrame.shapeOperation(id, index);
                } else if (line.equals("close")) {
                    break;
                }
                line = (String) ois.readObject();
            }
            ois.close();
            server.close();
        } catch (Exception e) {
        }
    }
}

⌨️ 快捷键说明

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