Repository navigation
Expand file tree
/
Copy pathItemPanel.java
More file actions
294 lines (265 loc) · 9.24 KB
/
Copy pathItemPanel.java
File metadata and controls
294 lines (265 loc) · 9.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
import java.awt.*;
import java.awt.event.*;
import javax.security.auth.Subject;
import javax.swing.*;
import java.util.*;
import java.io.*;
/* View : ゲーム画面(UI部) */
class ItemPanel extends JPanel implements Observer {
JLabel label1, label2, star_time_label, book_time_label, trap_label;
Image img;
JPanel mainpanel1, upperpanel1, mainpanel2, subpanel, item_time_panel, trap_panel;
Font font;
String str;
long min, sec;
protected Character chara;
public ImagePanel imagepanel1, imagepanel2, imagepanel3, imagepanel4, coinpanel;
protected AllItem allitem;
protected Count_time time;
private long timer;
private Dimension screenSize;
public ItemPanel(AllItem allitem, Character ch, Count_time time) {
this.allitem = allitem;
this.chara = ch;
this.time = time;
screenSize = Toolkit.getDefaultToolkit().getScreenSize();
if (screenSize.getHeight() < 1000) {
this.setPreferredSize(new Dimension(1000, (int) screenSize.getHeight() / 100 * 92));
} else {
this.setPreferredSize(new Dimension(1000, 950));
}
this.setLayout(new GridLayout(3, 1));
this.setOpaque(false);
try {
font = Font.createFont(Font.TRUETYPE_FONT, new File("data/PixelMplus12-Regular.ttf"));
font = font.deriveFont(100f);
} catch (FontFormatException e) {
System.out.println("形式がフォントではありません。");
} catch (IOException e) {
System.out.println("入出力エラーでフォントを読み込むことができませんでした。");
}
// grid1個目
mainpanel1 = new JPanel();
mainpanel1.setPreferredSize(new Dimension(1000, 100));
upperpanel1 = new JPanel();
upperpanel1.setOpaque(false);
upperpanel1.setPreferredSize(new Dimension(1000, 100));
timer = time.getTime();
label1 = new JLabel();
set_time(label1, time);
label1.setForeground(Color.RED);
label1.setFont(font);
label1.setOpaque(false);
upperpanel1.add(label1);
// タイマーを中央に寄せる
mainpanel1.setLayout(new GridLayout(1, 3));
trap_panel = new JPanel();
trap_label = new JLabel("状態異常", JLabel.CENTER);
font = font.deriveFont(80f);
trap_label.setFont(font);
font = font.deriveFont(100f);
trap_label.setOpaque(false);
trap_panel.setOpaque(false);
trap_panel.add(trap_label);
mainpanel1.add(trap_panel);
mainpanel1.add(upperpanel1);
coinpanel = new ImagePanel("data/coin.png", chara);
coinpanel.setOpaque(false);
mainpanel1.add(coinpanel);
mainpanel1.setOpaque(false);
this.add(mainpanel1);
// grid2個目
mainpanel1 = new JPanel();
mainpanel1.setPreferredSize(new Dimension(1000, 280));
label2 = new JLabel("<html><span style='font-size:80pt;color:yellow;'>ミラーorランダム発動中</span></html>",
JLabel.CENTER);
label2.setPreferredSize(new Dimension(1000, 280));
label2.setOpaque(false);
mainpanel1.setOpaque(false);
this.add(mainpanel1);
// grid3個目
mainpanel2 = new JPanel();
mainpanel2.setLayout(new BoxLayout(mainpanel2, BoxLayout.LINE_AXIS));
imagepanel1 = new ImagePanel("data/star.png", chara);
imagepanel1.setOpaque(false);
mainpanel2.add(imagepanel1);
imagepanel2 = new ImagePanel("data/sword.png", chara);
imagepanel2.setOpaque(false);
mainpanel2.add(imagepanel2);
imagepanel3 = new ImagePanel("data/hammer.png", chara);
imagepanel3.setOpaque(false);
mainpanel2.add(imagepanel3);
imagepanel4 = new ImagePanel("data/map.png", chara);
imagepanel4.setOpaque(false);
mainpanel2.add(imagepanel4);
mainpanel2.setOpaque(false);
// アイテムを中央に寄せる
mainpanel1 = new JPanel();
mainpanel1.setLayout(new GridLayout(1, 3));
item_time_panel = new JPanel();
item_time_panel.setLayout(new GridLayout(4, 1));
item_time_panel.setOpaque(false);
star_time_label = new JLabel();
book_time_label = new JLabel();
set_star_time(star_time_label, ch);
set_book_time(book_time_label, ch);
star_time_label.setForeground(Color.RED);
star_time_label.setOpaque(false);
star_time_label.setPreferredSize(new Dimension(300, 200));
star_time_label.setVerticalAlignment(JLabel.BOTTOM);
star_time_label.setHorizontalAlignment(JLabel.CENTER);
book_time_label.setForeground(Color.RED);
book_time_label.setOpaque(false);
book_time_label.setPreferredSize(new Dimension(300, 200));
book_time_label.setVerticalAlignment(JLabel.BOTTOM);
book_time_label.setHorizontalAlignment(JLabel.CENTER);
subpanel = new JPanel();
subpanel.setOpaque(false);
item_time_panel.add(subpanel);
subpanel = new JPanel();
subpanel.setOpaque(false);
item_time_panel.add(subpanel);
item_time_panel.add(star_time_label);
item_time_panel.add(book_time_label);
mainpanel1.add(item_time_panel);
mainpanel1.add(mainpanel2);
subpanel = new JPanel();
subpanel.setOpaque(false);
mainpanel1.add(subpanel);
mainpanel1.setOpaque(false);
this.add(mainpanel1);
this.setOpaque(false);
}
// 時間表記を定める
public void set_time(JLabel label, Count_time time) {
timer = time.getTime();
min = (timer / 600);
sec = (timer / 10) % 60;
str = String.format("%02d:%02d", min, sec);
label.setText(str);
label.setForeground(Color.RED);
}
// スターの残り時間についての表示
public void set_star_time(JLabel label, Character ch) {
sec = ch.getStarTime();
str = String.format("スター終了まで%d", sec);
label.setText(str);
font = font.deriveFont(35f);
label.setFont(font);
font = font.deriveFont(100f);
if (sec == 0) {
label.setForeground(Color.BLACK);
} else {
label.setForeground(Color.RED);
}
}
// マップ残り時間についての表示
public void set_book_time(JLabel label, Character ch) {
sec = ch.getBookTime();
str = String.format("マップ終了まで%d", sec);
label.setText(str);
font = font.deriveFont(35f);
label.setFont(font);
font = font.deriveFont(100f);
label.setForeground(Color.RED);
if (sec == 0 && ch.getInvincible() == false) {
label.setForeground(Color.BLACK);
} else if (sec == 0 && ch.getInvincible() == true) {
label.setForeground(Color.CYAN);
} else {
label.setForeground(Color.RED);
}
}
// 状態異常についての表示
public void set_trap_condition(JLabel label, Character ch) {
if (ch.get_mirror_now() == true) {
label.setForeground(Color.RED);
} else if (ch.get_mirror_now() == false && ch.getInvincible() == true) {
label.setForeground(Color.CYAN);
} else {
label.setForeground(Color.BLACK);
}
}
public void update(Observable o, Object arg) {
repaint();
}
class ImagePanel extends JPanel {
private String img_name, itemcount;
private int width, height, size = 0, item_count = 0;
private Font font;
private Character chara;
public ImagePanel(String img_name, Character ch) {
this.img_name = img_name;
chara = ch;
try {
font = Font.createFont(Font.TRUETYPE_FONT, new File("data/PixelMplus12-Regular.ttf"));
font = font.deriveFont(30f);
font = font.deriveFont(Font.BOLD);
} catch (FontFormatException e) {
System.out.println("形式がフォントではありません。");
} catch (IOException e) {
System.out.println("入出力エラーでフォントを読み込むことができませんでした。");
}
}
public void paintComponent(Graphics g) {
set_time(label1, time);
set_star_time(star_time_label, chara);
set_book_time(book_time_label, chara);
set_trap_condition(trap_label, chara);
super.paintComponent(g);
Image img = Toolkit.getDefaultToolkit().getImage(img_name);
width = this.getSize().width;
this.height = getSize().height;
if (width < height) {
size = width;
} else {
size = height;
} // アイテムを綺麗に表示するために短い方の長さに揃える
// 現在のアイテム数を取得
if (img_name == "data/star.png") {
item_count = chara.getStarCount();
if (chara.getInvincible() == true) {
g.setColor(Color.BLUE);
g.fillRect(0, height / 3 * 2, size, size);
}
} else if (img_name == "data/sword.png") {
item_count = chara.getSwordCount();
} else if (img_name == "data/hammer.png") {
item_count = chara.getHammerCount();
} else if (img_name == "data/map.png") {
item_count = chara.getBookCount();
} else if (img_name == "data/coin.png") {
item_count = chara.getCoinCount();
}
if (img_name == "data/coin.png") {
Graphics2D g2 = (Graphics2D) g;
BasicStroke bs = new BasicStroke(5);
g2.setStroke(bs);
g.drawImage(img, size / 3, 10, size / 2, size / 2, this);
g.setColor(Color.RED);
g2.drawRect(size / 3, 10, size / 2 + 1, size / 2 + 1);
itemcount = Integer.toString(item_count);
g.setFont(font);
g.drawString(itemcount, size / 4 * 3, size / 2);
} else {
Graphics2D g2 = (Graphics2D) g;
BasicStroke bs = new BasicStroke(3);
g2.setStroke(bs);
g.drawImage(img, 0, height / 3 * 2, size, size, this);
g.setColor(Color.RED);
g2.drawRect(0, height / 3 * 2, size - 1, size - 1);
itemcount = Integer.toString(item_count);
g.setFont(font);
if (item_count >= 10 && screenSize.getHeight() >= 1000) {
g.drawString(itemcount, width - 35, height - 30);
} else if (screenSize.getHeight() >= 1000) {
g.drawString(itemcount, width - 20, height - 30);
} else if (item_count >= 10) {
g.drawString(itemcount, width - 35, height - 20);
} else {
g.drawString(itemcount, width - 20, height - 20);
}
}
}
}
}