回答編集履歴
1
コード追加
answer
CHANGED
@@ -5,4 +5,98 @@
|
|
5
5
|
> 他にソースコードの改善点があれば
|
6
6
|
|
7
7
|
フレームを表示して以降は、Swing の部品を外部から操作してはいけません。(より正確には、フレームの表示自体もSwing上でやらないといけないのですが…。)
|
8
|
-
また、JFrame内をJPanelが動き回る構造よりも、JPanelをゲーム画面としてその中に各キャラクタを画像として書き込むほうが、自由度が高く、JPanelという重そうな(計ったことはありません)モノを使うより良いように思います。
|
8
|
+
また、JFrame内をJPanelが動き回る構造よりも、JPanelをゲーム画面としてその中に各キャラクタを画像として書き込むほうが、自由度が高く、JPanelという重そうな(計ったことはありません)モノを使うより良いように思います。
|
9
|
+
|
10
|
+
ボタンを押すとボールが動き回ります。
|
11
|
+
```java
|
12
|
+
package teratail_java.q357613;
|
13
|
+
|
14
|
+
import java.awt.Color;
|
15
|
+
import java.awt.Dimension;
|
16
|
+
import java.awt.FlowLayout;
|
17
|
+
import java.awt.Graphics;
|
18
|
+
import java.awt.Rectangle;
|
19
|
+
import java.awt.event.ActionEvent;
|
20
|
+
import java.awt.event.ActionListener;
|
21
|
+
import java.util.concurrent.Executors;
|
22
|
+
import java.util.concurrent.ScheduledExecutorService;
|
23
|
+
import java.util.concurrent.TimeUnit;
|
24
|
+
|
25
|
+
import javax.swing.JButton;
|
26
|
+
import javax.swing.JFrame;
|
27
|
+
import javax.swing.JPanel;
|
28
|
+
|
29
|
+
public class GameFrame extends JFrame {
|
30
|
+
public static void main(String[] args) {
|
31
|
+
new GameFrame().setVisible(true);
|
32
|
+
}
|
33
|
+
GameFrame() {
|
34
|
+
super("ゲーム画面");
|
35
|
+
setLocationRelativeTo(null);
|
36
|
+
setDefaultCloseOperation(EXIT_ON_CLOSE);
|
37
|
+
setResizable(false);
|
38
|
+
add(new GamePanel());
|
39
|
+
pack();
|
40
|
+
}
|
41
|
+
private class GamePanel extends JPanel {
|
42
|
+
private Ball ball;
|
43
|
+
private Rectangle frame;
|
44
|
+
private ScheduledExecutorService executor;
|
45
|
+
GamePanel() {
|
46
|
+
super(new FlowLayout());
|
47
|
+
Dimension size = new Dimension(200, 200);
|
48
|
+
setPreferredSize(size);
|
49
|
+
frame = new Rectangle(size);
|
50
|
+
|
51
|
+
ball = new Ball(90, 100, 0.9f, 0.5f);
|
52
|
+
|
53
|
+
executor = Executors.newSingleThreadScheduledExecutor();
|
54
|
+
|
55
|
+
JButton startButton = new JButton("start");
|
56
|
+
add(startButton);
|
57
|
+
startButton.addActionListener(new ActionListener() {
|
58
|
+
@Override
|
59
|
+
public void actionPerformed(ActionEvent e) {
|
60
|
+
startButton.setEnabled(false);
|
61
|
+
executor.scheduleAtFixedRate(new Runnable() {
|
62
|
+
@Override
|
63
|
+
public void run() {
|
64
|
+
ball.move(frame);
|
65
|
+
repaint();
|
66
|
+
}
|
67
|
+
}, 10, 10, TimeUnit.MILLISECONDS);
|
68
|
+
}
|
69
|
+
});
|
70
|
+
}
|
71
|
+
@Override
|
72
|
+
public void paintComponent(Graphics g) {
|
73
|
+
super.paintComponent(g);
|
74
|
+
ball.draw(g);
|
75
|
+
}
|
76
|
+
}
|
77
|
+
private class Ball {
|
78
|
+
private float x, y, dx, dy;
|
79
|
+
private int w=20, h=20;
|
80
|
+
Ball(int x, int y, float dx, float dy) {
|
81
|
+
this.x = x;
|
82
|
+
this.y = y;
|
83
|
+
this.dx = dx;
|
84
|
+
this.dy = dy;
|
85
|
+
}
|
86
|
+
void move(Rectangle frame) {
|
87
|
+
for(boolean xover=false, yover=false; !xover || !yover; ) {
|
88
|
+
xover = frame.contains(x+dx, y, w, h);
|
89
|
+
if(!xover) dx *= -1;
|
90
|
+
yover = frame.contains(x, y+dy, w, h);
|
91
|
+
if(!yover) dy *= -1;
|
92
|
+
}
|
93
|
+
x += dx;
|
94
|
+
y += dy;
|
95
|
+
}
|
96
|
+
void draw(Graphics g) {
|
97
|
+
g.setColor(Color.BLUE);
|
98
|
+
g.fillOval((int)x, (int)y, w, h);
|
99
|
+
}
|
100
|
+
}
|
101
|
+
}
|
102
|
+
```
|