質問編集履歴

4

コードの変更

2020/07/18 16:16

投稿

ppppapddp
ppppapddp

スコア1

test CHANGED
File without changes
test CHANGED
@@ -434,212 +434,160 @@
434
434
 
435
435
 
436
436
 
437
+
438
+
439
+ }
440
+
441
+ ```
442
+
443
+ ```
444
+
445
+ //BallPropagator.java
446
+
447
+ public class BallPropagator_19rd158{
448
+
449
+
450
+
451
+ public static double dt;
452
+
437
- public static void main(String[] args) {
453
+ public static int xmin;
438
-
439
-
440
-
441
-
442
-
443
- BallPropagator.dt=0.1;
454
+
444
-
445
-
446
-
447
-
448
-
449
- JFrame frame = new JFrame();
450
-
451
-
452
-
453
-
454
-
455
- BallMain panel = new BallMain(500,400);panel.setBackground(Color.white);panel.setPreferredSize(new Dimension(panel.xPanelSize, panel.yPanelSize));
456
-
457
-
458
-
459
-
460
-
461
- BallPropagator.xmin = 0;
455
+ public static int xmax;
462
-
463
- BallPropagator.xmax = panel.xPanelSize;
456
+
464
-
465
- BallPropagator.ymin = 0;
457
+ public static int ymin;
458
+
466
-
459
+ public static int ymax;
460
+
461
+
462
+
463
+
464
+
465
+ private double r;
466
+
467
+ private double x, y;
468
+
469
+ private double vx, vy;
470
+
471
+ private double ax, ay;
472
+
473
+
474
+
475
+ double p=0.8;
476
+
477
+
478
+
479
+ BallPropagator(double r,double x,double y,double vx,double vy,double ax,double ay){
480
+
481
+ this.r=r;
482
+
483
+ this.x=x;
484
+
485
+ this.y=y;
486
+
487
+ this.vx=vx;
488
+
489
+ this.vy=vy;
490
+
491
+ this.ax=ax;
492
+
493
+ this.ay=ay;
494
+
495
+ }
496
+
497
+
498
+
499
+ public void xUpdate() {
500
+
467
- BallPropagator.ymax = panel.yPanelSize;
501
+ this.x = this.x + this.vx * dt;
468
-
469
-
470
502
 
471
503
 
472
504
 
473
- frame.pack();
474
-
475
- //
476
-
477
- frame.setTitle("反射するボール");frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);frame.setResizable(true);
478
-
479
- frame.setVisible(true);
480
-
481
-
482
-
483
-
484
-
485
- Timer timer = new Timer(10, panel);
505
+ if(this.x < (double)xmin+this.r ){
486
-
506
+
507
+
508
+
487
- timer.start();
509
+ this.vx *= -p;
510
+
488
-
511
+ this.x = (double)xmin+this.r;
512
+
513
+ }else if((double)xmax-this.r < x ){
514
+
515
+
516
+
517
+ this.vx *= -p;
518
+
519
+ this.x = (double)xmax-this.r;
520
+
489
- }
521
+ }
522
+
523
+ }
524
+
525
+
526
+
527
+ public void yUpdate() {
528
+
529
+ this.y = this.y + this.vy * dt;
530
+
531
+
532
+
533
+ if(this.y < (double)ymin + this.r ){
534
+
535
+
536
+
537
+ this.vy *= -p;
538
+
539
+ this.y = (double)ymin+this.r;
540
+
541
+ }else if((double)ymax-this.r < this.y ){
542
+
543
+
544
+
545
+ this.vy *= -p;
546
+
547
+ this.y = (double)ymax-this.r;
548
+
549
+ }
550
+
551
+ }
552
+
553
+
554
+
555
+ public void vxUpdate(){
556
+
557
+
558
+
559
+ this.vx=this.ax*dt+this.vx;
560
+
561
+
562
+
563
+ }
564
+
565
+
566
+
567
+ public void vyUpdate(){
568
+
569
+ this.vy=this.ay*dt+this.vy;
570
+
571
+
490
572
 
491
573
  }
492
574
 
575
+ public double a(double x){
576
+
577
+ return x=this.x;
578
+
579
+ }
580
+
581
+ public double b(double x){
582
+
583
+ return x=this.y;
584
+
585
+ } public double c(double x){
586
+
587
+ return x=this.r;
588
+
589
+ }
590
+
591
+ }
592
+
493
593
  ```
494
-
495
- ```
496
-
497
- //BallPropagator.java
498
-
499
- public class BallPropagator_19rd158{
500
-
501
-
502
-
503
- public static double dt;
504
-
505
- public static int xmin;
506
-
507
- public static int xmax;
508
-
509
- public static int ymin;
510
-
511
- public static int ymax;
512
-
513
-
514
-
515
-
516
-
517
- private double r;
518
-
519
- private double x, y;
520
-
521
- private double vx, vy;
522
-
523
- private double ax, ay;
524
-
525
-
526
-
527
- double p=0.8;
528
-
529
-
530
-
531
- BallPropagator(double r,double x,double y,double vx,double vy,double ax,double ay){
532
-
533
- this.r=r;
534
-
535
- this.x=x;
536
-
537
- this.y=y;
538
-
539
- this.vx=vx;
540
-
541
- this.vy=vy;
542
-
543
- this.ax=ax;
544
-
545
- this.ay=ay;
546
-
547
- }
548
-
549
-
550
-
551
- public void xUpdate() {
552
-
553
- this.x = this.x + this.vx * dt;
554
-
555
-
556
-
557
- if(this.x < (double)xmin+this.r ){
558
-
559
-
560
-
561
- this.vx *= -p;
562
-
563
- this.x = (double)xmin+this.r;
564
-
565
- }else if((double)xmax-this.r < x ){
566
-
567
-
568
-
569
- this.vx *= -p;
570
-
571
- this.x = (double)xmax-this.r;
572
-
573
- }
574
-
575
- }
576
-
577
-
578
-
579
- public void yUpdate() {
580
-
581
- this.y = this.y + this.vy * dt;
582
-
583
-
584
-
585
- if(this.y < (double)ymin + this.r ){
586
-
587
-
588
-
589
- this.vy *= -p;
590
-
591
- this.y = (double)ymin+this.r;
592
-
593
- }else if((double)ymax-this.r < this.y ){
594
-
595
-
596
-
597
- this.vy *= -p;
598
-
599
- this.y = (double)ymax-this.r;
600
-
601
- }
602
-
603
- }
604
-
605
-
606
-
607
- public void vxUpdate(){
608
-
609
-
610
-
611
- this.vx=this.ax*dt+this.vx;
612
-
613
-
614
-
615
- }
616
-
617
-
618
-
619
- public void vyUpdate(){
620
-
621
- this.vy=this.ay*dt+this.vy;
622
-
623
-
624
-
625
- }
626
-
627
- public double a(double x){
628
-
629
- return x=this.x;
630
-
631
- }
632
-
633
- public double b(double x){
634
-
635
- return x=this.y;
636
-
637
- } public double c(double x){
638
-
639
- return x=this.r;
640
-
641
- }
642
-
643
- }
644
-
645
- ```

3

情報の追加

2020/07/18 16:16

投稿

ppppapddp
ppppapddp

スコア1

test CHANGED
File without changes
test CHANGED
@@ -6,6 +6,10 @@
6
6
 
7
7
  現在、自学でjavaをやっています。今回、最終的にボタンを押すとボールがその方向に加速するというプログラムを作ろうと思っています。ですが途中でボタンを押しても反応しなくなりました。なぜなのかわからないので教えていただきたいです
8
8
 
9
+ Sample.javaでコンパイルするとボタンが反応しないです
10
+
11
+
12
+
9
13
  ■■な機能を実装中に以下のエラーメッセージが発生しました。
10
14
 
11
15
 

2

付属のソースコードを追加しました

2020/07/18 16:07

投稿

ppppapddp
ppppapddp

スコア1

test CHANGED
File without changes
test CHANGED
@@ -24,6 +24,8 @@
24
24
 
25
25
  ```
26
26
 
27
+ //Sample.java
28
+
27
29
  import java.awt.*;
28
30
 
29
31
  import java.awt.event.*;
@@ -44,9 +46,9 @@
44
46
 
45
47
  double x,y,r,vx,vy,ax,ay;
46
48
 
47
- BallPropagator_19rd158 b1;
49
+ BallPropagator b1;
48
-
50
+
49
- BallPropagator_19rd158 b2;
51
+ BallPropagator b2;
50
52
 
51
53
  Color c1;
52
54
 
@@ -60,7 +62,7 @@
60
62
 
61
63
  static JButton btn4 = new JButton("←");
62
64
 
63
- Sample19rd158(int xPanelSize, int yPanelSize){
65
+ Sample(int xPanelSize, int yPanelSize){
64
66
 
65
67
 
66
68
 
@@ -72,9 +74,9 @@
72
74
 
73
75
 
74
76
 
75
- b1 = new BallPropagator_19rd158(10, 100, 100, 30, 40, 0, 9.8);
77
+ b1 = new BallPropagator(10, 100, 100, 30, 40, 0, 9.8);
76
-
78
+
77
- b2 = new BallPropagator_19rd158(10, 10, 100, 50, 20, 0, 9.8); /////////////////////////////////////////////
79
+ b2 = new BallPropagator(10, 10, 100, 50, 20, 0, 9.8); /////////////////////////////////////////////
78
80
 
79
81
  c1=Color.black;
80
82
 
@@ -228,7 +230,7 @@
228
230
 
229
231
 
230
232
 
231
- BallPropagator_19rd158.dt=0.1;
233
+ BallPropagator.dt=0.1;
232
234
 
233
235
 
234
236
 
@@ -240,7 +242,7 @@
240
242
 
241
243
 
242
244
 
243
- BallMain_19rd158 panel = new BallMain_19rd158(500,400);
245
+ BallMain panel = new BallMain(500,400);
244
246
 
245
247
 
246
248
 
@@ -258,13 +260,13 @@
258
260
 
259
261
 
260
262
 
261
- BallPropagator_19rd158.xmin = 0;
263
+ BallPropagator.xmin = 0;
262
-
264
+
263
- BallPropagator_19rd158.xmax = panel.xPanelSize;
265
+ BallPropagator.xmax = panel.xPanelSize;
264
-
266
+
265
- BallPropagator_19rd158.ymin = 0;
267
+ BallPropagator.ymin = 0;
266
-
268
+
267
- BallPropagator_19rd158.ymax = panel.yPanelSize;
269
+ BallPropagator.ymax = panel.yPanelSize;
268
270
 
269
271
 
270
272
 
@@ -302,7 +304,7 @@
302
304
 
303
305
  ```
304
306
 
305
-
307
+ //BallMain.java
306
308
 
307
309
  import java.awt.*;
308
310
 
@@ -312,7 +314,7 @@
312
314
 
313
315
 
314
316
 
315
- public class BallMain_19rd158 extends JPanel implements ActionListener{
317
+ public class BallMain extends JPanel implements ActionListener{
316
318
 
317
319
 
318
320
 
@@ -324,15 +326,15 @@
324
326
 
325
327
  double x,y,r,vx,vy,ax,ay;
326
328
 
327
- BallPropagator_19rd158 b1;
329
+ BallPropagator b1;
328
-
330
+
329
- BallPropagator_19rd158 b2;
331
+ BallPropagator b2;
330
332
 
331
333
  Color c1;
332
334
 
333
335
  Color c2;
334
336
 
335
- BallMain_19rd158(int xPanelSize, int yPanelSize){
337
+ BallMain (int xPanelSize, int yPanelSize){
336
338
 
337
339
 
338
340
 
@@ -344,9 +346,9 @@
344
346
 
345
347
 
346
348
 
347
- b1 = new BallPropagator_19rd158(10, 100, 100, 30, 40, 0, 9.8);
349
+ b1 = new BallPropagator (10, 100, 100, 30, 40, 0, 9.8);
348
-
350
+
349
- b2 = new BallPropagator_19rd158(10, 10, 100, 50, 20, 0, 9.8);
351
+ b2 = new BallPropagator (10, 10, 100, 50, 20, 0, 9.8);
350
352
 
351
353
  c1=Color.black;
352
354
 
@@ -434,7 +436,7 @@
434
436
 
435
437
 
436
438
 
437
- BallPropagator_19rd158.dt=0.1;
439
+ BallPropagator.dt=0.1;
438
440
 
439
441
 
440
442
 
@@ -446,19 +448,19 @@
446
448
 
447
449
 
448
450
 
449
- BallMain_19rd158 panel = new BallMain_19rd158(500,400);panel.setBackground(Color.white);panel.setPreferredSize(new Dimension(panel.xPanelSize, panel.yPanelSize));
451
+ BallMain panel = new BallMain(500,400);panel.setBackground(Color.white);panel.setPreferredSize(new Dimension(panel.xPanelSize, panel.yPanelSize));
450
-
451
-
452
-
453
-
454
-
452
+
453
+
454
+
455
+
456
+
455
- BallPropagator_19rd158.xmin = 0;
457
+ BallPropagator.xmin = 0;
456
-
458
+
457
- BallPropagator_19rd158.xmax = panel.xPanelSize;
459
+ BallPropagator.xmax = panel.xPanelSize;
458
-
460
+
459
- BallPropagator_19rd158.ymin = 0;
461
+ BallPropagator.ymin = 0;
460
-
462
+
461
- BallPropagator_19rd158.ymax = panel.yPanelSize;
463
+ BallPropagator.ymax = panel.yPanelSize;
462
464
 
463
465
 
464
466
 
@@ -488,6 +490,8 @@
488
490
 
489
491
  ```
490
492
 
493
+ //BallPropagator.java
494
+
491
495
  public class BallPropagator_19rd158{
492
496
 
493
497
 
@@ -520,7 +524,7 @@
520
524
 
521
525
 
522
526
 
523
- BallPropagator_19rd158(double r,double x,double y,double vx,double vy,double ax,double ay){
527
+ BallPropagator(double r,double x,double y,double vx,double vy,double ax,double ay){
524
528
 
525
529
  this.r=r;
526
530
 
@@ -548,7 +552,7 @@
548
552
 
549
553
  if(this.x < (double)xmin+this.r ){
550
554
 
551
- // this.vx = ??????????
555
+
552
556
 
553
557
  this.vx *= -p;
554
558
 
@@ -556,7 +560,7 @@
556
560
 
557
561
  }else if((double)xmax-this.r < x ){
558
562
 
559
- // this.vx = ??????????
563
+
560
564
 
561
565
  this.vx *= -p;
562
566
 

1

付属のソースコードを追加しました

2020/07/18 16:04

投稿

ppppapddp
ppppapddp

スコア1

test CHANGED
File without changes
test CHANGED
@@ -22,7 +22,7 @@
22
22
 
23
23
  ソースコード
24
24
 
25
-
25
+ ```
26
26
 
27
27
  import java.awt.*;
28
28
 
@@ -292,30 +292,346 @@
292
292
 
293
293
  timer.start();
294
294
 
295
-
296
-
297
-
295
+
298
296
 
299
297
  }
300
298
 
301
299
  }
302
300
 
303
-
304
-
305
-
306
-
307
-
308
-
309
- ### 試したこと
310
-
311
-
312
-
313
- ここに問題に対して試したことを記載してください。
314
-
315
-
316
-
317
- ### 補足情報(FW/ツールのバージョンなど)
318
-
319
-
320
-
321
- ここにより詳細な情報を記載してください。
301
+ ```
302
+
303
+ ```
304
+
305
+
306
+
307
+ import java.awt.*;
308
+
309
+ import java.awt.event.*;
310
+
311
+ import javax.swing.*;
312
+
313
+
314
+
315
+ public class BallMain_19rd158 extends JPanel implements ActionListener{
316
+
317
+
318
+
319
+ static int xPanelSize;
320
+
321
+ static int yPanelSize;
322
+
323
+
324
+
325
+ double x,y,r,vx,vy,ax,ay;
326
+
327
+ BallPropagator_19rd158 b1;
328
+
329
+ BallPropagator_19rd158 b2;
330
+
331
+ Color c1;
332
+
333
+ Color c2;
334
+
335
+ BallMain_19rd158(int xPanelSize, int yPanelSize){
336
+
337
+
338
+
339
+ this.xPanelSize = xPanelSize;
340
+
341
+ this.yPanelSize = yPanelSize;
342
+
343
+
344
+
345
+
346
+
347
+ b1 = new BallPropagator_19rd158(10, 100, 100, 30, 40, 0, 9.8);
348
+
349
+ b2 = new BallPropagator_19rd158(10, 10, 100, 50, 20, 0, 9.8);
350
+
351
+ c1=Color.black;
352
+
353
+ c2=Color.blue;
354
+
355
+
356
+
357
+ }
358
+
359
+
360
+
361
+
362
+
363
+ public void actionPerformed(ActionEvent e){
364
+
365
+
366
+
367
+
368
+
369
+ b1.xUpdate();
370
+
371
+ b1.yUpdate();
372
+
373
+
374
+
375
+ b1.vxUpdate();
376
+
377
+ b1.vyUpdate();
378
+
379
+
380
+
381
+
382
+
383
+ b2.xUpdate();
384
+
385
+ b2.yUpdate();
386
+
387
+
388
+
389
+ b2.vxUpdate();
390
+
391
+ b2.vyUpdate();
392
+
393
+
394
+
395
+ repaint();
396
+
397
+ }
398
+
399
+
400
+
401
+ public void paintComponent(Graphics g){
402
+
403
+ super.paintComponent(g);
404
+
405
+
406
+
407
+ g.setColor(c1);
408
+
409
+
410
+
411
+ g.fillOval((int)(b1.a(x) - b1.c(r)), (int)(b1.b(y) - b1.c(r)),
412
+
413
+ (int)(2*b1.c(r)), (int)(2*b1.c(r)));
414
+
415
+
416
+
417
+
418
+
419
+ g.setColor(c2);
420
+
421
+
422
+
423
+ g.fillOval((int)(b2.a(x) - b2.c(r)), (int)(b2.b(y) - b2.c(r)),
424
+
425
+ (int)(2*b2.c(r)), (int)(2*b2.c(r)));
426
+
427
+ }
428
+
429
+
430
+
431
+ public static void main(String[] args) {
432
+
433
+
434
+
435
+
436
+
437
+ BallPropagator_19rd158.dt=0.1;
438
+
439
+
440
+
441
+
442
+
443
+ JFrame frame = new JFrame();
444
+
445
+
446
+
447
+
448
+
449
+ BallMain_19rd158 panel = new BallMain_19rd158(500,400);panel.setBackground(Color.white);panel.setPreferredSize(new Dimension(panel.xPanelSize, panel.yPanelSize));
450
+
451
+
452
+
453
+
454
+
455
+ BallPropagator_19rd158.xmin = 0;
456
+
457
+ BallPropagator_19rd158.xmax = panel.xPanelSize;
458
+
459
+ BallPropagator_19rd158.ymin = 0;
460
+
461
+ BallPropagator_19rd158.ymax = panel.yPanelSize;
462
+
463
+
464
+
465
+
466
+
467
+ frame.pack();
468
+
469
+ //
470
+
471
+ frame.setTitle("反射するボール");frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);frame.setResizable(true);
472
+
473
+ frame.setVisible(true);
474
+
475
+
476
+
477
+
478
+
479
+ Timer timer = new Timer(10, panel);
480
+
481
+ timer.start();
482
+
483
+ }
484
+
485
+ }
486
+
487
+ ```
488
+
489
+ ```
490
+
491
+ public class BallPropagator_19rd158{
492
+
493
+
494
+
495
+ public static double dt;
496
+
497
+ public static int xmin;
498
+
499
+ public static int xmax;
500
+
501
+ public static int ymin;
502
+
503
+ public static int ymax;
504
+
505
+
506
+
507
+
508
+
509
+ private double r;
510
+
511
+ private double x, y;
512
+
513
+ private double vx, vy;
514
+
515
+ private double ax, ay;
516
+
517
+
518
+
519
+ double p=0.8;
520
+
521
+
522
+
523
+ BallPropagator_19rd158(double r,double x,double y,double vx,double vy,double ax,double ay){
524
+
525
+ this.r=r;
526
+
527
+ this.x=x;
528
+
529
+ this.y=y;
530
+
531
+ this.vx=vx;
532
+
533
+ this.vy=vy;
534
+
535
+ this.ax=ax;
536
+
537
+ this.ay=ay;
538
+
539
+ }
540
+
541
+
542
+
543
+ public void xUpdate() {
544
+
545
+ this.x = this.x + this.vx * dt;
546
+
547
+
548
+
549
+ if(this.x < (double)xmin+this.r ){
550
+
551
+ // this.vx = ??????????
552
+
553
+ this.vx *= -p;
554
+
555
+ this.x = (double)xmin+this.r;
556
+
557
+ }else if((double)xmax-this.r < x ){
558
+
559
+ // this.vx = ??????????
560
+
561
+ this.vx *= -p;
562
+
563
+ this.x = (double)xmax-this.r;
564
+
565
+ }
566
+
567
+ }
568
+
569
+
570
+
571
+ public void yUpdate() {
572
+
573
+ this.y = this.y + this.vy * dt;
574
+
575
+
576
+
577
+ if(this.y < (double)ymin + this.r ){
578
+
579
+
580
+
581
+ this.vy *= -p;
582
+
583
+ this.y = (double)ymin+this.r;
584
+
585
+ }else if((double)ymax-this.r < this.y ){
586
+
587
+
588
+
589
+ this.vy *= -p;
590
+
591
+ this.y = (double)ymax-this.r;
592
+
593
+ }
594
+
595
+ }
596
+
597
+
598
+
599
+ public void vxUpdate(){
600
+
601
+
602
+
603
+ this.vx=this.ax*dt+this.vx;
604
+
605
+
606
+
607
+ }
608
+
609
+
610
+
611
+ public void vyUpdate(){
612
+
613
+ this.vy=this.ay*dt+this.vy;
614
+
615
+
616
+
617
+ }
618
+
619
+ public double a(double x){
620
+
621
+ return x=this.x;
622
+
623
+ }
624
+
625
+ public double b(double x){
626
+
627
+ return x=this.y;
628
+
629
+ } public double c(double x){
630
+
631
+ return x=this.r;
632
+
633
+ }
634
+
635
+ }
636
+
637
+ ```