我正在为CS类编写一个Paint App,我无法将它发送到服务器。它应该连接到服务器,因此多个客户端可以一起处理同一个绘图,但我似乎无法将新对象发送到服务器。我知道它连接到服务器,因为它在建立连接时提供了两端的反馈但是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();
}
});
}
问题行标有&#34; // TODO:FIXME:&#34;目标是在释放鼠标时通知并发送给服务器。
答案 0 :(得分:1)
---- [已解决] ----
我遇到了一些问题:
首先:我在同一个类中创建了一个私有 ServerListener 类,上面的代码是来自但我还没有在构造函数中创建了它的一个实例。哑。我知道。
其次:我发送到服务器的对象( PaintObject )不可序列化。 对象必须可序列化才能使用ObjectOutputStream.writeObject发送。现在公共PaintObject类开始了:
public class PaintObject implements Serializable {
那些是问题。我只是以为我会成为一名优秀的互联网公民,并在我破解它时回答我自己的问题。我希望这对未来有所帮助。