案1 Draw
を内部クラスにする
Javaっぽい感じがする。
Java
1import java.awt.Color;
2import java.awt.Graphics;
3import java.awt.event.KeyEvent;
4import java.awt.event.KeyListener;
5import javax.swing.JFrame;
6import javax.swing.JPanel;
7
8public class ShootingGame {
9 public static void main(String[] args) {
10 new ShootingFrame("ShootingGame", 400, 300);
11 }
12}
13
14class ShootingFrame extends JFrame implements KeyListener {
15 private int playerX;
16 private int playerY;
17
18 public ShootingFrame(String title, int width, int height) {
19 super(title);
20 setDefaultCloseOperation(EXIT_ON_CLOSE);
21 setSize(width, height);
22 setLocationRelativeTo(null);
23 setResizable(false);
24 setVisible(true);
25 addKeyListener(this);
26
27 add(new Draw());
28
29 // setVisible後でないとサイズが取れないので注意
30 playerX = getContentPane().getWidth() / 2;
31 playerY = getContentPane().getHeight() / 2;
32 }
33
34 @Override public void keyPressed(KeyEvent e) {
35 switch (e.getKeyCode()) {
36 case KeyEvent.VK_LEFT: playerX -= 5; break;
37 case KeyEvent.VK_RIGHT: playerX += 5; break;
38 case KeyEvent.VK_UP: playerY -= 5; break;
39 case KeyEvent.VK_DOWN: playerY += 5; break;
40 case KeyEvent.VK_SPACE: break;
41 }
42 repaint();
43 }
44
45 @Override public void keyTyped(KeyEvent e) {}
46
47 @Override public void keyReleased(KeyEvent e) {}
48
49
50 class Draw extends JPanel {
51 public Draw() {
52 setBackground(Color.WHITE);
53 }
54
55 @Override public void paintComponent(Graphics g) {
56 super.paintComponent(g);
57
58 g.setColor(Color.BLACK);
59 g.fillRect(playerX - 50 / 2, playerY - 50 / 2, 50, 50);
60 }
61 }
62}
案2 Draw
でKeyListener
する
ボタン等がなくパネルだけならこれでいい気もする。
Java
1import java.awt.Color;
2import java.awt.Dimension;
3import java.awt.Graphics;
4import java.awt.event.KeyEvent;
5import java.awt.event.KeyListener;
6import javax.swing.JFrame;
7import javax.swing.JPanel;
8
9public class ShootingGame {
10 public static void main(String[] args) {
11 new ShootingFrame("ShootingGame");
12 }
13}
14
15class ShootingFrame extends JFrame {
16 public ShootingFrame(String title) {
17 super(title);
18 setDefaultCloseOperation(EXIT_ON_CLOSE);
19 setResizable(false);
20
21 add(new Draw());
22 pack();
23 setLocationRelativeTo(null);
24 setVisible(true);
25 }
26}
27
28class Draw extends JPanel implements KeyListener {
29 private int playerX = 400 / 2;
30 private int playerY = 300 / 2;
31
32 public Draw() {
33 setBackground(Color.WHITE);
34 setFocusable(true);
35 setPreferredSize(new Dimension(400, 300));
36 addKeyListener(this);
37// requestFocusInWindow();
38 }
39
40 @Override public void paintComponent(Graphics g) {
41 super.paintComponent(g);
42
43 g.setColor(Color.BLACK);
44 g.fillRect(playerX - 50 / 2, playerY - 50 / 2, 50, 50);
45 }
46
47 @Override public void keyPressed(KeyEvent e) {
48 switch (e.getKeyCode()) {
49 case KeyEvent.VK_LEFT: playerX -= 5; break;
50 case KeyEvent.VK_RIGHT: playerX += 5; break;
51 case KeyEvent.VK_UP: playerY -= 5; break;
52 case KeyEvent.VK_DOWN: playerY += 5; break;
53 case KeyEvent.VK_SPACE: break;
54 }
55 repaint();
56 }
57
58 @Override public void keyTyped(KeyEvent e) {}
59
60 @Override public void keyReleased(KeyEvent e) {}
61}
案3 ShootingFrame
とDraw
で共通の何かを持つ
なんにしろPlayer
クラス的なものは作ることになる気はする。
Java
1import java.awt.Color;
2import java.awt.Graphics;
3import java.awt.event.KeyEvent;
4import java.awt.event.KeyListener;
5import javax.swing.JFrame;
6import javax.swing.JPanel;
7
8public class ShootingGame {
9 public static void main(String[] args) {
10 new ShootingFrame("ShootingGame", 400, 300);
11 }
12}
13
14class Player {
15 public int X;
16 public int Y;
17}
18
19class ShootingFrame extends JFrame implements KeyListener {
20 private Player player = new Player();
21
22 public ShootingFrame(String title, int width, int height) {
23 super(title);
24 setDefaultCloseOperation(EXIT_ON_CLOSE);
25 setSize(width, height);
26 setLocationRelativeTo(null);
27 setResizable(false);
28 setVisible(true);
29 addKeyListener(this);
30
31 add(new Draw(player));
32
33 // setVisible後でないとサイズが取れないので注意
34 player.X = getContentPane().getWidth() / 2;
35 player.Y = getContentPane().getHeight() / 2;
36 }
37
38 @Override public void keyPressed(KeyEvent e) {
39 switch (e.getKeyCode()) {
40 case KeyEvent.VK_LEFT: player.X -= 5; break;
41 case KeyEvent.VK_RIGHT: player.X += 5; break;
42 case KeyEvent.VK_UP: player.Y -= 5; break;
43 case KeyEvent.VK_DOWN: player.Y += 5; break;
44 case KeyEvent.VK_SPACE: break;
45 }
46 repaint();
47 }
48
49 @Override public void keyTyped(KeyEvent e) {}
50
51 @Override public void keyReleased(KeyEvent e) {}
52}
53
54class Draw extends JPanel {
55 private Player player;
56
57 public Draw(Player player) {
58 this.player = player;
59 setBackground(Color.WHITE);
60 }
61
62 @Override public void paintComponent(Graphics g) {
63 super.paintComponent(g);
64
65 g.setColor(Color.BLACK);
66 g.fillRect(player.X - 50 / 2, player.Y - 50 / 2, 50, 50);
67 }
68}
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2021/08/08 04:51