d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Java Android Drawing Issue
Add Reply New Topic New Poll
Member
Posts: 10,083
Joined: Aug 19 2007
Gold: 0.00
May 10 2015 12:25pm
I have a client (android) and a server (computer).
Right now you can draw from the server and it will show the client, and the other way around.

My problem is the drawing function itself. I'm drawing by using lines, but right now there are holes between the lines and I need it fixed.
Need this done asap. Pm me if you're interested. Will pay.

This is what I'm sending from the server side:
Code
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);
}
}
}
});


Here's what I'm receiving on the client from the server:
Code
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;

}



Here's the function I made on the client that uses the data received from the server to create lines between the points:
Code
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();
}



It's supposed to draw a smooth path , creating lines between the points, but what I'm getting is holes between points. I followed a tutorial to make a simple drawing function using path and canvas,
which I also tried using to draw the lines from the server, but I just got the same result :/.

This is what is being displayed when I draw on the servers frame:


This post was edited by Polsenols on May 10 2015 12:55pm
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
May 10 2015 12:32pm
whatever you do, don't post your code or a screenshot of the problem.
Member
Posts: 10,083
Joined: Aug 19 2007
Gold: 0.00
May 10 2015 12:50pm
Quote (carteblanche @ May 10 2015 06:32pm)
whatever you do, don't post your code or a screenshot of the problem.


Sorry, It's a lot of code though, and I'm new to this sub-forum. Not sure what the standards were here.

I'll post the most important parts, sec.


Edit: continued:



I use this code to draw on the client, which is working fine and is creating a smooth path. I didn't know how to implement this to use the data from the object the server is sending.
Code
@Override
public boolean onTouchEvent(MotionEvent event) {

float touchX = event.getX();
float touchY = event.getY();

if(event.getAction() == MotionEvent.ACTION_DOWN){
drawPath.moveTo(touchX,touchY);

oldX = event.getX();
oldY = event.getY();
}

else if(event.getAction() == MotionEvent.ACTION_MOVE){
try {
currentX = event.getX();
currentY = event.getY();
drawPath.lineTo(touchX, touchY);
mouseSend = new MouseData((int)oldX,(int)oldY,(int)currentX,(int)currentY,false, false, false);
out.writeObject(mouseSend);
oldX = currentX;
oldY = currentY;
}
catch(IOException e){
e.printStackTrace();
}

}

else if(event.getAction() == MotionEvent.ACTION_UP){
drawCanvas.drawPath(drawPath,drawPaint);
drawPath.reset();

}
invalidate();
return true;
}



If I replace my draw functon with the following, it sort of works. It's a smooth path but now it's a continous line that never stops. I can't start a new one.
Code
public void drawPath() {

invalidate();

if(isDrawing=true) {
drawCanvas.drawLine(testx, testy, xNew, yNew, drawPaint);
testx = xNew;
testy = yNew;
invalidate();


System.out.println(xReceive + " " + yReceive + " " + xNew + " " + yNew);
}

invalidate();
}


Here's what it looks like if I replace the code with the above:


If anyone can help me out, I'd appreciate it a lot. I really have no clue what to do :/ I thought about using array list and connect the dots, but I have no clue how to do that

This post was edited by Polsenols on May 10 2015 01:07pm
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
May 10 2015 01:24pm
for starters, fix your condition:
Code
if(isDrawing=true) {

second, ignore whatever drawing tool you have on the server for the time being. manually send 2 points of data to the client and see if it draws a line. if not, it should be much easier to determine where the problem is.
Member
Posts: 10,083
Joined: Aug 19 2007
Gold: 0.00
May 10 2015 01:40pm
Quote (carteblanche @ May 10 2015 07:24pm)
for starters, fix your condition:
Code
if(isDrawing=true) {

second, ignore whatever drawing tool you have on the server for the time being. manually send 2 points of data to the client and see if it draws a line. if not, it should be much easier to determine where the problem is.



Sorry but I'm not sure what you mean with fix it. Do you mean remove it?

I sent an object with the points 50,50 and 150,150. The client drew the line with said coordinates.
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
May 10 2015 01:50pm
Quote (Polsenols @ May 10 2015 03:40pm)
Sorry but I'm not sure what you mean with fix it. Do you mean remove it?


it's not doing what you think it's doing. google the difference between assignment operator and conditional equals operator.

Quote
I sent an object with the points 50,50 and 150,150. The client drew the line with said coordinates.


draw a quick curve from the server and log the data you send to the client to see the gaps. then send just that data to the client without the drawing tool. presumably the client will look identical as before, including the gaps. now remove data points until you have the minimum data points to see a gap. then debug that.

Go Back To Programming & Development Topic List
Add Reply New Topic New Poll