Processingでカーソルが図形に重なると色が変わるプログラムで、1つ目の図形と2つ目の図形の色を変えようとしています。
以下のコードではどちらも図形2の色になってしまって困っています。
Processing
1// Use the included processing code serial library 2import processing.serial.*; 3 4int spos=90; 5int wpos=90; 6 7Serial port; // The serial port 8ArrayList<MyShape> shapes = new ArrayList<MyShape>(); 9 10void setup() { 11 size(720, 720); 12 background(125); 13 frameRate(100); 14 15 println(Serial.list()); // List COM-ports 16 17 //select second com-port from the list 18 port = new Serial(this, Serial.list()[1], 9600); 19 20 //1 21 MyShape ichi; 22 ichi = new MyRect(100, 150, 32, 200); 23 shapes.add(ichi); 24 25 //2 26 MyShape ni; 27 ni = new MyRect(200, 200, 32, 200); 28 shapes.add(ni); 29 ni = new MyRect(300, 200, 32, 200); 30 shapes.add(ni); 31 32 33} 34 35void draw() 36{ 37 update(mouseX, mouseY); 38 rect (310, 310, 100, 100); 39 line(360, 0, 360, 720); 40 line(0, 360, 720, 360); 41 42 //音と形 43 background(102); 44 for (MyShape ichi : shapes) { 45 if (ichi.contains(mouseX, mouseY)) { 46 fill(#CE2828); 47 port.write('a'); 48 } else { 49 fill(204); 50 port.write('b'); 51 } 52 ichi.draw(); 53 } 54 55 for (MyShape ni : shapes) { 56 if (ni.contains(mouseX, mouseY)) { 57 fill(#1318F0); 58 port.write('a'); 59 } else { 60 fill(204); 61 port.write('b'); 62 } 63 ni.draw(); 64 } 65} 66 67void update(int x, int y) 68{ 69 //Calculate servo1 postion from mouseX 70 spos= x/4; 71 72 //Output the servo1 position ( from 0 to 180) 73 port.write("s"+spos); 74 75 //Calculate servo2 postion from mouseY 76 wpos= (y/4)-10; 77 78 //Output the servo2 position ( from 0 to 180) 79 port.write("w"+wpos); 80} 81 82// 長方形を表すクラス 83class MyRect extends MyShape { 84 float x, y, w, h; 85 86 MyRect(float x, float y, float w, float h) { 87 this.x = x; 88 this.y = y; 89 this.w = w; 90 this.h = h; 91 } 92 93 void draw(){ 94 rect(x, y, w, h); 95 } 96 97 boolean contains(float mx, float my) { 98 return mx >= x && mx <= (x + w) && my >= y && my <= (y + h); 99 } 100} 101 102// 任意の図形を表す抽象クラス 103abstract class MyShape { 104 // 画面上へこの図形を描画するメソッド 105 abstract void draw(); 106 107 // 指定位置がこの図形に含まれるかどうか判定するメソッド 108 abstract boolean contains(float x, float y); 109} 110 111
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2017/07/07 14:47