gpt4 book ai didi

java - 画图应用程序无法发送到本地服务器

转载 作者:行者123 更新时间:2023-12-01 10:09:13 24 4
gpt4 key购买 nike

我正在为 CS 类编写一个 Paint 应用程序,但无法让它将数据发送到服务器。它应该连接到服务器,以便多个客户端可以一起处理同一幅画,但我似乎无法将新对象发送到服务器。我知道它连接到服务器,因为当建立连接但 ObjectOutputStream.writeObject 无法到达服务器时,它会在两端提供反馈。请让我知道我缺少什么!谢谢大家!

private ArrayList<PaintObject> shapes = new ArrayList<PaintObject>();
private Point startDrag, endDrag;
private ColorShapeSelectorJPanel colorShapeChooserArea = new ColorShapeSelectorJPanel();
private PaintObject currPaintObj = null;
private Socket socket;
private ObjectOutputStream oos;
private ObjectInputStream ois;

private static final String ADDRESS = "localhost";

public PaintingField() {
this.setBackground(Color.WHITE);
this.setSize(2000, 1400);
this.openConnection();
initializeListeners();

}

// Establish connection with the server.
private void openConnection() {
/* Our server is on our computer, but make sure to use the same port. */
try {
// Connect to the Server
socket = new Socket(ADDRESS, Server.SERVER_PORT);
oos = new ObjectOutputStream(socket.getOutputStream());
ois = new ObjectInputStream(socket.getInputStream());
System.out.println("Connected to server at " + ADDRESS + ":" + Server.SERVER_PORT);
} catch (IOException e) {
// e.printStackTrace();
this.cleanUpAndQuit("Couldn't connect to the server");
}

}

// Remove connection with server
private void cleanUpAndQuit(String string) {
JOptionPane.showMessageDialog(PaintingField.this, string);
try {
if (socket != null)
socket.close();
} catch (IOException e) {
// Couldn't close the socket, we are in deep trouble. Abandon ship.
e.printStackTrace();

}
}

// Get listeners running
private void initializeListeners() {
this.addMouseListener(new MouseAdapter() {
// Begin dragging the shape
public void mousePressed(MouseEvent evnt) {
startDrag = new Point(evnt.getX(), evnt.getY());
endDrag = startDrag;
repaint();
}

// When mouse is released, get the shape
@Override
public void mouseReleased(MouseEvent evnt) {

// If rectangle...
if (colorShapeChooserArea.isRectangleSelected()) {
currPaintObj = new PaintObject(makeRectangle(startDrag.x, startDrag.y, evnt.getX(), evnt.getY()),
colorShapeChooserArea.getColor(), false);
}

// If ellipse...
else if (colorShapeChooserArea.isEllipseSelected()) {
currPaintObj = new PaintObject(makeEllipse(startDrag.x, startDrag.y, evnt.getX(), evnt.getY()),
colorShapeChooserArea.getColor(), false);
}

// if line
else if (colorShapeChooserArea.isLineSelected()) {
currPaintObj = new PaintObject(makeLine(startDrag.x, startDrag.y, evnt.getX(), evnt.getY()),
colorShapeChooserArea.getColor(), false);
}

// if doge
else if (colorShapeChooserArea.isImageSelected()) {
currPaintObj = new PaintObject(makeRectangle(startDrag.x, startDrag.y, evnt.getX(), evnt.getY()),
Color.WHITE, true);
}

// Send the object to the server
// TODO: FIXME: oos.writeObject NOT SENDING!!!
try {
/* Someone pressed enter? Send the message to the server! */
oos.writeObject(currPaintObj);
} catch (IOException ex) {
PaintingField.this.cleanUpAndQuit("Couldn't send a message to the server");
}

shapes.add(currPaintObj);
startDrag = null;
endDrag = null;
repaint();

}

});

this.addMouseMotionListener(new MouseMotionAdapter() {
public void mouseDragged(MouseEvent evnt) {
endDrag = new Point(evnt.getX(), evnt.getY());
repaint();
}
});

}

问题行标记为“//TODO: FIXME:”,目标是在释放鼠标时通知并发送给服务器。

最佳答案

---- [已解决] ----

我遇到了一些问题:

首先:我在上面的代码来自的同一个类中创建了一个私有(private)ServerListener,但我没有创建一个它在构造函数中的实例。哑的。我知道。

其次:我发送到服务器的对象 (PaintObject) 不可序列化。 对象必须可序列化才能使用 ObjectOutputStream.writeObject 发送。公共(public) PaintObject 类现在开始:

public class PaintObject implements Serializable {

这些就是问题所在。我只是想当我破解它时我会成为一个好的互联网公民并回答我自己的问题。我希望这对 future 有所帮助。

关于java - 画图应用程序无法发送到本地服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36252868/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com