客户端GUI在服务器发送消息时不更新

时间:2015-04-19 20:43:23

标签: java

我有一个接口,我的所有消息都实现了。当通过ObbjectOutputStream发送序列化消息时,消息似乎是基于println()语句传递的,但是客户端gui没有得到更新。程序有一个游戏循环线程,有一个无限循环来重新绘制程序。通过bufferedreader传递消息时,程序会更新。 谢谢你的帮助!

以下是邮件发送时的代码:

    @Override
    public void mouseReleased(MouseEvent e) {
        System.out.println("released");
        //released = true;
        world.getSerArrow().move();
        ArrowReleased arrowReleased = new ArrowReleased(world.getSerArrow());
        ObjectOutputStream output = server.getSocketThread().getOutput();
        try {
            output.writeObject(arrowReleased);
            output.flush();
        } catch (IOException e1) {
            e1.printStackTrace();
        }
    }

这是阅读它的代码:

    public class ClientThread extends Thread {

private Socket socket;
private BufferedReader reader;
private PrintWriter out;
private Person person;
private ServerBowAndArrow arrow;
private ObjectInputStream input;

public ClientThread(Socket socket, Person person, ServerBowAndArrow arrow) {
    this.socket = socket;
    this.person = person;
    this.arrow = arrow;
}

public PrintWriter getOut() {
    return out;
}

@Override
public void run() {

    InputStream in;
    try {
        in = socket.getInputStream();
        reader = new BufferedReader(new InputStreamReader(in));
        input = new ObjectInputStream(in);
        Messages msg = (Messages) input.readObject();
        msg.perform();

        out = new PrintWriter(socket.getOutputStream(), true);
        String inputLine;
        while ((inputLine = reader.readLine()) != null) {

            person.setX(Integer.valueOf(inputLine));

            person.setY(Integer.valueOf(reader.readLine()));

            arrow.setX(Integer.valueOf(reader.readLine()));
            arrow.setY(Integer.valueOf(reader.readLine()));
        }

    } catch (IOException | ClassNotFoundException e) {
        e.printStackTrace();
    }

}

}

这是应该发生的执行方法:

    @Override
public void perform() {
    System.out.println("hello");
    int x1 = arrow.getX1();
    int x2 = arrow.getX2();
    arrow.setX1(x1 + 100);
    arrow.setX2(x2 + 100);
}

0 个答案:

没有答案