回答編集履歴
3
コード追加
answer
CHANGED
@@ -16,4 +16,116 @@
|
|
16
16
|
System.out.println(file.exists()); //有るか無いか
|
17
17
|
}
|
18
18
|
}
|
19
|
+
```
|
20
|
+
---
|
21
|
+
じゃんけんの "手" に関わるものを enum Hand に集約してみます。
|
22
|
+
クラス分けも、パネルをレフリー/コンピュータ/プレイヤーに見立ててみました。
|
23
|
+
```java
|
24
|
+
import java.awt.*;
|
25
|
+
import java.util.Random;
|
26
|
+
import java.util.function.Consumer;
|
27
|
+
|
28
|
+
import javax.swing.*;
|
29
|
+
|
30
|
+
public class MainFrame extends JFrame {
|
31
|
+
public static void main(String[] args) {
|
32
|
+
SwingUtilities.invokeLater(() -> new MainFrame().setVisible(true));
|
33
|
+
}
|
34
|
+
|
35
|
+
MainFrame() {
|
36
|
+
super("じゃんけんゲーム");
|
37
|
+
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
38
|
+
setResizable(false);
|
39
|
+
|
40
|
+
RefereePanel refereePanel = new RefereePanel();
|
41
|
+
add(refereePanel, BorderLayout.NORTH);
|
42
|
+
|
43
|
+
ComputerPanel computerPanel = new ComputerPanel();
|
44
|
+
add(computerPanel, BorderLayout.CENTER);
|
45
|
+
|
46
|
+
add(new PlayerPanel(playerHand ->
|
47
|
+
refereePanel.finish(playerHand, computerPanel.getHand())
|
48
|
+
), BorderLayout.SOUTH);
|
49
|
+
|
50
|
+
pack();
|
51
|
+
setLocationRelativeTo(null);
|
52
|
+
}
|
53
|
+
|
54
|
+
private static enum Hand {
|
55
|
+
グー("image1.png"), チョキ("image2.png"), パー("image3.png");
|
56
|
+
|
57
|
+
String imagefile;
|
58
|
+
Hand(String imagefile) {
|
59
|
+
this.imagefile = imagefile;
|
60
|
+
}
|
61
|
+
/**
|
62
|
+
* 勝敗
|
63
|
+
* @param other 相手
|
64
|
+
* @return 0=引き分け, 1=負け, 2=勝ち
|
65
|
+
*/
|
66
|
+
int judge(Hand other) {
|
67
|
+
return (ordinal() - other.ordinal() + 3) % 3;
|
68
|
+
}
|
69
|
+
}
|
70
|
+
|
71
|
+
private static class RefereePanel extends JPanel {
|
72
|
+
private static final String[] TEXTS = { "あいこかよ!", "お前の負けかよ!", "お前の勝ちかよ!" };
|
73
|
+
private JLabel label;
|
74
|
+
RefereePanel() {
|
75
|
+
super(new BorderLayout());
|
76
|
+
setBackground(Color.BLACK);
|
77
|
+
setSize(640, 50);
|
78
|
+
setPreferredSize(getSize());
|
79
|
+
|
80
|
+
add(label = new MyLabel("「さあ、じゃんけんで勝負だ!」", 24, Color.WHITE));
|
81
|
+
}
|
82
|
+
void finish(Hand playerHand, Hand computerHand) {
|
83
|
+
label.setText(TEXTS[playerHand.judge(computerHand)]);
|
84
|
+
}
|
85
|
+
}
|
86
|
+
|
87
|
+
private static class ComputerPanel extends JPanel {
|
88
|
+
private Random random = new Random();
|
89
|
+
private JLabel label;
|
90
|
+
ComputerPanel() {
|
91
|
+
super(new BorderLayout());
|
92
|
+
setBackground(Color.WHITE);
|
93
|
+
setSize(640, 380);
|
94
|
+
setPreferredSize(getSize());
|
95
|
+
|
96
|
+
add(label = new MyLabel("じゃんけん……", 54, Color.BLACK));
|
97
|
+
}
|
98
|
+
Hand getHand() {
|
99
|
+
Hand computerHand = Hand.values()[random.nextInt(3)];
|
100
|
+
label.setText(computerHand.toString());
|
101
|
+
return computerHand;
|
102
|
+
}
|
103
|
+
}
|
104
|
+
|
105
|
+
private static class PlayerPanel extends JPanel {
|
106
|
+
PlayerPanel(Consumer<Hand> referee) {
|
107
|
+
super(new GridLayout());
|
108
|
+
setBackground(Color.BLACK);
|
109
|
+
setSize(640, 50);
|
110
|
+
setPreferredSize(getSize());
|
111
|
+
|
112
|
+
for(Hand hand : Hand.values()) {
|
113
|
+
JButton button = new JButton(hand.toString(), new ImageIcon("img/" + hand.imagefile));
|
114
|
+
button.setFont(new Font("MS ゴシック", Font.PLAIN, 24));
|
115
|
+
button.addActionListener(v -> referee.accept(hand));
|
116
|
+
add(button);
|
117
|
+
}
|
118
|
+
}
|
119
|
+
}
|
120
|
+
|
121
|
+
private static class MyLabel extends JLabel {
|
122
|
+
MyLabel(String str, int size, Color color) {
|
123
|
+
super(str);
|
124
|
+
setForeground(color);
|
125
|
+
setFont(new Font("MS ゴシック", Font.PLAIN, size));
|
126
|
+
setHorizontalAlignment(JLabel.CENTER);
|
127
|
+
setVerticalAlignment(JLabel.CENTER);
|
128
|
+
}
|
129
|
+
}
|
130
|
+
}
|
19
131
|
```
|
2
追加
answer
CHANGED
@@ -1,4 +1,19 @@
|
|
1
1
|
パスの指定がダメなようです。
|
2
2
|
まず、パッケージのフォルダ構造は実行には関係ありません。
|
3
3
|
そして、 src フォルダは eclipse の開発環境としての構造で、実行は src フォルダのある位置つまり 「プロジェクトフォルダ」がカレントフォルダになります。
|
4
|
-
従って、`(プロジェクトフォルダ)\img\image1.png` を指定するなら相対パスとしては `"img/image1.png"` となります。
|
4
|
+
従って、`(プロジェクトフォルダ)\img\image1.png` を指定するなら相対パスとしては `"img/image1.png"` となります。
|
5
|
+
|
6
|
+
↓のようなプログラムを作って確認してみては如何でしょうか。
|
7
|
+
```java
|
8
|
+
package jp.hoge.fuga.foo;
|
9
|
+
|
10
|
+
import java.io.File;
|
11
|
+
|
12
|
+
public class Test {
|
13
|
+
public static void main(String[] args) {
|
14
|
+
File file = new File("img/image1.png");
|
15
|
+
System.out.println(file.getAbsolutePath()); //絶対パス
|
16
|
+
System.out.println(file.exists()); //有るか無いか
|
17
|
+
}
|
18
|
+
}
|
19
|
+
```
|
1
修正
answer
CHANGED
@@ -1,4 +1,4 @@
|
|
1
1
|
パスの指定がダメなようです。
|
2
|
-
|
2
|
+
まず、パッケージのフォルダ構造は実行には関係ありません。
|
3
|
-
|
3
|
+
そして、 src フォルダは eclipse の開発環境としての構造で、実行は src フォルダのある位置つまり 「プロジェクトフォルダ」がカレントフォルダになります。
|
4
4
|
従って、`(プロジェクトフォルダ)\img\image1.png` を指定するなら相対パスとしては `"img/image1.png"` となります。
|