回答編集履歴

1

コードを少し小さく

2021/08/13 11:20

投稿

jimbe
jimbe

スコア13209

test CHANGED
@@ -252,13 +252,13 @@
252
252
 
253
253
 
254
254
 
255
- setTrumps();
255
+ setCards();
256
-
256
+
257
- }
257
+ }
258
-
259
-
260
-
258
+
259
+
260
+
261
- void setTrumps() {
261
+ void setCards() {
262
262
 
263
263
  //シャッフル
264
264
 
@@ -370,6 +370,8 @@
370
370
 
371
371
 
372
372
 
373
+ removeMouseListener();
374
+
373
375
  Trump cardOne = (Trump)labelOne.getClientProperty(TRUMP_PROPERTY);
374
376
 
375
377
  if (cardTwo.getNumber() == cardOne.getNumber()) { //2枚目と1枚目が同じカード
@@ -398,14 +400,48 @@
398
400
 
399
401
 
400
402
 
403
+ abstract class DelayWorker extends SwingWorker<Object,Object> {
404
+
405
+ private long delay;
406
+
407
+ DelayWorker(long delay) {
408
+
409
+ this.delay = delay;
410
+
411
+ }
412
+
413
+ @Override
414
+
415
+ public Object doInBackground() {
416
+
417
+ try {
418
+
419
+ Thread.sleep(delay);
420
+
421
+ } catch (InterruptedException e) {
422
+
423
+ e.printStackTrace();
424
+
425
+ }
426
+
427
+ return null;
428
+
429
+ }
430
+
431
+ }
432
+
433
+
434
+
401
435
  //1秒後に One,Two それぞれを非表示状態にし再表示する
402
436
 
403
- class InvisibleWorker extends SwingWorker<Object,Object> {
437
+ class InvisibleWorker extends DelayWorker {
404
438
 
405
439
  private JLabel labelOne, labelTwo;
406
440
 
407
441
  InvisibleWorker(JLabel labelOne, JLabel labelTwo) {
408
442
 
443
+ super(1000);
444
+
409
445
  this.labelOne = labelOne;
410
446
 
411
447
  this.labelTwo = labelTwo;
@@ -414,21 +450,27 @@
414
450
 
415
451
  @Override
416
452
 
417
- public Object doInBackground() {
453
+ protected void done() {
454
+
418
-
455
+ labelOne.setVisible(false);
456
+
457
+ labelTwo.setVisible(false);
458
+
459
+ repaint();
460
+
419
- removeMouseListener();
461
+ setMouseListener();
420
-
421
- try {
462
+
422
-
423
- Thread.sleep(1000);
424
-
425
- } catch (InterruptedException e) {
426
-
427
- e.printStackTrace();
428
-
429
- }
463
+ }
464
+
430
-
465
+ }
466
+
467
+ //1秒後にリスタート状態にする
468
+
469
+ class RestartWorker extends DelayWorker {
470
+
471
+ RestartWorker() {
472
+
431
- return null;
473
+ super(1000);
432
474
 
433
475
  }
434
476
 
@@ -436,9 +478,7 @@
436
478
 
437
479
  protected void done() {
438
480
 
439
- labelOne.setVisible(false);
481
+ setCards();
440
-
441
- labelTwo.setVisible(false);
442
482
 
443
483
  repaint();
444
484
 
@@ -450,35 +490,39 @@
450
490
 
451
491
 
452
492
 
453
- //1秒後にリスタート状態にする
493
+ //1秒後に One,Two それぞれを close 状態にし再表示する
454
-
494
+
455
- class RestartWorker extends SwingWorker<Object,Object> {
495
+ class RedoWorker extends DelayWorker {
496
+
497
+ private JLabel labelOne, labelTwo;
498
+
499
+ private Trump cardOne, cardTwo;
500
+
501
+ RedoWorker(JLabel labelOne, Trump cardOne, JLabel labelTwo, Trump cardTwo) {
502
+
503
+ super(1000);
504
+
505
+ this.labelOne = labelOne;
506
+
507
+ this.cardOne = cardOne;
508
+
509
+ this.labelTwo = labelTwo;
510
+
511
+ this.cardTwo = cardTwo;
512
+
513
+ }
456
514
 
457
515
  @Override
458
516
 
459
- public Object doInBackground() {
460
-
461
- removeMouseListener();
462
-
463
- try {
464
-
465
- Thread.sleep(1000);
466
-
467
- } catch (InterruptedException e) {
468
-
469
- e.printStackTrace();
470
-
471
- }
472
-
473
- return null;
474
-
475
- }
476
-
477
- @Override
478
-
479
517
  protected void done() {
480
518
 
519
+ cardOne.close();
520
+
521
+ labelOne.setIcon(cardOne.getIcon());
522
+
481
- setTrumps();
523
+ cardTwo.close();
524
+
525
+ labelTwo.setIcon(cardTwo.getIcon());
482
526
 
483
527
  repaint();
484
528
 
@@ -488,68 +532,6 @@
488
532
 
489
533
  }
490
534
 
491
-
492
-
493
- //1秒後に One,Two それぞれを close 状態にし再表示する
494
-
495
- class RedoWorker extends SwingWorker<Object,Object> {
496
-
497
- private JLabel labelOne, labelTwo;
498
-
499
- private Trump cardOne, cardTwo;
500
-
501
- RedoWorker(JLabel labelOne, Trump cardOne, JLabel labelTwo, Trump cardTwo) {
502
-
503
- this.labelOne = labelOne;
504
-
505
- this.cardOne = cardOne;
506
-
507
- this.labelTwo = labelTwo;
508
-
509
- this.cardTwo = cardTwo;
510
-
511
- }
512
-
513
- @Override
514
-
515
- public Object doInBackground() {
516
-
517
- removeMouseListener();
518
-
519
- try {
520
-
521
- Thread.sleep(1000);
522
-
523
- } catch (InterruptedException e) {
524
-
525
- e.printStackTrace();
526
-
527
- }
528
-
529
- return null;
530
-
531
- }
532
-
533
- @Override
534
-
535
- protected void done() {
536
-
537
- cardOne.close();
538
-
539
- labelOne.setIcon(cardOne.getIcon());
540
-
541
- cardTwo.close();
542
-
543
- labelTwo.setIcon(cardTwo.getIcon());
544
-
545
- repaint();
546
-
547
- setMouseListener();
548
-
549
- }
550
-
551
- }
552
-
553
535
  }
554
536
 
555
537
  ```