问题
Ok, so I have a server (on computer) and the client (android phone).
I have set up a connection via socket, and I can draw on the server frame, and then show it on the clients screen.
This is what I'm sending:
addMouseListener(new MouseAdapter() {
public void mousePressed(MouseEvent e) {
// save coord x,y when mouse is pressed
startDrawing=true;
oldX = e.getX();
oldY = e.getY();
try {
if (s != null) {
mouseSend = new MouseData(oldX, oldY, currentX, currentY, startDrawing, isDrawing, endDrawing);
out.writeObject(mouseSend);
}
}
catch (IOException ex){
ex.printStackTrace();
}
}
});
addMouseMotionListener(new MouseMotionAdapter() {
public void mouseDragged(MouseEvent e) {
// coord x,y when drag mouse
currentX = e.getX();
currentY = e.getY();
isDrawing = true;
startDrawing=false;
endDrawing=false;
if (g2 != null) {
try {
// draw line if g2 context not null
g2.drawLine(oldX, oldY, currentX, currentY);
if(s!=null) {
mouseSend = new MouseData(oldX, oldY, currentX, currentY, startDrawing, isDrawing, endDrawing);
out.writeObject(mouseSend);
}
// refresh draw area to repaint
repaint();
// store current coords x,y as olds x,y
oldX = currentX;
oldY = currentY;
}
catch (IOException ex) {
Logger.getLogger(Server.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
});
And here's what I receive on the client, and use that data to draw lines:
socket = new Socket("192.168.20.7",8080);
out = new ObjectOutputStream(socket.getOutputStream());
in = new ObjectInputStream(socket.getInputStream());
while(in!=null) {
mouseReceive = (MouseData) in.readObject();
xReceive = mouseReceive.mouseX;
yReceive = mouseReceive.mouseY;
xNew = mouseReceive._nowX;
yNew = mouseReceive._nowY;
startDrawing = mouseReceive.startDrawing;
isDrawing = mouseReceive.isDrawing;
endDrawing = mouseReceive.endDrawing;
}
And the draw function:
public void drawPath() {
invalidate();
if(startDrawing=true){
testx=xReceive;
testy=yReceive;
}
if(isDrawing=true) {
drawCanvas.drawLine(testx, testy, xNew, yNew, drawPaint);
invalidate();
System.out.println(xReceive + " " + yReceive + " " + xNew + " " + yNew);
}
invalidate();
}
Now comes the problem. I want it to draw a path wherever the person on the server is drawing, and I'm trying to do it using lines. But right now it's creating dots and not connecting them properly. I've tried so many things, also tried using Path on canvas, but I couldn't get anything to work :(
It's starting to get frustrating so I thought I would go here to search for help.
I thought about connecting them via an array list and creating lines between the points, but I have no idea where to start :/
Please if someone could help me I'd appreciate it so much!
Here's a picture of what it looks like:

来源:https://stackoverflow.com/questions/30155357/drawing-with-lines-function-in-android-using-java