2019年12月12日 星期四

呼嚕嚕的p語言日記-week14

1.小畫家畫東西


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,pmouseX,pmouseY);
    client.write(mouseX+" "+mouseY+" "+pmouseX+" "+pmouseY+"\n");

  }


}



sever端


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);

  }



}
即可傳送小畫家的座標



2.若要改成client 畫的東西 server端馬上接收的話


server 端改成


import processing.net.*;
Server server=null;
Client client=null;
void setup(){
  size(300,300);
  server = new Server(this ,7777);

}
int x=0,y=0,px=0,py=0;
void draw(){
  Client temp= server.available();
  if(temp != null){
  
   client=temp;  
  
  }
  if(client != null){
    String now = client.readString();
    if(now!=null){
   
    String[] xy= splitTokens(now);
    x=int(xy[0]);y=int(xy[1]);
    px=int (xy[2]); py=int (xy[3]);
    line(x,y,px,py);
    }
    
  }



}














3.若想刪除之前畫線內容



import processing.net.*;
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);


}

沒有留言:

張貼留言

alanhc 互動技術-week17 [final]

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