0
我正在爲CS類寫一個塗料應用程序,我無法將它發送到服務器。它應該連接到一臺服務器,以便多個客戶端可以在同一張繪畫上一起工作,但我似乎無法將新對象發送到服務器。我知道它連接到服務器,因爲它在建立連接時在兩端提供反饋,但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:」我們的目標一直是通知並釋放鼠標時發送給服務器。