回答編集履歴

1

ラムダやめ

2020/06/20 07:53

投稿

TN8001
TN8001

スコア9326

test CHANGED
@@ -16,55 +16,61 @@
16
16
 
17
17
  ```Java
18
18
 
19
- new javax.swing.Timer(50, evt -> {
19
+ new javax.swing.Timer(50, new ActionListener() {
20
20
 
21
- double tm = 0.001 * (System.currentTimeMillis() - tm0);
21
+ @Override
22
22
 
23
- t1.setText(String.format("%5.2f", tm));
23
+ public void actionPerformed(ActionEvent evt) {
24
24
 
25
+ double tm = 0.001 * (System.currentTimeMillis() - tm0);
26
+
27
+ t1.setText(String.format("%5.2f", tm));
28
+
25
- int dx = 100, dy = 50;
29
+ int dx = 100, dy = 50;
26
30
 
27
31
 
28
32
 
29
- double x = tm * dx; // 総移動距離
33
+ double x = tm * dx; // 総移動距離
30
34
 
31
- double y = tm * dy;
35
+ double y = tm * dy;
32
36
 
33
- int w = getWidth() - 40; // 折り返しサイズ
37
+ int w = getWidth() - 40; // 折り返しサイズ
34
38
 
35
- int h = getHeight() - 40;
39
+ int h = getHeight() - 40;
36
40
 
37
41
 
38
42
 
39
- if ((int) (x / w) % 2 == 0) { // 総移動距離を横折り返しサイズで割った商が偶数なら→向き
43
+ if ((int) (x / w) % 2 == 0) { // 総移動距離を横折り返しサイズで割った商が偶数なら→向き
40
44
 
41
- x = x % w; // 余りをそのままxに
45
+ x = x % w; // 余りをそのままxに
42
46
 
43
- } else { // 奇数なら←向き
47
+ } else { // 奇数なら←向き
44
48
 
45
- x = w - x % w; // 反転されるのでwから引く
49
+ x = w - x % w; // 反転されるのでwから引く
46
50
 
47
- }
51
+ }
48
52
 
49
- if ((int) (y / h) % 2 == 0) { // 縦も同様
53
+ if ((int) (y / h) % 2 == 0) { // 縦も同様
50
54
 
51
- y = y % h;
55
+ y = y % h;
52
56
 
53
- } else {
57
+ } else {
54
58
 
55
- y = h - y % h;
59
+ y = h - y % h;
56
60
 
57
- }
61
+ }
58
62
 
59
- c1.moveTo(20 + (int) x, 20 + (int) y);
63
+ c1.moveTo(20 + (int) x, 20 + (int) y);
60
64
 
61
65
 
62
66
 
63
- // 1行で書くならこんなん??
67
+ // 1行で書くならこんなん??
64
68
 
65
- // c1.moveTo(20 + (int) Math.abs((x + w) % (w * 2) - w), 20 + (int) Math.abs((y + h) % (h * 2) - h));
69
+ // c1.moveTo(20 + (int) Math.abs((x + w) % (w * 2) - w), 20 + (int) Math.abs((y + h) % (h * 2) - h));
66
70
 
67
- repaint();
71
+ repaint();
72
+
73
+ }
68
74
 
69
75
  }).start();
70
76