My aim is to have a picture drawn on one device to be duplicated onto another, I have managed to send the coordinates of the path to a networking thread but am unable to handle these on the second device. how would i draw a path on the second device using coordinates from the stream in as real time as possible. Currently i have a threaded connection:
public class connecting implements Runnable{
Socket sock;
ObjectInputStream ois;
@Override
public void run() {
try{
sock = new Socket("10.42.34.46", 1337);
InputStream is = sock.getInputStream();
ois = new ObjectInputStream(new BufferedInputStream(is));
}catch(IOException ex){
ex.printStackTrace();
}
while(true){
com.DrawTastic.Drawring serverDraw = null;
serverDraw = (com.DrawTastic.Drawring) ois.readObject();
float mX = serverDraw.getMx();
float mY = serverDraw.getMy();
}
}
}
how would i get the float variables to continuously draw into this onDraw method in another thread
@Override
public void onDraw(Canvas canvas) {
connecting ncoords = new connecting();
mPath.lineTo(mX, mY);
canvas.drawPath(mPath, mPaint);
canvas.drawColor(Color.WHITE);
canvas.drawBitmap(mBitmap, 0, 0, mBitmapPaint);
canvas.drawPath(mPath, mPaint);
}
}
This is the most basic tutorial about client/server programming in Java. Let’s start with naming
The client implements
onTouchEvent(MotionEvent)and writes x and y coordinates to its socket’sOutputStream. Even if you want it as realtime as possible, you may need to store the events (x, y and timestamp) locally and transmit them at certain time intervals. Like every other blocking operation, this will run on a separate thread than the main application thread. This is a pseudo implementationThe server will have a
ServerSocketlistening for events. Every time an event is received it will draw. Note that in order to access the drawing thread from the background thread which the socket is listening on, you may use aHandler. A Handler is simply a way to run some code on a specific thread. You only need to declare aHandlerinstance variable for yourServerActivityand instantiate it inonCreate: this way theHandlerwill be bound to the application main thread, which is also the UI thread.The last part is how to remember already drawn things, both on the client and on the server side. This is because each call to
onDrawwill give you a clean bitmap, so you must either store each event in a list and draw from that list, or you can keep a cache of already drawn things (see this paste)