-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathView.java
358 lines (322 loc) · 12 KB
/
View.java
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
/**
* View: Contains everything about graphics and images
* Know size of world, which images to load etc
*
* has methods to
* provide boundaries
* use proper images for direction
* load images for all direction (an image should only be loaded once!!! why?)
**/
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.event.ActionEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.AbstractAction;
import javax.swing.Action;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class View extends JFrame{
final static int frameWidth = 800;//500
final static int frameHeight = 800;//300
final static int imgWidth = 165;//165
final static int imgHeight = 165;
final int frameCount = 8;
private int picNum = 0;
private int picNumJump = 0;
private int picNumFire = 0;
private BufferedImage[][] pics;
private BufferedImage[][] jumppics;
private Direction dir = Direction.SOUTHEAST;
private int xloc;
private int yloc;
private DrawPanel drawPanel= new DrawPanel();
final int frameStartSize = 800;
private boolean actionJumpFire = false;
View () {
add(drawPanel);
// load any of the images that you would want into the picNames, in case you want to do anything
// other than just move the orc
String[] picNames = {"images/orc/orc_forward_southeast.png",
"images/orc/orc_forward_southwest.png", "images/orc/orc_forward_west.png",
"images/orc/orc_forward_south.png", "images/orc/orc_forward_northwest.png",
"images/orc/orc_forward_northeast.png", "images/orc/orc_forward_north.png",
"images/orc/orc_forward_east.png", "images/orc/orc_jump_north.png",
"images/orc/orc_jump_south.png", "images/orc/orc_jump_east.png",
"images/orc/orc_jump_west.png", "images/orc/orc_fire_north.png",
"images/orc/orc_fire_south.png", "images/orc/orc_fire_east.png",
"images/orc/orc_fire_west.png", "images/orc/orc_idle_ewns.png"};
//
// "images/orc/orc_forward_east.png","images/orc/orc_jump_north.png","images/orc/orc_jump_south.png",
// "images/orc/orc_jump_west.png","images/orc/orc_jump_east.png","images/orc/orc_jump_northwest.png",
// "images/orc/orc_jump_southeast.png","images/orc/orc_jump_northeast.png",
// "images/orc/orc_jump_southwest.png"};
//>>>>>>> master
// Initialize pics with a give 2D array
pics = new BufferedImage[picNames.length][frameCount];
//jumppics = new BufferedImage[picjumpNames.length][8];
for(int j = 0; j < picNames.length; j++) {
// load the image
BufferedImage img = createImage(picNames[j]);
//BufferedImage jumpimg = createImage(picjumpNames[j]);
// create the frameCount of the orc from that certain png to make it walk(change)
pics[j] = new BufferedImage[frameCount];
//<<<<<<< ManuelCuesta
// System.out.println(j);
if (j < 8) {
for(int i = 0; i < frameCount; i++) {
// all of the different movements of that image
pics[j][i] = img.getSubimage(imgWidth*i, 0, imgWidth, imgHeight);
}
} else if (j > 7 && j < 12) {
for(int i = 0; i < 8; i++) {
pics[j][i] = img.getSubimage(imgWidth*i, 0, imgWidth, imgHeight);
// System.out.print(i);
}
} else {
int k = 0;
for(int i = 0; i < 4; i++) {
pics[j][k] = img.getSubimage(imgWidth*i, 0, imgWidth, imgHeight);
System.out.print(i);
k++;
}
for(int i = 2; i > -1; i--) {
pics[j][k] = img.getSubimage(imgWidth*i, 0, imgWidth, imgHeight);
k++;
}
}
//=======
// //jumppics[j] = new BufferedImage[8];
// for(int i = 0; i < frameCount; i++) {
// // all of the different movements of that image
// pics[j][i] = img.getSubimage(imgWidth*i, 0, imgWidth, imgHeight);
// }
//for(int i = 0; i < 8; i++) {
// all of the different movements of that image
//jumppics[j][i] = jumpimg.getSubimage(imgWidth*i, 0, imgWidth, imgHeight);
//}
//>>>>>>> master
}
setBackground(Color.gray);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setSize(getWidth(), getHeight());
setVisible(true);
pack();
}
// Override the paint method in JPanel, and use getPicDir to get the right image
// corresponding to the correct direction
public void paint(Graphics g) {
g.drawImage(pics[getPicDir(dir)][picNum], xloc, yloc, Color.gray, this);
}
public void drawPanel() {
drawPanel.repaint();
}
private BufferedImage createImage(String fname){
BufferedImage bufferedImage;
try {
// Method now takes a String (fname) in order to get the
// right image to load for all the images passed through
bufferedImage = ImageIO.read(new File(fname));
return bufferedImage;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
public int getWidth() {
return frameWidth;
}
public int getHeight() {
return frameHeight;
}
public int getImageWidth() {
return imgWidth;
}
public int getImageHeight() {
return imgHeight;
}
public DrawPanel getPanel(){
return drawPanel;
}
public Direction getDir(){
return dir;
}
// I wanted this to be a switch statement, so that would be the only thing to change this into
// but switch-case statements needed ints and was having trouble with getting the enums to work
// in my favor with converting to ints, but I found a way to compare the strings, which works
// so it is just an alternative to what I originally wanted
private int getPicDir(Direction d) {
if (d.getName().equals(Direction.SOUTHEAST.getName())) { return 0;}
else if (d.getName().equals(Direction.SOUTHWEST.getName())) { return 1;}
else if (d.getName().equals(Direction.WEST.getName())) { return 2;}
else if (d.getName().equals(Direction.SOUTH.getName())) { return 3;}
else if (d.getName().equals(Direction.NORTHWEST.getName())) { return 4;}
else if (d.getName().equals(Direction.NORTHEAST.getName())) { return 5;}
else if (d.getName().equals(Direction.NORTH.getName())) { return 6;}
else if (d.getName().equals(Direction.EAST.getName())) { return 7;}
//<<<<<<< ManuelCuesta
else if (d.getName().equals(Direction.JUMPNORTH.getName())) { return 8;}
else if (d.getName().equals(Direction.JUMPSOUTH.getName())) { return 9;}
else if (d.getName().equals(Direction.JUMPEAST.getName())) { return 10;}
else if (d.getName().equals(Direction.JUMPWEST.getName())) { return 11;}
else if (d.getName().equals(Direction.FIRENORTH.getName())) { return 12;}
else if (d.getName().equals(Direction.FIRESOUTH.getName())) { return 13;}
else if (d.getName().equals(Direction.FIREEAST.getName())) { return 14;}
else if (d.getName().equals(Direction.FIREWEST.getName())) { return 15;}
else if (d.getName().equals(Direction.IDLEEWNS.getName())) { return 16;}
//=======
// else if (d.getName().equals(Direction.JUMP_NORTH.getName())) { return 8;}
// else if (d.getName().equals(Direction.JUMP_SOUTH.getName())) { return 9;}
// else if (d.getName().equals(Direction.JUMP_WEST.getName())) { return 10;}
// else if (d.getName().equals(Direction.JUMP_EAST.getName())) { return 11;}
// else if (d.getName().equals(Direction.JUMP_NORTHWEST.getName())) { return 12;}
// else if (d.getName().equals(Direction.JUMP_SOUTHEAST.getName())) { return 13;}
// else if (d.getName().equals(Direction.JUMP_NORTHEAST.getName())) { return 14;}
// else if (d.getName().equals(Direction.JUMP_SOUTHWEST.getName())) { return 15;}
//>>>>>>> master
else { return 99999;}// Never going to happen, just a default for compiling
}
public void setActionJumpFire(boolean bool) {
actionJumpFire = bool;
}
public boolean getActionJumpFire() {
return actionJumpFire;
}
public Direction getDirFromJumpFire(Direction d) {
if (d == Direction.JUMPNORTH || d == Direction.FIRENORTH) {
return Direction.NORTH;
} else if (d == Direction.JUMPSOUTH || d == Direction.FIRESOUTH) {
return Direction.SOUTH;
} else if (d == Direction.JUMPEAST || d == Direction.FIREEAST) {
return Direction.EAST;
} else if (d == Direction.JUMPWEST || d == Direction.FIREWEST) {
return Direction.WEST;
} else {return d;} //default
}
@SuppressWarnings("serial")
private class DrawPanel extends JPanel {
public DrawPanel(){
super();
setFocusable(true);
addKeyListener(new KeyAdapter(){
@Override
//<<<<<<< ManuelCuesta
public void keyPressed(KeyEvent e){
if (e.getKeyCode() == KeyEvent.VK_UP){
dir = Direction.NORTH;
}
else if (e.getKeyCode() == KeyEvent.VK_DOWN){
dir = Direction.SOUTH;
}
else if (e.getKeyCode() == KeyEvent.VK_RIGHT){
dir = Direction.EAST;
}
else if (e.getKeyCode() == KeyEvent.VK_LEFT){
dir = Direction.WEST;
}
else if (e.getKeyCode() == KeyEvent.VK_J) {
dir = jumpPicForDirect(dir);
actionJumpFire = true;
}
else if(e.getKeyCode() == KeyEvent.VK_F){
dir = firePicForDirect(dir);
actionJumpFire = true;
}
}
});
}
//=======
// public void keyPressed(KeyEvent e){
// if (e.getKeyCode() == KeyEvent.VK_UP){
// dir = Direction.NORTH;
// }
// else if (e.getKeyCode() == KeyEvent.VK_DOWN){
// dir = Direction.SOUTH;
// }
// else if (e.getKeyCode() == KeyEvent.VK_RIGHT){
// dir = Direction.EAST;
// }
// else if (e.getKeyCode() == KeyEvent.VK_LEFT){
// dir = Direction.WEST;
// }
// else if (e.getKeyCode() == KeyEvent.VK_J){
// dir = Direction.JUMP_NORTH;
// }
// else if (e.getKeyCode() == KeyEvent.VK_F){
// dir = Direction.WEST;
// }
// }
// });
// }
//>>>>>>> master
public Direction jumpPicForDirect(Direction d) {
if (d == Direction.SOUTH) {
return Direction.JUMPSOUTH;
}
else if (d == Direction.NORTH) {
return Direction.JUMPNORTH;
}
else if (d == Direction.EAST) {
return Direction.JUMPEAST;
}
else if (d == Direction.WEST) {
return Direction.JUMPWEST;
}
else {return d;}
}
public Direction firePicForDirect(Direction d) {
if (d == Direction.SOUTH) {
return Direction.FIRESOUTH;
}
else if (d == Direction.NORTH) {
return Direction.FIRENORTH;
}
else if (d == Direction.EAST) {
return Direction.FIREEAST;
}
else if (d == Direction.WEST) {
return Direction.FIREWEST;
}
else{return d;}
}
protected void paintComponent(Graphics g) {
super.paintComponent(g);
g.setColor(Color.gray);
//<<<<<<< ManuelCuesta
if (getPicDir(dir) < 8) {
picNum = (picNum + 1) % frameCount;
g.drawImage(pics[getPicDir(dir)][picNum], xloc, yloc, Color.gray, this);
} else if (getPicDir(dir) > 7 && getPicDir(dir) < 12) {
picNumJump = (picNumJump + 1) % 8;
g.drawImage(pics[getPicDir(dir)][picNumJump], xloc, yloc, Color.gray, this);
} else {
picNumFire = (picNumFire + 1) % 7;
g.drawImage(pics[getPicDir(dir)][picNumFire], xloc, yloc, Color.gray, this);
}
//=======
// picNum = (picNum + 1) % 8;
// g.drawImage(pics[getPicDir(dir)][picNum], xloc, yloc, Color.gray, this);
//>>>>>>> master
}
public Dimension getPreferredSize() {
return new Dimension(frameStartSize, frameStartSize);
}
}
public BufferedImage[][] getPics() {
return pics;
}
// update location, continue through the subimages(picNum),, and then repaint the image
public void update(int xloc, int yloc, Direction d) {//, JFrame frame) {
this.xloc = xloc;
this.yloc = yloc;
dir = d;
}
}