回答編集履歴

1

コード追加

2021/09/03 13:56

投稿

jimbe
jimbe

スコア12659

test CHANGED
@@ -13,3 +13,191 @@
13
13
  フレームを表示して以降は、Swing の部品を外部から操作してはいけません。(より正確には、フレームの表示自体もSwing上でやらないといけないのですが…。)
14
14
 
15
15
  また、JFrame内をJPanelが動き回る構造よりも、JPanelをゲーム画面としてその中に各キャラクタを画像として書き込むほうが、自由度が高く、JPanelという重そうな(計ったことはありません)モノを使うより良いように思います。
16
+
17
+
18
+
19
+ ボタンを押すとボールが動き回ります。
20
+
21
+ ```java
22
+
23
+ package teratail_java.q357613;
24
+
25
+
26
+
27
+ import java.awt.Color;
28
+
29
+ import java.awt.Dimension;
30
+
31
+ import java.awt.FlowLayout;
32
+
33
+ import java.awt.Graphics;
34
+
35
+ import java.awt.Rectangle;
36
+
37
+ import java.awt.event.ActionEvent;
38
+
39
+ import java.awt.event.ActionListener;
40
+
41
+ import java.util.concurrent.Executors;
42
+
43
+ import java.util.concurrent.ScheduledExecutorService;
44
+
45
+ import java.util.concurrent.TimeUnit;
46
+
47
+
48
+
49
+ import javax.swing.JButton;
50
+
51
+ import javax.swing.JFrame;
52
+
53
+ import javax.swing.JPanel;
54
+
55
+
56
+
57
+ public class GameFrame extends JFrame {
58
+
59
+ public static void main(String[] args) {
60
+
61
+ new GameFrame().setVisible(true);
62
+
63
+ }
64
+
65
+ GameFrame() {
66
+
67
+ super("ゲーム画面");
68
+
69
+ setLocationRelativeTo(null);
70
+
71
+ setDefaultCloseOperation(EXIT_ON_CLOSE);
72
+
73
+ setResizable(false);
74
+
75
+ add(new GamePanel());
76
+
77
+ pack();
78
+
79
+ }
80
+
81
+ private class GamePanel extends JPanel {
82
+
83
+ private Ball ball;
84
+
85
+ private Rectangle frame;
86
+
87
+ private ScheduledExecutorService executor;
88
+
89
+ GamePanel() {
90
+
91
+ super(new FlowLayout());
92
+
93
+ Dimension size = new Dimension(200, 200);
94
+
95
+ setPreferredSize(size);
96
+
97
+ frame = new Rectangle(size);
98
+
99
+
100
+
101
+ ball = new Ball(90, 100, 0.9f, 0.5f);
102
+
103
+
104
+
105
+ executor = Executors.newSingleThreadScheduledExecutor();
106
+
107
+
108
+
109
+ JButton startButton = new JButton("start");
110
+
111
+ add(startButton);
112
+
113
+ startButton.addActionListener(new ActionListener() {
114
+
115
+ @Override
116
+
117
+ public void actionPerformed(ActionEvent e) {
118
+
119
+ startButton.setEnabled(false);
120
+
121
+ executor.scheduleAtFixedRate(new Runnable() {
122
+
123
+ @Override
124
+
125
+ public void run() {
126
+
127
+ ball.move(frame);
128
+
129
+ repaint();
130
+
131
+ }
132
+
133
+ }, 10, 10, TimeUnit.MILLISECONDS);
134
+
135
+ }
136
+
137
+ });
138
+
139
+ }
140
+
141
+ @Override
142
+
143
+ public void paintComponent(Graphics g) {
144
+
145
+ super.paintComponent(g);
146
+
147
+ ball.draw(g);
148
+
149
+ }
150
+
151
+ }
152
+
153
+ private class Ball {
154
+
155
+ private float x, y, dx, dy;
156
+
157
+ private int w=20, h=20;
158
+
159
+ Ball(int x, int y, float dx, float dy) {
160
+
161
+ this.x = x;
162
+
163
+ this.y = y;
164
+
165
+ this.dx = dx;
166
+
167
+ this.dy = dy;
168
+
169
+ }
170
+
171
+ void move(Rectangle frame) {
172
+
173
+ for(boolean xover=false, yover=false; !xover || !yover; ) {
174
+
175
+ xover = frame.contains(x+dx, y, w, h);
176
+
177
+ if(!xover) dx *= -1;
178
+
179
+ yover = frame.contains(x, y+dy, w, h);
180
+
181
+ if(!yover) dy *= -1;
182
+
183
+ }
184
+
185
+ x += dx;
186
+
187
+ y += dy;
188
+
189
+ }
190
+
191
+ void draw(Graphics g) {
192
+
193
+ g.setColor(Color.BLUE);
194
+
195
+ g.fillOval((int)x, (int)y, w, h);
196
+
197
+ }
198
+
199
+ }
200
+
201
+ }
202
+
203
+ ```