2019年12月12日 星期四

Week_14

1. 可以遠端連線的小畫家

只要在Clien端畫畫就會出現一模一樣的內容在Server端

 Server Code: 
import processing.net.*;

Server server; // 伺服端
Client client; // 客戶端

void setup() {
    size(300, 300);
    server = new Server(this, 7777); //Server port 號 7777
}

void draw() {
    Client temp = server.available();
    if (temp != null) {
        client = temp;
    }
    
    if (client != null) {
        String now = client.readString(); // 讀客戶端傳過來的四個座標
    
        if (now != null) {
            println(now);
            String[] loc = splitTokens(now); // 把接收到的字串斷開,拿到各個座標
            line(int(loc[0]), int(loc[1]),int(loc[2]), int(loc[3]));
        } 
    }
}
 Client Code: 
import processing.net.*;

Client client; // Client
void setup() {
    size(300, 300);
    client = new Client(this, "127.0.0.1", 7777); // 連線到Server
}

void draw() {
    if (mousePressed) {
        line(mouseX, mouseY, pmouseX, pmouseY);
    
        // 把滑鼠畫的座標傳到Sever去
        client.write(mouseX + " " + mouseY + " " + pmouseX + " " + pmouseY);
    }
}



2. 可以遠端連線並且擦掉的小畫家

只要在Clien端畫畫就會出現一模一樣的內容在Server端,並且按右鍵就可以擦掉

 Server Code: 
import processing.net.*;

Server server; // 伺服端
Client client; // 客戶端

ArrayList<PVector> points;

void setup() {
  size(300, 300);
  server = new Server(this, 7777); //Server port 號 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);
  }
  
  Client temp = server.available();
  if (temp != null) {
    client = temp;
  }
  
  if (client != null) {
    String now = client.readString(); // 讀客戶端傳過來的四個座標
    
    if (now != null) {
      println(now);
      if (now.equals("remove")) {
        if (points.size()>0) points.remove(points.size()-1);
        
      } else if (now.contains(" ")){
        
        String[] loc = splitTokens(now); // 把接收到的字串斷開,拿到各個座標
        points.add(new PVector(int(loc[0]), int(loc[1])));
      }
    } 
  }
}
 Client Code: 
import processing.net.*;

Client client; // Client
ArrayList<PVector> points; // 存圈圈的座標
String sentInfor = ""; // 要傳給Server的資料

void setup() {
  size(300, 300);
  client = new Client(this, "127.0.0.1", 7777); // 連線到Server
  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);
  }
  
  if (sentInfor != "") {
    client.write(sentInfor);
  }
}

void mouseDragged() {
  if (mouseButton == LEFT) {
    points.add(new PVector(mouseX, mouseY));
    sentInfor = mouseX + " " + mouseY;
  } 
  if (mouseButton == RIGHT && points.size()>0) {
    points.remove(points.size()-1);
    sentInfor = "remove";
  }
}

void mouseReleased() {
  sentInfor = "";
}

沒有留言:

張貼留言

alanhc 互動技術-week17 [final]

回顧這學期的作品:  期中作業:LANDING:PLANET 賣點&特點: 炫麗的特效 物理(星球重力及降落)及粒子系統(噴射) 世界地圖可根據視角縮放 困難點: 重寫3次最終改寫成物件導向的CLASS寫法...