1.客戶端畫線,Server端可以收到且印出數字
程式碼:
Client端
import processing.net.*; Client client; void setup(){ size(300,300); client=new Client(this,"127.0.0.1",7777);//我們的Server也在localhost } void draw(){ if(mousePressed){ line(mouseX,mouseY,pmouseX,pmouseY); client.write(mouseX +" "+ mouseY+" "+ pmouseX +" "+pmouseY +"\n");//要送出4個數字 } }
Server端
import processing.net.*; Server server=null; Client client=null;//等一下要上門的client,只接受一位客人 void setup(){ size(300,300); server = new Server(this, 7777);//不用寫IP } void draw(){ Client temp = server.available();//開門看一下,有沒有人來阿?! if(temp != null){//有人上鉤了...連線成功 client = temp; } if(client != null){ String now = client.readString();//如果有送資料,會讀到4個座標 if(now!=null){print(now); } } }
2.客戶端畫線,Server端可以同步畫出一樣的東西
Client端
import processing.net.*; Client client; void setup(){ size(300,300); client=new Client(this,"127.0.0.1",7777);//我們的Server也在localhost } void draw(){ if(mousePressed){ line(mouseX,mouseY,pmouseX,pmouseY); client.write(mouseX +" "+ mouseY+" "+ pmouseX +" "+pmouseY +"\n");//要送出4個數字 } }
Server端
import processing.net.*; Server server=null; Client client=null;//等一下要上門的client,只接受一位客人 void setup(){ size(300,300); server = new Server(this, 7777);//不用寫IP } void draw(){ Client temp = server.available();//開門看一下,有沒有人來阿?! if(temp != null){//有人上鉤了...連線成功 client = temp; } if(client != null){ String now = client.readString();//如果有送資料,會讀到4個座標 if(now!=null){ String[]xyxy = splitTokens(now); line (int(xyxy[0]),int(xyxy[1]),int(xyxy[2]),int(xyxy[3])); } } }
3.左鍵可以畫圖,右鍵可以刪除
程式碼:
import processing.net.*;
Client client;
ArrayList<PVector> points;
void setup(){
size(300,300);
client=new Client(this,"127.0.0.1",7777);//我們的Server也在localhost
points=new ArrayList<PVector>();
}//要加上網路連線
void draw(){
background(255);
for (int i=0; i<points.size();i++){///方法1
ellipse(points.get(i).x,points.get(i).y,5,5);
}
}
void mouseDragged(){
if(mouseButton==LEFT) points.add(new PVector(mouseX,mouseY));
if(mouseButton==RIGHT) points.remove(0);
}




沒有留言:
張貼留言