-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameframe.java
More file actions
256 lines (203 loc) · 6.75 KB
/
Gameframe.java
File metadata and controls
256 lines (203 loc) · 6.75 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
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
import java.util.*;
import java.lang.Object.*;
public class Gameframe extends JPanel {
//in order to connect map interaction with info panel
public int mapIndex;
public int mouseMap;
//constructor of GUI
public Gameframe(){
//super();
JPanel content = this;
content.setLayout(new BorderLayout());
// create the header
JPanel headerPanel = new JPanel();
headerPanel.add(new JLabel("<html><h1>War Of Mine</h1></html>"));
content.add(headerPanel,BorderLayout.PAGE_START);
// create the map
Map mapP = new Map();
content.add(mapP,BorderLayout.CENTER);
// create the info panel
JPanel info = new JPanel();
content.add(info,BorderLayout.LINE_END);
//create the control JPanel
JPanel control = new JPanel();
control.setLayout(new GridBagLayout());
////create buttonP
JPanel buttonP = new JPanel();
buttonP.setLayout(new GridBagLayout());
GridBagConstraints c1 = new GridBagConstraints();
JButton up = new JButton("Up");up.setPreferredSize(new Dimension(70, 20));
c1.gridx = 1;
c1.gridy = 0;
buttonP.add(up,c1);
GridBagConstraints c2 = new GridBagConstraints();
JButton left = new JButton("Left");left.setPreferredSize(new Dimension(70, 20));
c2.gridx = 0;
c2.gridy = 1;
buttonP.add(left,c2);
GridBagConstraints c3 = new GridBagConstraints();
JButton stats = new JButton("Stats");stats.setPreferredSize(new Dimension(70, 20));
c3.gridx = 1;
c3.gridy = 1;
buttonP.add(stats,c3);
GridBagConstraints c4 = new GridBagConstraints();
JButton right = new JButton("Right");right.setPreferredSize(new Dimension(70, 20));
c4.gridx = 2;
c4.gridy = 1;
buttonP.add(right,c4);
GridBagConstraints c5 = new GridBagConstraints();
JButton down = new JButton("Down");down.setPreferredSize(new Dimension(70, 20));
c5.gridx = 1;
c5.gridy = 2;
buttonP.add(down,c5);
//add buttonP constrain and add to control panel
GridBagConstraints buttonPc = new GridBagConstraints();
buttonPc.gridx = 0;
buttonPc.gridy = 0;
buttonPc.fill = GridBagConstraints.HORIZONTAL;
control.add(buttonP, buttonPc);
// ending added buttonP
// start add log panel
JPanel logP = new JPanel();
logP.setLayout(new GridBagLayout());
GridBagConstraints p1 = new GridBagConstraints();
JTextArea logTA = new JTextArea("LOG WINDOW");
logTA.setPreferredSize(new Dimension(400,100));
p1.gridx = 0;
p1.gridy = 0;
p1.gridwidth = 4;
p1.ipady = 40;
logP.add(logTA, p1);
GridBagConstraints p2 = new GridBagConstraints();
JTextField inputTF = new JTextField("INPUT FEILD");
inputTF.setPreferredSize(new Dimension(360,40));
p2.gridx = 0;
p2.gridy = 1;
p2.gridwidth = 3;
logP.add(inputTF, p2);
GridBagConstraints p3 = new GridBagConstraints();
JButton inputB = new JButton("O");
inputB.setPreferredSize(new Dimension(40,40));
p3.gridx = 3;
p3.gridy = 1;
logP.add(inputB, p3);
GridBagConstraints logPc = new GridBagConstraints();
logPc.gridx = 1;
logPc.gridy =0;
logPc.gridwidth = 2;
logPc.weightx = 1;
logPc.fill = GridBagConstraints.HORIZONTAL;
control.add(logP, logPc);
// start add dice panel
GridBagConstraints dicePc = new GridBagConstraints();
JPanel diceP = new JPanel();
dicePc.gridx = 3;
dicePc.gridy = 0;
control.add(diceP, dicePc);
content.add(control, BorderLayout.PAGE_END);
}
//constructing a interactive map of 15x11 scale
public class Map extends JPanel implements MouseListener, MouseMotionListener{
int mousemoveIndex;
int currentx, currenty;
ArrayList<Block> blocks = new ArrayList<Block>();
public Map(){
setBackground(Color.black);
//setLayout(new BoxLayout(this, BoxLayout.PAGE_AXIS));
//setPreferredSize(new Dimension(600,440));
addMouseListener(this);
addMouseMotionListener(this);
int blockx =20;
int blocky =20;
for(int j=0; j<15;j++){
for(int i=0; i<11; i++){
Block z = new Block(blockx,blocky);
blocks.add(z);
blockx +=40;
}
blockx=20;
blocky+=40;
}
}
public void paintComponent(Graphics g){
// make a wihte sketchpad
g.setColor(Color.white);
g.fillRect(0,0,getWidth(),getHeight());
for(Block z : blocks){
z.draw(g);
}
}
public void mousePressed(MouseEvent evt) { }
public void mouseEntered(MouseEvent evt) { }
public void mouseExited(MouseEvent evt) { }
public void mouseClicked(MouseEvent evt) { }
public void mouseReleased(MouseEvent evt) { }
// these are the MouseMotionEvent methods
// when mouse moved in map, mouse move index change
public void mouseMoved(MouseEvent evt){
int mouseX = evt.getX();
int mouseY = evt.getY();
for(Block i : blocks){
if(i.isinBlock(mouseX,mouseY)==true){
i.color = Color.blue;
mousemoveIndex = blocks.indexOf(i);
} else{
i.color = Color.white;
}
}
repaint();
}
public void mouseDragged(MouseEvent evt){ }
class Block{
Color color = Color.white;
int x,y;
public Block(int x, int y){
this.x = x;
this.y = y;
}
public void draw(Graphics g){
g.setColor(color);
g.fillRect( x - 20, y - 20, 40, 40 );
g.setColor(Color.black);
g.drawRect( x - 20, y - 20, 40, 40 );
}
public Boolean isinBlock(int a, int b){
if(a >= x-20 && a < x+20 && b >= y-20 && b < y+20){
return true;
} else{
return false;
}
}
}
}
//end of map
/**
* Create the GUI and show it. For thread safety,
* this method should be invoked from the
* event-dispatching thread.
*/
private static void createAndShowGUI() {
//Create and set up the window.
JFrame frame = new JFrame("WAR OF MINE");
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
//Create and set up the content pane.
JComponent newContentPane = new Gameframe();
newContentPane.setOpaque(true); //content panes must be opaque
frame.setContentPane(newContentPane);
//Display the window.
frame.setSize(800,1000);
frame.setVisible(true);
}
public static void main(String[] args) {
//Schedule a job for the event-dispatching thread:
//creating and showing this application's GUI.
javax.swing.SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGUI();
}
});
}
}