java—使用套接字将jframe图形从客户端发送到服务器

yuvru6vn  于 2021-07-06  发布在  Java
关注(0)|答案(1)|浏览(334)

我正试图发送我的java文件,其中有一个jframe绘图按钮,它可以将背景的颜色从客户机更改为服务器。服务器接收该绘图并打开它,但当我单击按钮时,没有任何更改。我做错什么了?由于某些原因,应用程序有时无法运行。
按图纸编码

package Drawings;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Rectangle2D;
import javax.swing.*;

public class DrawingTwo extends JFrame {

    public static final int DEFAULT_WIDTH = 300;
    public static final int DEFAULT_HEIGHT = 300;

    public Color tvColor;
    public Color smileColor;
    public int h;
    public int h2;
    public int h3;
    public int h4;
    public int h5;
    public int h6;
    public String l;
    public DrawComponent c;

    public static void main(String[] args) {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                new DrawingTwo();
            }
        });

    }

    public DrawingTwo() {
        super();
        setOneChanell();

        Container container = getContentPane();
        container.setBackground(new Color(242, 212, 252));
        container.setLayout(new BorderLayout(20, 20));
        container.setPreferredSize(new Dimension(DEFAULT_WIDTH, DEFAULT_HEIGHT));

        ButtonListener listener = new ButtonListener();

        JLabel lb = new JLabel(l);
        lb.setText(l);
        container.add(lb);
        JButton j2 = new JButton("2");
        j2.addActionListener(listener);
        container.add(j2, BorderLayout.NORTH);

        c = new DrawComponent();
        container.add(c, BorderLayout.CENTER);

        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        pack();
        setVisible(true);
    }

    public void setOneChanell() {
        this.tvColor = new Color(255, 153, 153);
        this.smileColor = new Color(255, 247, 10);
        this.h = 110;
        this.h2 = 55;
        this.h3 = 60;
        this.h4 = 60;
        this.h5 = 0;
        this.h6 = -180;
        this.l = "Канал для веселых";
    }

    public void setTwoChanell() {
        this.tvColor = new Color(172, 194, 157);
        this.smileColor = new Color(0, 161, 219);
        this.h = 115;
        this.h2 = 87;
        this.h3 = 50;
        this.h4 = 40;
        this.h5 = 0;
        this.h6 = +180;
        this.l = "Канал для грустных";
    }

    public class DrawComponent extends JComponent {

        @Override
        public void paintComponent(Graphics g) {
            super.paintComponent(g);
            g.setColor(Color.black);
            g.fillRect(37, 26, 210, 130);

            g.setColor(tvColor);
            g.fillRect(42, 30, 200, 120);

            g.setColor(Color.darkGray);
            g.fillRect(135, 156, 15, 20);

            g.setColor(Color.darkGray);
            g.fillRect(83, 170, 120, 13);

            g.setColor(smileColor);
            g.fillOval(100, 45, 80, 80);

            g.setColor(Color.BLACK);
            g.drawArc(120, 70, 10, 10, 0, 360);
            g.drawArc(150, 70, 10, 10, 0, 360);

            g.drawString(l, 83, 200);
            g.setColor(Color.BLACK);
            g.drawArc(h, h2, h3, h4, h5, h6);
        }

    }

    public class ButtonListener implements ActionListener {

        @Override
        public void actionPerformed(ActionEvent event) {
            JButton button = (JButton) event.getSource();
            if (button.getText().equals("2")) {
                setTwoChanell();
                button.setText("1");

            } else {
                setOneChanell();
                button.setText("2");
            }
            c.repaint();
        }

    }

}

这是客户端文件

package ClientToServer;

import java.io.*;

import java.net.Socket;

import Drawings.DrawingTwo;

public class Client {

    public static void main(String[] args) throws IOException {

        Socket socket = new Socket("localhost", 12345);
        OutputStream outputStream = socket.getOutputStream();
        ObjectOutputStream objectOutputStream = new ObjectOutputStream(outputStream);
        objectOutputStream.writeObject(new DrawingTwo());
        objectOutputStream.flush();
        objectOutputStream.close();
    }

}

这是服务器文件

package ClientToServer;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

import Drawings.DrawingTwo;

import javax.swing.*;

public class Server {

    public static void main(String[] args) throws IOException, ClassNotFoundException {

        ServerSocket serverSocket = new ServerSocket(12345);
        Socket client = serverSocket.accept();
        ObjectInputStream inputStream = new ObjectInputStream(client.getInputStream());

        DrawingTwo object = (DrawingTwo) inputStream.readObject();
        object.setVisible(true);
        object.setTitle("Server");
        object.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        client.close();
        inputStream.close();
        serverSocket.close();
    }
}
zwghvu4y

zwghvu4y1#

我建议你多读一点关于socket通信的知识。如果您有一个服务器套接字,它应该一直运行。在您的代码中,当您从客户机接收到数据后立即关闭套接字连接。
另一件事是;只有一种类型限制了通信:drawingtwo()类。在服务器端,您无法接收任何其他数据。
让我们一步一步地看看你的代码。
必须为通信目的定义新对象

import java.io.Serializable;

public class CommunicationObject implements Serializable{

    private DrawingTwo mDrawingTwo;
    private boolean mSmileyFace = true;
    private boolean mIsInitialConnection = true;

    public DrawingTwo getmDrawingTwo() {
        return mDrawingTwo;
    }
    public void setmDrawingTwo(DrawingTwo mDrawingTwo) {
        this.mDrawingTwo = mDrawingTwo;
    }
    public boolean ismSmileyFace() {
        return mSmileyFace;
    }
    public void setmSmileyFace(boolean mSmileyFace) {
        this.mSmileyFace = mSmileyFace;
    }
    public boolean ismIsInitialConnection() {
        return mIsInitialConnection;
    }
    public void setmIsInitialConnection(boolean mIsInitialConnection) {
        this.mIsInitialConnection = mIsInitialConnection;
    }   
}

这里有3个字段。
图纸2;你的实际布局是什么
布尔笑脸;指示按钮的情况
布尔初始连接;决定是初始化以便服务器初始化布局还是更改笑脸
你的服务器必须一直监听

import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.net.ServerSocket;
import java.net.Socket;

import javax.swing.JFrame;

public class Server {

    static boolean isRunning = true;

    public static void main(String[] args) throws IOException, ClassNotFoundException {
        ServerSocket serverSocket = new ServerSocket(12345);
        Socket client = null;
        ObjectInputStream inputStream = null;
        InputStream is = null;
        DrawingTwo drawingTwo = null;
        CommunicationObject communicationObject = null;

        while (isRunning) {
            client = serverSocket.accept();
            inputStream = new ObjectInputStream(client.getInputStream());

            is = client.getInputStream();

            communicationObject = (CommunicationObject) inputStream.readObject();

            if (communicationObject.ismIsInitialConnection()) {
                drawingTwo = (DrawingTwo) communicationObject.getmDrawingTwo();
                drawingTwo.setVisible(true);
                drawingTwo.setTitle("Server");
                drawingTwo.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            } else {                

                if (communicationObject.ismSmileyFace()) {
                    drawingTwo.setOneChanell();                     
                } else {
                    drawingTwo.setTwoChanell();                     
                }
                drawingTwo.c.repaint();

            }

        }
        client.close();
        inputStream.close();
        serverSocket.close();
    }
}

请注意,isrunning现在总是正确的。必须处理异常和其他情况才能停止或重新启动它。
在drawingtwo类中为jbutton创建一个getter

import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.WindowConstants;

import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.geom.Rectangle2D;
import java.beans.PropertyChangeListener;

public class DrawingTwo extends JFrame{

      public static final int DEFAULT_WIDTH = 300;
        public static final int DEFAULT_HEIGHT = 300;

        public Color tvColor;
        public Color smileColor;
        public int h;
        public int h2;
        public int h3;
        public int h4;
        public int h5;
        public int h6;
        public String l;
        public DrawComponent c;

        private JButton j2;

        public static void main(String[] args) {
            EventQueue.invokeLater(new Runnable() {
                @Override
                public void run() {
                    new DrawingTwo();
                }
            });

        }

        public DrawingTwo() {
            super();
            setOneChanell();

            Container container = getContentPane();
            container.setBackground(new Color(242, 212, 252));
            container.setLayout(new BorderLayout(20, 20));
            container.setPreferredSize(new Dimension(DEFAULT_WIDTH, DEFAULT_HEIGHT));

            ButtonListener listener = new ButtonListener();

            JLabel lb = new JLabel(l);
            lb.setText(l);
            container.add(lb);
            j2 = new JButton("2");
            j2.addActionListener(listener);
            container.add(j2, BorderLayout.NORTH);

            c = new DrawComponent();
            container.add(c, BorderLayout.CENTER);

            setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
            pack();
            setVisible(true);

        }

        public void setOneChanell() {
            this.tvColor = new Color(255, 153, 153);
            this.smileColor = new Color(255, 247, 10);
            this.h = 110;
            this.h2 = 55;
            this.h3 = 60;
            this.h4 = 60;
            this.h5 = 0;
            this.h6 = -180;
            this.l = "1";
        }

        public void setTwoChanell() {
            this.tvColor = new Color(172, 194, 157);
            this.smileColor = new Color(0, 161, 219);
            this.h = 115;
            this.h2 = 87;
            this.h3 = 50;
            this.h4 = 40;
            this.h5 = 0;
            this.h6 = +180;
            this.l = "2";
        }

        public JButton getJButton() {
            return j2;
        }

        public class DrawComponent extends JComponent {

            @Override
            public void paintComponent(Graphics g) {
                super.paintComponent(g);
                g.setColor(Color.black);
                g.fillRect(37, 26, 210, 130);

                g.setColor(tvColor);
                g.fillRect(42, 30, 200, 120);

                g.setColor(Color.darkGray);
                g.fillRect(135, 156, 15, 20);

                g.setColor(Color.darkGray);
                g.fillRect(83, 170, 120, 13);

                g.setColor(smileColor);
                g.fillOval(100, 45, 80, 80);

                g.setColor(Color.BLACK);
                g.drawArc(120, 70, 10, 10, 0, 360);
                g.drawArc(150, 70, 10, 10, 0, 360);

                g.drawString(l, 83, 200);
                g.setColor(Color.BLACK);
                g.drawArc(h, h2, h3, h4, h5, h6);
            }

        }

        public class ButtonListener implements ActionListener {

            @Override
            public void actionPerformed(ActionEvent event) {
                JButton button = (JButton) event.getSource();
                if (button.getText().equals("2")) {
                    setTwoChanell();
                    button.setText("1");

                } else {
                    setOneChanell();
                    button.setText("2");
                }
                c.repaint();
            }

        }
}

记住你的按钮有两个功能
更改您已经处理过的客户机上的布局
发送信号到你的服务器,这样它也可以改变它
最后是client类。

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.net.Socket;
import java.net.UnknownHostException;

import javax.swing.JButton;

public class Client {

    static OutputStream outputStream;
    static ObjectOutputStream objectOutputStream;
    static Socket socket;    

    public static void main(String[] args) throws UnknownHostException, IOException {

        DrawingTwo drawingTwo = new DrawingTwo();

        CommunicationObject communicationObject = new CommunicationObject();        
        communicationObject.setmDrawingTwo(drawingTwo);
        communicationObject.setmIsInitialConnection(true);

        write(communicationObject);

        JButton button = drawingTwo.getJButton();

        button.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent arg0) {

                try {
                    if (button.getText().equals("2")) {

                        communicationObject.setmIsInitialConnection(false);
                        communicationObject.setmSmileyFace(false);
                        write(communicationObject);
                    } else {
                        communicationObject.setmIsInitialConnection(false);
                        communicationObject.setmSmileyFace(true);
                        write(communicationObject);
                    }
                } catch (Exception e) {
                    // TODO: handle exception
                }
            }
        });
    }

    public static void write(CommunicationObject comObject) throws IOException
    {       
        socket = new Socket("localhost", 12345);

        outputStream = socket.getOutputStream();
        objectOutputStream = new ObjectOutputStream(outputStream);

        objectOutputStream.writeObject(comObject);

        objectOutputStream.flush();     
        objectOutputStream.close();
    }   
}

在这里创建一个communicationobject,并将drawingtwo类指定给它。
看看从哪里将jbutton从drawingtwo提取到客户机类中,这样就可以将actionlistener绑定到它。通过这个动作监听器,您现在可以与服务器通信了。
注意,由于客户机和服务器都可以访问drawingtwo,所以不需要通过socket发送整个类。只需向服务器发送一条消息,它应该自己创建一个示例。

相关问题