# arduino 20221201 # mousePressed 滑鼠按下的範例 ``` void setup() { size(400, 400); } void draw() { line(0, 0, random(width), random(height)); } void mousePressed() { stroke(random(255), random(255), random(255)); } ``` # server 收指令 red green ``` import processing.net.*; boolean myServerRunning = true; Server myServer; void setup() { size(400, 400); textFont(createFont("SanSerif", 16)); myServer = new Server(this, 10002); } void draw() { text("server", 15, 45); Client thisClient = myServer.available(); if (thisClient != null) { if (thisClient.available() > 0) { background(0); String data = thisClient.readString(); if (data.indexOf("red")>0) { background(255, 0, 0); } if (data.indexOf("green")>0) { background(0, 255, 0); } text("mesage from: " + thisClient.ip() + " : " + data, 15, 60); thisClient.stop(); } } } ``` # client 發送指令 滑鼠左鍵 red / 滑鼠右鍵green ``` import processing.net.*; Client c; String data; void setup() { size(200, 200); } void mousePressed() { if (mouseButton == LEFT) { background(255, 0, 0); c = new Client(this, "localhost", 10002); // Connect to server on port 80 c.write("GET /red HTTP/1.0\r\n"); // Use the HTTP "GET" command to ask for a Web page c.write("\r\n"); } if (mouseButton == RIGHT) { background(0, 255, 0); c = new Client(this, "localhost", 10002); // Connect to server on port 80 c.write("GET /green HTTP/1.0\r\n"); // Use the HTTP "GET" command to ask for a Web page c.write("\r\n"); } } void draw() { //if (c.available() > 0) { // If there's incoming data from the client... // data = c.readString(); // ...then grab it and print it // println(data); //} } ``` # 滑鼠亂跑 ``` import java.awt.Robot; Robot robot; void setup(){ try { robot = new Robot(); robot.setAutoDelay(0); } catch (Exception e) { e.printStackTrace(); } } void draw(){ robot.mouseMove((int)random(600), (int)random(600)); } ``` # ngrok http 10002 ![](https://i.imgur.com/FUwhZ8Q.png) # server red green mouseRun mouseStop code ``` import java.awt.Robot; Robot robot; import processing.net.*; boolean myServerRunning = true; Server myServer; boolean mouseRun = false; void setup() { size(400, 400); textFont(createFont("SanSerif", 16)); myServer = new Server(this, 10002); try { robot = new Robot(); robot.setAutoDelay(0); } catch (Exception e) { e.printStackTrace(); } } void draw() { text("server", 15, 45); Client thisClient = myServer.available(); if (thisClient != null) { if (thisClient.available() > 0) { background(0); String data = thisClient.readString(); if (data.indexOf("red")>0) { background(255, 0, 0); } if (data.indexOf("green")>0) { background(0, 255, 0); } if (data.indexOf("mouseRun")>0) { mouseRun = true; } if (data.indexOf("mouseStop")>0) { mouseRun = false; } text("mesage from: " + thisClient.ip() + " : " + data, 15, 60); thisClient.stop(); } } if (mouseRun) { robot.mouseMove((int)random(600), (int)random(600)); } } ```