質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.48%
button

HTMLで用いる<button>タグです。

Processing

Processingは、オープンソースプロジェクトによるCGのためのプログラミング言語です。Javaをベースにしており、グラフィック機能に特化しています。イメージの生成やアニメーションなど、視覚的なフィードバックを簡単に得ることが可能です。

Q&A

解決済

1回答

6712閲覧

Processing cp5でボタンが押されたときにすべてのボタンを消したいです。

ATMEGA

総合スコア12

button

HTMLで用いる<button>タグです。

Processing

Processingは、オープンソースプロジェクトによるCGのためのプログラミング言語です。Javaをベースにしており、グラフィック機能に特化しています。イメージの生成やアニメーションなど、視覚的なフィードバックを簡単に得ることが可能です。

0グッド

0クリップ

投稿2018/01/29 05:20

Processsing で57個のボタンをfor分で回して作成しました。
作成はでき、ボタン自体もしっかりと機能しています。

そこで、一つのボタンを押したときに全てのボタンを消して、背景に画像を表示しようと思い以下のコードを書きました。

Processing

1import controlP5.*; 2 3ControlP5 cp5; 4 5String ikkai = "ikkai.txt"; 6String ikkaiData[]; 7String nikai = "nikai.txt"; 8String nikaiData[]; 9String sankai = "sankai.txt"; 10String sankaiData[]; 11String yonkai = "yonkai.txt"; 12String yonkaiData[]; 13int x=0; 14PImage img1; 15PImage img2; 16PImage img3; 17PImage img4; 18 19void setup(){ 20 img1 = loadImage("ikkai.png"); 21 img2 = loadImage("nikai.png"); 22 img3 = loadImage("sankai.png"); 23 img4 = loadImage("yonkai.png"); 24 size(1253,915); 25 26 cp5 = new ControlP5(this); 27 ControlFont fo = new ControlFont(createFont("MS Gothic", 18)); 28 29 PFont font = createFont("MS Gothic",18); 30 textFont(font); 31 32 ikkaiData = loadStrings(ikkai); 33 if(ikkaiData == null){ 34 println(ikkai + "読み込み失敗"); 35 exit(); 36 } 37 nikaiData = loadStrings(nikai); 38 if(nikaiData == null){ 39 println(nikai + "読み込み失敗"); 40 exit(); 41 } 42 sankaiData = loadStrings(sankai); 43 if(sankaiData == null){ 44 println(sankai + "読み込み失敗"); 45 exit(); 46 } 47 yonkaiData = loadStrings(yonkai); 48 if(yonkaiData == null){ 49 println(yonkai + "読み込み失敗"); 50 exit(); 51 } 52 53 54 text("一階",30,30); 55 int x=30; 56 for(int i=0; i<5;i++){ 57 createButton("b" + i,ikkaiData[i],x,40,fo); 58 x = x+180; 59 } 60 x=30; 61 for(int i=5; i<10;i++){ 62 createButton("b" + i,ikkaiData[i],x,100,fo); 63 x = x+180; 64 } 65 x=30; 66 for(int i=10; i<16;i++){ 67 createButton("b" + i,ikkaiData[i],x,160,fo); 68 x = x+180; 69 } 70 71 text("二階",30,240); 72 x=30; 73 for(int i=0; i<5;i++){ 74 createButton("b" + (i+16),nikaiData[i],x,250,fo); 75 x = x+180; 76 } 77 x=30; 78 for(int i=5; i<10;i++){ 79 createButton("b" + (i+16),nikaiData[i],x,310,fo); 80 x = x+180; 81 } 82 x=30; 83 for(int i=10; i<16;i++){ 84 createButton("b" + (i+16),nikaiData[i],x,370,fo); 85 x = x+180; 86 } 87 88 text("三階",30,450); 89 x=30; 90 for(int i=0; i<5;i++){ 91 createButton("b" + (i+32),sankaiData[i],x,460,fo); 92 x = x+180; 93 } 94 x=30; 95 for(int i=5; i<10;i++){ 96 createButton("b" + (i+32),sankaiData[i],x,520,fo); 97 x = x+180; 98 } 99 x=30; 100 for(int i=10; i<16;i++){ 101 createButton("b" + (i+32),sankaiData[i],x,580,fo); 102 x = x+180; 103 } 104 105 text("四階",30,660); 106 x=30; 107 for(int i=0; i<5;i++){ 108 createButton("b" + (i+48),yonkaiData[i],x,670,fo); 109 x = x+180; 110 } 111 x=30; 112 for(int i=5; i<9;i++){ 113 createButton("b" + (i+48),yonkaiData[i],x,730,fo); 114 x = x+180; 115 } 116} 117 118void draw(){ 119} 120 121//button 122void b0(){ 123 background(img1); 124 for(int i = 0; i<57; i++){ 125 cp5.getController("b" + i).remove(); 126 } 127} 128void b1(){ 129 background(img1); 130 for(int i = 0; i<57; i++){ 131 cp5.getController("b" + i).remove(); 132 } 133} 134void b2(){ 135 background(img1); 136 for(int i = 0; i<57; i++){ 137 cp5.getController("b" + i).remove(); 138 } 139} 140void b3(){ 141 background(img1); 142 for(int i = 0; i<57; i++){ 143 cp5.getController("b" + i).remove(); 144 } 145} 146void b4(){ 147 background(img1); 148 for(int i = 0; i<57; i++){ 149 cp5.getController("b" + i).remove(); 150 } 151} 152void b5(){ 153 background(img1); 154 for(int i = 0; i<57; i++){ 155 cp5.getController("b" + i).remove(); 156 } 157} 158void b6(){ 159 background(img1); 160 for(int i = 0; i<57; i++){ 161 cp5.getController("b" + i).remove(); 162 } 163} 164void b7(){ 165 background(img1); 166 for(int i = 0; i<57; i++){ 167 cp5.getController("b" + i).remove(); 168 } 169} 170void b8(){ 171 background(img1); 172 for(int i = 0; i<57; i++){ 173 cp5.getController("b" + i).remove(); 174 } 175} 176void b9(){ 177 background(img1); 178 for(int i = 0; i<57; i++){ 179 cp5.getController("b" + i).remove(); 180 } 181} 182void b10(){ 183 background(img1); 184 for(int i = 0; i<57; i++){ 185 cp5.getController("b" + i).remove(); 186 } 187} 188void b11(){ 189 background(img1); 190 for(int i = 0; i<57; i++){ 191 cp5.getController("b" + i).remove(); 192 } 193} 194void b12(){ 195 background(img1); 196 for(int i = 0; i<57; i++){ 197 cp5.getController("b" + i).remove(); 198 } 199} 200void b13(){ 201 background(img1); 202 for(int i = 0; i<57; i++){ 203 cp5.getController("b" + i).remove(); 204 } 205} 206void b14(){ 207 background(img1); 208 for(int i = 0; i<57; i++){ 209 cp5.getController("b" + i).remove(); 210 } 211} 212void b15(){ 213 background(img1); 214 for(int i = 0; i<57; i++){ 215 cp5.getController("b" + i).remove(); 216 } 217} 218void b16(){; 219 background(img1); 220 for(int i = 0; i<57; i++){ 221 cp5.getController("b" + i).remove(); 222 } 223} 224void b17(){ 225 background(img1); 226 for(int i = 0; i<57; i++){ 227 cp5.getController("b" + i).remove(); 228 } 229} 230void b18(){ 231 background(img1); 232 for(int i = 0; i<57; i++){ 233 cp5.getController("b" + i).remove(); 234 } 235} 236void b19(){ 237 background(img1); 238 for(int i = 0; i<57; i++){ 239 cp5.getController("b" + i).remove(); 240 } 241} 242void b20(){ 243 background(img1); 244 for(int i = 0; i<57; i++){ 245 cp5.getController("b" + i).remove(); 246 } 247} 248void b21(){ 249 background(img1); 250 for(int i = 0; i<57; i++){ 251 cp5.getController("b" + i).remove(); 252 } 253} 254void b22(){ 255 background(img1); 256 for(int i = 0; i<57; i++){ 257 cp5.getController("b" + i).remove(); 258 } 259} 260void b23(){ 261 background(img1); 262 for(int i = 0; i<57; i++){ 263 cp5.getController("b" + i).remove(); 264 } 265} 266void b24(){ 267 background(img1); 268 for(int i = 0; i<57; i++){ 269 cp5.getController("b" + i).remove(); 270 } 271} 272void b25(){ 273 background(img1); 274 for(int i = 0; i<57; i++){ 275 cp5.getController("b" + i).remove(); 276 } 277} 278void b26(){ 279 background(img1); 280 for(int i = 0; i<57; i++){ 281 cp5.getController("b" + i).remove(); 282 } 283} 284void b27(){ 285 background(img1); 286 for(int i = 0; i<57; i++){ 287 cp5.getController("b" + i).remove(); 288 } 289} 290void b28(){ 291 background(img1); 292 for(int i = 0; i<57; i++){ 293 cp5.getController("b" + i).remove(); 294 } 295} 296void b29(){ 297 background(img1); 298 for(int i = 0; i<57; i++){ 299 cp5.getController("b" + i).remove(); 300 } 301} 302void b30(){ 303 background(img1); 304 for(int i = 0; i<57; i++){ 305 cp5.getController("b" + i).remove(); 306 } 307} 308void b31(){ 309 background(img1); 310 for(int i = 0; i<57; i++){ 311 cp5.getController("b" + i).remove(); 312 } 313} 314void b32(){ 315 background(img1); 316 for(int i = 0; i<57; i++){ 317 cp5.getController("b" + i).remove(); 318 } 319} 320void b33(){ 321 background(img1); 322 for(int i = 0; i<57; i++){ 323 cp5.getController("b" + i).remove(); 324 } 325} 326void b34(){ 327 background(img1); 328 for(int i = 0; i<57; i++){ 329 cp5.getController("b" + i).remove(); 330 } 331} 332void b35(){ 333 background(img1); 334 for(int i = 0; i<57; i++){ 335 cp5.getController("b" + i).remove(); 336 } 337} 338void b36(){ 339 background(img1); 340 for(int i = 0; i<57; i++){ 341 cp5.getController("b" + i).remove(); 342 } 343} 344void b37(){ 345 background(img1); 346 for(int i = 0; i<57; i++){ 347 cp5.getController("b" + i).remove(); 348 } 349} 350void b38(){ 351 background(img1); 352 for(int i = 0; i<57; i++){ 353 cp5.getController("b" + i).remove(); 354 } 355} 356void b39(){ 357 background(img1); 358 for(int i = 0; i<57; i++){ 359 cp5.getController("b" + i).remove(); 360 } 361} 362void b40(){ 363 background(img1); 364 for(int i = 0; i<57; i++){ 365 cp5.getController("b" + i).remove(); 366 } 367} 368void b41(){ 369 background(img1); 370 for(int i = 0; i<57; i++){ 371 cp5.getController("b" + i).remove(); 372 } 373} 374void b42(){ 375 background(img1); 376 for(int i = 0; i<57; i++){ 377 cp5.getController("b" + i).remove(); 378 } 379} 380void b43(){ 381 background(img1); 382 for(int i = 0; i<57; i++){ 383 cp5.getController("b" + i).remove(); 384 } 385} 386void b44(){ 387 background(img1); 388 for(int i = 0; i<57; i++){ 389 cp5.getController("b" + i).remove(); 390 } 391} 392void b45(){ 393 background(img1); 394 for(int i = 0; i<57; i++){ 395 cp5.getController("b" + i).remove(); 396 } 397} 398void b46(){ 399 background(img1); 400 for(int i = 0; i<57; i++){ 401 cp5.getController("b" + i).remove(); 402 } 403} 404void b47(){ 405 background(img1); 406 for(int i = 0; i<57; i++){ 407 cp5.getController("b" + i).remove(); 408 } 409} 410void b48(){ 411 background(img1); 412 for(int i = 0; i<57; i++){ 413 cp5.getController("b" + i).remove(); 414 } 415} 416void b49(){ 417 background(img1); 418 for(int i = 0; i<57; i++){ 419 cp5.getController("b" + i).remove(); 420 } 421} 422void b50(){ 423 background(img1); 424 for(int i = 0; i<57; i++){ 425 cp5.getController("b" + i).remove(); 426 } 427} 428void b51(){ 429 background(img1); 430 for(int i = 0; i<57; i++){ 431 cp5.getController("b" + i).remove(); 432 } 433} 434void b52(){ 435 background(img1); 436 for(int i = 0; i<57; i++){ 437 cp5.getController("b" + i).remove(); 438 } 439} 440void b53(){ 441 background(img1); 442 for(int i = 0; i<57; i++){ 443 cp5.getController("b" + i).remove(); 444 } 445} 446void b54(){ 447 background(img1); 448 for(int i = 0; i<57; i++){ 449 cp5.getController("b" + i).remove(); 450 } 451} 452void b55(){ 453 background(img1); 454 for(int i = 0; i<57; i++){ 455 cp5.getController("b" + i).remove(); 456 } 457} 458void b56(){ 459 background(img1); 460 for(int i = 0; i<57; i++){ 461 cp5.getController("b" + i).remove(); 462 } 463} 464 465 466Button createButton(String theName, String theLabel, int theX, int theY, ControlFont theFont) { 467 Button b = cp5.addButton(theName) 468 .setLabel(theLabel) 469 .setPosition(theX,theY) 470 .setSize(170,50); 471 b.getCaptionLabel().setFont(theFont); 472 return b; 473}

"b0"をクリックした時には全てのボタンが消えるのですが、"b1"以降のボタンをクリックすると" ArrayIndexOutOfBoundsException: Arra index out of range: 0" とエラーが出ます。

試しに"b1()"のfor文をi<56にするとエラーはでなくなりますが、"b56"のボタンは消えません。

なぜこのようなことになるのか、また解決策を教えて頂きたいです。

※ikkai.txtなどのtxtファイルにはボタンに表示するためのテキストが入っています。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

提示しておられるコードはいささか長いので、若干イベントハンドリングコードを変更しつつより短いコードで動かしてみますと・・・

Java

1import controlP5.*; 2 3ControlP5 cp5; 4 5String ikkai = "ikkai.txt"; 6String ikkaiData[]; 7PImage img1; 8 9void setup(){ 10 img1 = loadImage("ikkai.png"); 11 size(360, 200); 12 13 cp5 = new ControlP5(this); 14 ControlFont fo = new ControlFont(createFont("MS Gothic", 18)); 15 16 PFont font = createFont("MS Gothic", 18); 17 textFont(font); 18 19 ikkaiData = loadStrings(ikkai); 20 21 text("一階",30,30); 22 int x = 30; 23 for (int i = 0; i < 5; i++) { 24 createButton("b" + i, ikkaiData[i], x, 40, fo); 25 x += 60; 26 } 27} 28 29Button createButton(String theName, String theLabel, int theX, int theY, ControlFont theFont) { 30 Button b = cp5.addButton(theName) 31 .setLabel(theLabel) 32 .setPosition(theX,theY) 33 .setSize(60,50); 34 b.getCaptionLabel().setFont(theFont); 35 return b; 36} 37 38void removeAllButtonsAndDrawImage() { 39 image(img1, 0, 0); 40 for (int i = 0; i < 5; i++) { 41 Controller c = cp5.getController("b" + i); 42 // 以下のif文はデバッグのためにやってみただけで意味は特にありません。 43 if (c != null) { 44 c.remove(); 45 } else { 46 println("b" + i + " does not exist"); 47 } 48 } 49} 50 51void draw(){ 52} 53 54// 全てのControlP5のコントロールのイベントを受けるハンドラー(なのかな?) 55void controlEvent(ControlEvent event) { 56 String controlName = event.getController().getName(); 57 println("event fired from " + controlName); 58 removeAllButtonsAndDrawImage(); 59}

text

1ControlP5 2.2.6 infos, comments, questions at http://www.sojamo.de/libraries/controlP5 2event fired from b1 3java.lang.ArrayIndexOutOfBoundsException: Array index out of range: 0 4 at java.util.Vector.get(Vector.java:748) 5 at controlP5.ControllerList.get(Unknown Source) 6 at controlP5.ControllerGroup.setMousePressed(Unknown Source) 7 at controlP5.ControlWindow.mouseReleasedEvent(Unknown Source) 8 at controlP5.ControlWindow.mouseEvent(Unknown Source) 9 at sun.reflect.GeneratedMethodAccessor4.invoke(Unknown Source) 10 at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) 11 at java.lang.reflect.Method.invoke(Method.java:498) 12 at processing.core.PApplet$RegisteredMethods.handle(PApplet.java:1429) 13 at processing.core.PApplet.handleMethods(PApplet.java:1624) 14 at processing.core.PApplet.handleMouseEvent(PApplet.java:2687) 15 at processing.core.PApplet.dequeueEvents(PApplet.java:2609) 16 at processing.core.PApplet.handleDraw(PApplet.java:2450) 17 at processing.awt.PSurfaceAWT$12.callDraw(PSurfaceAWT.java:1547) 18 at processing.core.PSurfaceNone$AnimationThread.run(PSurfaceNone.java:316)

確かに最初のボタンをクリックした場合はエラーは発生しませんが、2番目(以降?)のボタンをクリックすると調子が悪い様です。スタックトレースをみた感じ「ボタンを押した=MousePressed」際のイベントの後、「ボタンを離した=MouseReleased」イベントを処理しようとした際になんらかの内部矛盾が生じているように見えました。ControlP5のコントロールの属性を見ると「isPressed()」のようなメソッドがあることから「そのコントロールに対してMouseClickedイベントが発生した際にコントロールに「PRESSED状態」を設定し、MouseReleaseイベントが発生したら内部状態の「PRESSED状態」を解除しようとしていると思います。マウスを離した際に「PRESSED状態のコントロールの状態をPRESSEDでない状態に変更しようとして、それに失敗している(その時点ではもうコントロールが消されてしまっているから)」といった状況になっているのではないでしょうか?

ControlP5の内部を調べてないので上記は単なる想像ですが「MousePressed時のイベントハンドラーの中で直接コントロールを削除する」ような使い方を想定していないか、またはバグのような気がしました。複数ある最初のコントロールの場合だけは例外が発生せずに動くようですが、たまたま動いているだけのような気がします。

###対処
マウスが離されたときを狙ってボタンを削除すればよいのではないかと思い、ボタンを生成する際に

.activateBy(ControlP5.RELEASED)

とやってみたのですが、やはり例外が起きます。ならばということで「ボタンが押されたことをフラグに記録し、ボタン関連イベントの処理が終わった後で定期起動メソッド(つまりdrawメソッド)の中でそのフラグをみてボタンを消す(イベントの遅延発動とでもいいましょうか)という考え方にしてみました。

こうするとどのボタンを押しても例外は発生せず一応期待通りに動作するように見えます。

java

1...省略... 2 3// フラグを用意する 4boolean buttonClicked = false; 5 6void draw(){ 7 if (buttonClicked) { 8 println("removing all buttons"); 9 removeAllButtonsAndDrawImage(); 10 buttonClicked = false; 11 } 12} 13 14// 全てのControlP5のコントロールのイベントを受けるハンドラー(なのかな?) 15void controlEvent(ControlEvent event) { 16 Controller controller = event.getController(); 17 String controlName = controller.getName(); 18 println("event fired from " + controlName); 19 // イベント発生元のコントロールがButtonだったら 20 if (controller instanceof Button) { 21 //removeAllButtonsAndDrawImage(); // 直接ボタンを削除せずに 22 buttonClicked = true; // 後でボタンを削除するためのフラグのみ設定する 23 } 24}

本当のところどうするのが一番よいのかまでは分かりませんでしたが、回避例として挙げてみました。

投稿2018/01/29 07:24

KSwordOfHaste

総合スコア18394

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

ATMEGA

2018/01/30 02:38

素早い回答ありがとうございます。MouseReleasedが影響していたのですね。詳しい解説までありがとうございます。 おかげで解決いたしました。 ほんとにありがとうございます。
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.48%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問