client程式碼:
import processing.net.*;
Client client;
void setup() {
size(300, 300);
client = new Client(this, "127.0.0.1", 7777);
}
void draw() {
if (mousePressed) {
line(mouseX, mouseY, mouseX, mouseY);
client.write(mouseX+" "+mouseY+" "+pmouseX+" "+pmouseY+"\n");
}
}
server程式碼:
import processing.net.*;
Server server=null;
Client client=null;
void setup() {
size(300, 300);
server = new Server(this, 7777);
}
void draw() {
Client temp = server.available();
if (temp != null) {
client = temp;
}
if (client != null) {
String now = client.readString();
if (now!=null) print(now);
}
}
讓client端可以連線到server端
server更新程式碼:
Server server=null;
Client client=null;
void setup() {
size(300, 300);
server = new Server(this, 7777);
}
void draw() {
Client temp = server.available();
if (temp != null) {
client = temp;
}
if (client != null) {
String now = client.readString();
if (now!=null) {
String []xyxy=splitTokens(now);
line(int(xyxy[0]), int(xyxy[1]), int(xyxy[2]), int(xyxy[3]));
}
}
}
按滑鼠左鍵畫畫 右鍵退回
client更新程式碼:
Client client;
ArrayList<PVector> points;
void setup() {
size(300, 300);
client = new Client(this, "127.0.0.1", 7777);
points=new ArrayList<PVector>();
}
void draw() {
background(255);
for (int i=0; i<points.size(); i++) {
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.size()>0) points.remove(points.size()-1);
}



沒有留言:
張貼留言