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

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

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

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

Q&A

解決済

2回答

4447閲覧

描画されない原因について

msw

総合スコア9

Processing

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

0グッド

0クリップ

投稿2020/01/02 11:15

前提・実現したいこと

追加したコードを動かしたい。

発生している問題・エラーメッセージ

下記のコード中に記してある(←追加したもの)コードを追加した所、
実行しても描画されなくなってしまいました。
エラーメッセージも出ておらず、何が原因なのか分かりません。
追加する場所を間違えているのでしょうか?

該当のソースコード

Processing

1import ddf.minim.*; 2import ddf.minim.analysis.*; 3import ddf.minim.effects.*; 4import ddf.minim.signals.*; 5import ddf.minim.spi.*; 6import ddf.minim.ugens.*; 7 8Minim minim; 9AudioPlayer player; 10int buffersize = 512; 11 12class Note { 13 final float x; 14 final float y; 15 final float w; 16 final float h; 17 18 final float r; 19 final float r2; 20 21 final color col;//HSB, alpha 22 23 boolean isAlive = true; 24 25 Note(final float x, final float y, final float playerIn) { 26 this.x = random(minx, maxx); //横 ←追加したもの 27 this.y = random(miny, maxy); //縦 ←追加したもの 28 this.w = playerIn/2; 29 this.h = playerIn/2; 30 this.r = this.w / 2; 31 this.r2 = this.r * this.r; 32 this.col = color(random(360), 100, 100, 70); 33 } 34 35 void draw() { 36 colorMode(HSB, 360, 100, 100, 100); 37 noStroke(); 38 fill(col); 39 // ellipse(x, y, w, h); 40 } 41 42 void update() { 43 } 44 45 boolean isExpired() { 46 return !isAlive; 47 } 48 49 void toggle() { 50 isAlive = !isAlive; 51 } 52} 53 54abstract class Effect { 55 protected float time; 56 protected float t = 0; 57 final float dt = 1.0; 58 59 Effect() { 60 } 61 62 abstract void draw(); 63 64 abstract void update(); 65 66 void dec() { 67 time -= dt; 68 t += dt; 69 } 70 71 boolean isExpired() { 72 return time < 0; 73 } 74} 75 76abstract class MultiEffect extends Effect { 77 ArrayList<Effect> chidren = new ArrayList<Effect>(); 78 79 MultiEffect() { 80 } 81 82 @Override 83 void draw() { 84 for (Effect eff : chidren) { 85 eff.draw(); 86 } 87 } 88 89 @Override 90 void update() { 91 for (Effect eff : chidren) { 92 eff.update(); 93 } 94 } 95 96 @Override 97 boolean isExpired() { 98 for (Effect eff : chidren) { 99 if (!eff.isExpired()) return false; 100 } 101 return true; 102 } 103} 104 105class Bakuhatu extends Effect { 106 final float x; 107 final float y; 108 float r; 109 float dr = 1; 110 final float ddr = 0.5; 111 112 final int R, G, B; 113 int alpha; 114 int da = 0; 115 final int dda = 1; 116 117 Bakuhatu(final float x, final float y, final float r, final color col, final float time) { 118 this.x = x; 119 this.y = y; 120 this.r = r; 121 122 final int mask = 0xFF; 123 alpha = (col>>24) & mask; 124 alpha /= 2; 125 R = (col>>16) & mask; 126 G = (col>>8) & mask; 127 B = col & mask; 128 129 this.time = time; 130 } 131 132 @Override 133 void draw() { 134 noStroke(); 135 //fill(col); 136 colorMode(RGB); 137 fill(R, G, B, alpha); 138 ellipse(x, y, 2*r, 2*r); 139 } 140 141 @Override 142 void update() { 143 dec(); 144 r += dr; 145 dr += ddr; 146 147 alpha -= da; 148 da += dda; 149 } 150} 151 152final float g = 0.2; 153 154class Hinoko extends Effect { 155 float x; 156 float y; 157 final float r = 2; 158 final float v0 = 5; 159 float vx; 160 float vy; 161 162 final float theta; 163 164 final color col; 165 int R, G, B; 166 int alpha; 167 float da = 1; 168 final float dda = 0.05; 169 float drx, dry; 170 float ddrx, ddry; 171 172 Hinoko(final float x, final float y, final float r, final color col, final float theta, final float time) { 173 174 this.x = x; 175 this.y = y; 176 this.vx = v0 * cos(theta); 177 this.vy = v0 * sin(theta); 178 this.col = col; 179 180 final int mask = 0xFF; 181 alpha = (col>>24) & mask; 182 //alpha /= 2; 183 R = (col>>16) & mask; 184 G = (col>>8) & mask; 185 B = col & mask; 186 187 this.drx = 0.5; 188 this.dry = 0.05; 189 this.ddrx = 0.5; 190 this.ddry = 0.05; 191 192 this.time = time; 193 this.theta = theta; 194 } 195 196 @Override 197 void draw() { 198 noStroke(); 199 colorMode(RGB); 200 fill(R, G, B, alpha); 201 ellipse(x, y, r, r); 202 } 203 204 @Override 205 void update() { 206 dec(); 207 208 alpha -= da; 209 da += dda; 210 211 x += vx; 212 y += vy; 213 y += g*t; 214 215 drx += ddrx; 216 dry += ddry; 217 } 218} 219 220class Hanabi extends MultiEffect { 221 Hanabi(final float x, final float y, final float r, final color col) { 222 super(); 223 224 final float timeB = 30; 225 chidren.add(new Bakuhatu(x, y, r, col, timeB)); 226 227 final float timeH = 50; 228 final int max = 32; 229 final float dtheta = TWO_PI / max; 230 for (int i=0; i<max; i++) { 231 chidren.add(new Hinoko(x, y, r, col, dtheta*i, timeH)); 232 } 233 } 234} 235 236class Effects extends ArrayList<Effect> { 237 Effects() { 238 //do nothing 239 } 240 241 void add(Effects temp) { 242 this.addAll(temp); 243 } 244 245 void draw() { 246 for (Effect eff : this) { 247 eff.draw(); 248 } 249 } 250 251 void update() { 252 ArrayList<Effect> temp = new ArrayList<Effect>(); 253 for (Effect eff : this) { 254 eff.update(); 255 if (!eff.isExpired()) temp.add(eff); 256 } 257 this.clear(); 258 this.addAll(temp); 259 } 260} 261 262class NoteController { 263 ArrayList<Note> notes = new ArrayList<Note>(); 264 265 final float margin = 100; 266 final float xmin = margin; 267 final float xmax = width-margin; 268 final float ymin = margin; 269 final float ymax = height-margin; 270 271 NoteController() { 272 // 273 } 274 //円の制御 275 void add(final float playerIn) { 276 if (notes.size() > 100) return;//check 277 if (playerIn < 20) return; 278 if (frameCount % 20 != 0) return; 279 280 final float x = random(xmin, xmax); 281 final float y = random(ymin, ymax); 282 notes.add(new Note(x, y, playerIn)); 283 } 284 285 void draw() { 286 for (Note note : notes) { 287 note.draw(); 288 } 289 } 290 291 ArrayList<Note> update() { 292 ArrayList<Note> next = new ArrayList<Note>(); 293 ArrayList<Note> del = new ArrayList<Note>(); 294 for (Note note : notes) { 295 note.update(); 296 if (note.isExpired()) { 297 del.add(note); 298 } else { 299 next.add(note); 300 } 301 } 302 notes.clear(); 303 notes.addAll(next); 304 return del; 305 } 306 307 //真偽を使って判断 308 boolean isHit(final Note note) { 309 return true; 310 } 311 //全指定 312 void mouseClicked() { 313 for (Note note : notes) { 314 note.toggle(); 315 } 316 } 317} 318 319class Converter { 320 Effects makeHanabis(ArrayList<Note> notes) { 321 Effects temp = new Effects(); 322 for (Note note : notes) { 323 temp.add(makeHanabi(note)); 324 } 325 return temp; 326 } 327 328 Hanabi makeHanabi(Note note) { 329 Hanabi temp = new Hanabi(note.x, note.y, note.r, note.col); 330 return temp; 331 } 332} 333 334NoteController controller; 335Effects effects; 336Converter converter; 337 338float minx, maxx; 339float miny, maxy; 340ArrayList<PVector> bow=new ArrayList<PVector>(); 341 342void setup() { 343 fullScreen(); 344 frameRate(60); 345 colorMode(HSB, 360, 100, 100, 100); 346 controller = new NoteController(); 347 effects = new Effects(); 348 converter = new Converter(); 349 350 minim = new Minim(this); 351 //in = minim.getLineIn(Minim.STEREO,buffersize); 352 final String fn = "Mountain.mp3"; 353 player = minim.loadFile(fn); 354 player.loop(); 355} 356 357void draw(float x, float y) { 358 background(200); 359 360 Tree(30, width/2, height, PI, width/7); 361 362 //↓追加したもの 363EXIT: 364 while (true) { 365 x = random(minx, maxx); //横 366 y = random(miny, maxy); //縦 367 for (PVector p : bow) { 368 if (dist(x, y, p.x, p.y )<20) { 369 370 noStroke(); 371 fill(random(255), random(255), random(255), 50); 372 ellipse(x, y, 10, 10); 373 374 fill(#959592); 375 rect(0, 1030, 2000, 50); 376 break EXIT; 377 } 378 } 379 } 380 //↑追加したもの 381 382 final float playerIn = player.mix.level()*150; 383 controller.add(playerIn); 384 controller.draw(); 385 ArrayList<Note> temp = controller.update(); 386 387 effects.add(converter.makeHanabis(temp)); 388 effects.draw(); 389 effects.update(); 390} 391 392//木の生成 393void Tree(float strokeWeight, float x, float y, float rotate, float length) { 394 pushMatrix(); //座標を一時保存 395 translate(x, y); //座標移動 396 rotate(rotate); //座標回転 397 strokeWeight(strokeWeight); //線の輪郭線の太さ 398 stroke(70, 0, 0); //線の色 399 line(0, 0, 0, length); //線の描画,配列の長さ 400 popMatrix(); //変更した座標を元に戻す 401 402 //↓追加したもの 403 if (strokeWeight<2 || x%5>3 && strokeWeight<3) { 404 float xx=x-sin(rotate)*length; 405 float yy=y+cos(rotate)*length; 406 if (xx<minx) { 407 minx=xx; 408 } 409 if (xx>maxx) { 410 maxx=xx; 411 } 412 if (yy<miny) { 413 miny=yy; 414 } 415 if (yy>maxy) { 416 maxy=yy; 417 } 418 PVector endp=new PVector(xx, yy); 419 bow.add(endp); 420 return; 421 } 422 //↑追加したもの 423 424 Tree(strokeWeight*2/3, x-sin(rotate)*length, y+cos(rotate)*length, rotate-PI/10, length*2/3); 425 Tree(strokeWeight*2/3, x-sin(rotate)*length, y+cos(rotate)*length, rotate+PI/10, length*2/3); 426} 427 428void keyPressed() { 429 if (key == 's') { //音楽ストップ 430 player.pause(); 431 } else if (key == 'r') { //音楽リスタート 432 player.rewind(); 433 player.play(); 434 } 435} 436 437void stop() { 438 minim.stop(); 439 super.stop(); 440} 441 442void mouseClicked() { 443 controller.mouseClicked(); 444} 445

補足情報(FW/ツールのバージョンなど)

Processing 3.5.3

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

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

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

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

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

guest

回答2

0

自己解決

1日掛けてなんとかプログラムを動かす事が出来ました!
まだ過去の試行錯誤による余計な残りものや書き方が汚い点がありますが、
これから仕上げて完成形にしたいと思います。
thkana様には大変お世話になりました。心より感謝申し上げます。
実行画面

Processing

1import ddf.minim.*; 2import ddf.minim.analysis.*; 3import ddf.minim.effects.*; 4import ddf.minim.signals.*; 5import ddf.minim.spi.*; 6import ddf.minim.ugens.*; 7 8float minx, maxx; 9float miny, maxy; 10ArrayList<PVector> bow=new ArrayList<PVector>(); 11 12Minim minim; 13//AudioInput in; 14AudioPlayer player; 15//float volumeIn; 16int buffersize = 512; 17 18class Note { 19 final float x; 20 final float y; 21 final float w; 22 final float h; 23 final float r; 24 final float r2; 25 26 final color col;//HSB, alpha 27 28 boolean isAlive = true; 29 30 Note(final float x, final float y, final float playerIn) { 31 int n=(int)random(0, bow.size()); 32 float r=random(0, 10); 33 float th=random(0, PI*2); 34 this.x = bow.get(n).x+r*cos(th); 35 this.y = bow.get(n).y+r*sin(th); 36 this.w = playerIn/3; 37 this.h = playerIn/3; 38 39 this.r = this.w / 2; 40 this.r2 = this.r * this.r; 41 42 this.col = color(random(360), 100, 100, 200); 43 } 44 45 void draw() { 46 colorMode(HSB, 360, 100, 100, 200); 47 noStroke(); 48 fill(col); 49 ellipse(x, y, w, h); 50 } 51 52 void update() { 53 } 54 55 boolean isExpired() { 56 return !isAlive; 57 } 58 59 void toggle() { 60 isAlive = !isAlive; 61 } 62} 63 64abstract class Effect { 65 protected float time; 66 protected float t = 0; 67 final float dt = 1.0; 68 69 Effect() { 70 } 71 72 abstract void draw(); 73 74 abstract void update(); 75 76 void dec() { 77 time -= dt; 78 t += dt; 79 } 80 81 boolean isExpired() { 82 return time < 0; 83 } 84} 85 86abstract class MultiEffect extends Effect { 87 ArrayList<Effect> chidren = new ArrayList<Effect>(); 88 89 MultiEffect() { 90 } 91 92 @Override 93 void draw() { 94 for (Effect eff : chidren) { 95 eff.draw(); 96 } 97 } 98 99 @Override 100 void update() { 101 for (Effect eff : chidren) { 102 eff.update(); 103 } 104 } 105 106 @Override 107 boolean isExpired() { 108 for (Effect eff : chidren) { 109 if (!eff.isExpired()) return false; 110 } 111 return true; 112 } 113} 114 115class Bakuhatu extends Effect { 116 final float x; 117 final float y; 118 float r; 119 float dr = 1; 120 final float ddr = 0.5; 121 //color col; 122 123 final int R, G, B; 124 int alpha; 125 int da = 0; 126 final int dda = 1; 127 128 Bakuhatu(final float x, final float y, final float r, final color col, final float time) { 129 this.x = x; 130 this.y = y; 131 this.r = r; 132 133 final int mask = 0xFF; 134 alpha = (col>>24) & mask; 135 alpha /= 2; 136 R = (col>>16) & mask; 137 G = (col>>8) & mask; 138 B = col & mask; 139 140 this.time = time; 141 } 142 143 @Override 144 void draw() { 145 noStroke(); 146 //fill(col); 147 colorMode(RGB); 148 fill(R, G, B, alpha); 149 ellipse(x, y, 2*r, 2*r); 150 } 151 152 @Override 153 void update() { 154 dec(); 155 r += dr; 156 dr += ddr; 157 158 alpha -= da; 159 da += dda; 160 } 161} 162 163final float g = 0.2; 164 165class Hinoko extends Effect { 166 float x; 167 float y; 168 final float r = 2; 169 final float v0 = 5; 170 float vx; 171 float vy; 172 173 final float theta; 174 175 final color col; 176 int R, G, B; 177 int alpha; 178 float da = 1; 179 final float dda = 0.05; 180 181 //final float start; 182 183 float drx, dry; 184 float ddrx, ddry; 185 186 Hinoko(final float x, final float y, final float r, final color col, final float theta, final float time) { 187 188 this.x = x; 189 this.y = y; 190 191 this.vx = v0 * cos(theta); 192 this.vy = v0 * sin(theta); 193 194 this.col = col; 195 //this.col = color(h,s,b,alpha); 196 197 final int mask = 0xFF; 198 alpha = (col>>24) & mask; 199 //alpha /= 2; 200 R = (col>>16) & mask; 201 G = (col>>8) & mask; 202 B = col & mask; 203 204 this.drx = 0.5; 205 this.dry = 0.05; 206 this.ddrx = 0.5; 207 this.ddry = 0.05; 208 209 this.time = time; 210 //this.start = time; 211 this.theta = theta; 212 } 213 214 @Override 215 void draw() { 216 noStroke(); 217 colorMode(RGB); 218 fill(R, G, B, alpha); 219 //fill(col); 220 ellipse(x, y, r, r); 221 } 222 223 @Override 224 void update() { 225 dec(); 226 227 alpha -= da; 228 da += dda; 229 230 x += vx; 231 y += vy; 232 y += g*t; 233 234 drx += ddrx; 235 dry += ddry; 236 } 237} 238 239class Hanabi extends MultiEffect { 240 Hanabi(final float x, final float y, final float r, final color col) { 241 super(); 242 243 final float timeB = 30; 244 chidren.add(new Bakuhatu(x, y, r, col, timeB)); 245 246 final float timeH = 50; 247 final int max = 32; 248 final float dtheta = TWO_PI / max; 249 for (int i=0; i<max; i++) { 250 chidren.add(new Hinoko(x, y, r, col, dtheta*i, timeH)); 251 } 252 } 253} 254 255class Effects extends ArrayList<Effect> { 256 Effects() { 257 //do nothing 258 } 259 260 void add(Effects temp) { 261 this.addAll(temp); 262 } 263 264 void draw() { 265 for (Effect eff : this) { 266 eff.draw(); 267 } 268 } 269 270 void update() { 271 ArrayList<Effect> temp = new ArrayList<Effect>(); 272 for (Effect eff : this) { 273 eff.update(); 274 if (!eff.isExpired()) temp.add(eff); 275 } 276 this.clear(); 277 this.addAll(temp); 278 } 279} 280 281class NoteController { 282 ArrayList<Note> notes = new ArrayList<Note>(); 283 284 final float margin = 100; 285 final float xmin = margin; 286 final float xmax = width-margin; 287 final float ymin = margin; 288 final float ymax = height-margin; 289 290 NoteController() { 291 // 292 } 293 //円の制御 294 void add(final float playerIn) { 295 if (notes.size() > 100) return;//check 296 if (playerIn < 20) return; 297 if (frameCount % 20 != 0) return; 298 //if (frameCount % (int)random(10, 40) != 0) return; 299 300 final float x = random(xmin, xmax); 301 final float y = random(ymin, ymax); 302 notes.add(new Note(x, y, playerIn)); 303 } 304 305 void draw() { 306 for (Note note : notes) { 307 note.draw(); 308 } 309 } 310 311 ArrayList<Note> update() { 312 ArrayList<Note> next = new ArrayList<Note>(); 313 ArrayList<Note> del = new ArrayList<Note>(); 314 for (Note note : notes) { 315 note.update(); 316 if (note.isExpired()) { 317 del.add(note); 318 } else { 319 next.add(note); 320 } 321 } 322 notes.clear(); 323 notes.addAll(next); 324 return del; 325 } 326 327 //真偽を使って判断 328 boolean isHit(final Note note) { 329 return true; 330 } 331 //全指定 332 void mouseClicked() { 333 for (Note note : notes) { 334 note.toggle(); 335 } 336 } 337} 338 339class Converter { 340 Effects makeHanabis(ArrayList<Note> notes) { 341 Effects temp = new Effects(); 342 for (Note note : notes) { 343 temp.add(makeHanabi(note)); 344 } 345 return temp; 346 } 347 348 Hanabi makeHanabi(Note note) { 349 Hanabi temp = new Hanabi(note.x, note.y, note.r,note.col); 350 return temp; 351 } 352} 353 354NoteController controller; 355Effects effects; 356Converter converter; 357 358void setup() { 359 fullScreen(); 360 frameRate(60); 361 362 /*Tree(30, width/2, height, PI, width/8); 363 fill(#8B7250); 364 rect(0, 1030, 2000, 50); */ 365 366 colorMode(HSB, 360, 100, 100, 100); 367 controller = new NoteController(); 368 effects = new Effects(); 369 converter = new Converter(); 370 371 minim = new Minim(this); 372 //in = minim.getLineIn(Minim.STEREO,buffersize); 373 final String fn = "Mountain.mp3"; 374 player = minim.loadFile(fn); 375 player.loop(); 376} 377 378void draw() { 379 background(200); 380 381 Tree(30, width/2, height, PI, width/7); 382 noStroke(); 383 fill(#8B7250); 384 rect(0, 1030, 2000, 50); 385 386 // volumeIn = in.mix.level()*200; 387 final float playerIn = player.mix.level()*150; 388 controller.add(playerIn); 389 controller.draw(); 390 ArrayList<Note> temp = controller.update(); 391 392 effects.add(converter.makeHanabis(temp)); 393 effects.draw(); 394 effects.update(); 395} 396 397//木の生成 398void Tree(float strokeWeight, float x, float y, float rotate, float length) { 399 pushMatrix(); //座標を一時保存 400 translate(x, y); //座標移動 401 rotate(rotate); //座標回転 402 strokeWeight(strokeWeight); //線の輪郭線の太さ 403 stroke(#64482A); //線の色 404 line(0, 0, 0, length); //線の描画,配列の長さ 405 popMatrix(); //変更した座標を元に戻す 406 if (strokeWeight<2 || x%5>3 && strokeWeight<3) { 407 float xx=x-sin(rotate)*length; 408 float yy=y+cos(rotate)*length; 409 if (xx<minx) { 410 minx=xx; 411 } 412 if (xx>maxx) { 413 maxx=xx; 414 } 415 if (yy<miny) { 416 miny=yy; 417 } 418 if (yy>maxy) { 419 maxy=yy; 420 } 421 PVector endp=new PVector(xx, yy); 422 bow.add(endp); 423 return; 424 } 425 Tree(strokeWeight*2/3, x-sin(rotate)*length, y+cos(rotate)*length, rotate-PI/10, length*2/3); 426 Tree(strokeWeight*2/3, x-sin(rotate)*length, y+cos(rotate)*length, rotate+PI/10, length*2/3); 427} 428 429int count = 1; 430 431void keyPressed() { 432 if (key == 's') { //音楽ストップ 433 player.pause(); 434 } else if (key == 'r') { //音楽リスタート 435 player.rewind(); 436 player.play(); 437 } 438 439 // Pのキーが入力された時に保存 440 if (key == 'p' || key == 'P') { 441 442 // デスクトップのパスを取得 443 String path = System.getProperty("user.home") + "/Desktop/screenshot" + count + ".jpg"; 444 445 // 保存 446 save(path); 447 448 // 番号を加算 449 count++; 450 451 // ログ用途 452 println("screen saved." + path); 453 } 454} 455 456void stop() { 457 minim.stop(); 458 super.stop(); 459} 460 461void mouseClicked() { 462 controller.mouseClicked(); 463} 464

投稿2020/01/03 08:51

msw

総合スコア9

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

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

0

出来ればgitとかを導入してプログラムの変更履歴を残して、常に「動かなくなる直前」に戻せるようにしておくといいと思うけど。

「追加したもの」とコメントがついてなくて、しかし変わっているところがミソだと思う。前のプログラムとよく比べてみましょう。

投稿2020/01/02 12:20

編集2020/01/02 12:21
thkana

総合スコア7639

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問