-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathView.java
More file actions
112 lines (95 loc) · 3.02 KB
/
View.java
File metadata and controls
112 lines (95 loc) · 3.02 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
import java.awt.Color;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import javax.imageio.ImageIO;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
/**
* 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?)
**/
class View extends JPanel {
final private int width, height, imageWidth, imageHeight;
public JButton button;
private JFrame frame;
private OrcImage action;
private OrcImage prevAction;
private static HashMap<OrcImage, BufferedImage[]> images;
private int x, y, xDir, yDir; //need global state attribute information for the repaint method
private int picNum = 0;
BufferedImage[] pics;
//Read image from file and return
private BufferedImage createImage(OrcImage image) {//String path){
BufferedImage bufferedImage;
try {
bufferedImage = ImageIO.read(new File(image.path()));
return bufferedImage;
} catch (IOException e) {
e.printStackTrace();
}
return null;
// TODO: Change this method so you can load other orc animation bitmaps
}
View(JButton button){
setFocusable(true); // necessary to take key inputs
this.width=500;
this.height=300;
this.imageWidth=165;
this.imageHeight=165;
this.frame = new JFrame();
this.frame.add(this);
this.button = button;
this.add(this.button);
images = new HashMap<OrcImage,BufferedImage[]>();
for(OrcImage orcImage : OrcImage.values()) { //use an enum to map directions to images
BufferedImage img = createImage(orcImage);
pics = new BufferedImage[orcImage.frameCount()];
for(int i = 0; i < orcImage.frameCount(); i++)
pics[i] = img.getSubimage(this.imageWidth*i, 0, this.imageWidth, this.imageHeight);
//System.out.println(images+","+orcImage+","+pics);
images.put(orcImage,pics);
}
//this.frame.setSize(100,100);
this.action = OrcImage.FORWARD_S;
this.frame.setBackground(Color.gray);
this.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.frame.setSize(this.width, this.height);
this.frame.setVisible(true);
}
public int getWidth(){
return this.width;
}
public int getHeight(){
return this.height;
}
public int getImageWidth(){
return this.imageWidth;
}
public int getImageHeight(){
return this.imageHeight;
}
public void update(Model model){
this.removeAll();
this.action = model.getAction();
this.x = model.getX();
this.y = model.getY();
if(this.action!=this.prevAction && this.picNum!=0) { //if there is a new action to perform
picNum=0;//reset the animation
}
setBackground(Color.gray);
this.picNum = (this.picNum + 1) % this.action.frameCount();
this.frame.getGraphics().drawImage(this.images.get(this.action)[this.picNum],this.x,this.y,Color.gray, this);
this.add(this.button);
this.button.setVisible(true);
this.button.repaint();
this.prevAction = this.action;
}
}