-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathGameView.java
151 lines (139 loc) · 4.4 KB
/
GameView.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
/*
* Name: GameView
* Beschreibung: Explaination
*
*
*/
import javax.swing.*;
import java.awt.*;
import java.awt.event.ComponentAdapter;
import java.awt.event.ComponentEvent;
import java.awt.image.BufferedImage;
import java.util.List;
public class GameView extends JPanel {
private JFrame window;
private GameModel model;
/**
* the constructor of the GameView class. It creates a new GameView using a
* title, width, height and a boolean to either use a free ratio of width and height,
* or lock it.
* @param title
* the title of the window
* @param width
* integer value of the view width
* @param height
* integer value of the view height
* @param fixedRatio
* boolean to either allow a free width/height ratio or lock it
* */
public GameView(String title, int width, int height, boolean fixedRatio) {
super(true);
window = new JFrame(title);
if(fixedRatio) {
window.addComponentListener(new ComponentAdapter() {
@Override
public void componentResized(ComponentEvent e) {
int paneWidth = window.getContentPane().getWidth();
int windowWidth = window.getWidth();
int borderWidth = windowWidth-paneWidth;
int paneHeight = window.getContentPane().getHeight();
int windowHeight = window.getHeight();
int borderHeight = windowHeight-paneHeight;
int stretchFactor = 1;
if (window.getWidth() >= window.getHeight()*3) {
stretchFactor = 2;
}
int factor = (paneWidth-(28*stretchFactor))/(28*stretchFactor)+1;
Dimension dim = new Dimension(factor*28*stretchFactor+borderWidth, factor*14+borderHeight);
if (!dim.equals(window.getSize())) {
window.setPreferredSize(dim);
window.pack();
}
}
});
}
window.add(this);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
window.setResizable(false);
window.setVisible(true);
window.setPreferredSize(new Dimension(width, height));
window.pack();
this.setBackground(Color.BLACK);
this.setFocusable(true);
this.requestFocusInWindow();
}
/**
* a variation of the default GameView constructor.
* whenever you call this one, the fixedRatio boolean is set to
* false by default.
*@param title
* the title of the window
* @param width
* integer value of the view width
* @param height
* integer value of the view height
* */
public GameView(String title, int width, int height) {
this(title, width, height, false);
}
/**
* returns the active JFrame of this view
* */
public JFrame getFrame() {
return this.window;
}
/**
* the overridden paint function of the jframe;
* it gets every GameObject it needs to draw using the
* getObjects() method from the GameModel and creates
* a rectangle for every object using a for loop to iterate
* through the list.
* */
@Override
public void paint(Graphics g) {
super.paint(g);
List<GameObject> objects = model.getObjects();
int stretchFactor = 1;
if (this.getWidth() >= this.getHeight()*4) {
stretchFactor = 2;
}
for (GameObject gameObject : objects) {
if (gameObject.isVisible()) {
int x = (int) (gameObject.getX() * this.getWidth() / 28)/stretchFactor;
int y = (int) ((14 - gameObject.getY()) * this.getHeight() / 14);
int w = (int) (gameObject.getWidth() * this.getWidth() / 28)/stretchFactor;
int h = (int) ((gameObject.getHeight()) * this.getHeight() / 14);
g.setColor(gameObject.getColor());
g.fillRect(x, y - h, w, h);
}
}
}
/**
* updates the currently active model by replacing its state
* with the parsed model's state
* @param model
* the model which is used to replace the current state
* */
public void update(GameModel model) {
this.model = model; //thoeretisch können 2 Models auf der Selben view angezeigt werden.
repaint();
}
/**
* reset method for the view, this just sets the
* background color to black.
* */
public void reset() {
this.setBackground(Color.BLACK);
}
/**
* returns a buffered image of the current view;
* this is later used to get all RGB values and parse
* the view into the array which is sent to the lighthouse.
* */
public BufferedImage getViewImage() {
BufferedImage bi = new BufferedImage(28, 14, BufferedImage.TYPE_INT_RGB);
Graphics2D g = bi.createGraphics();
this.paint(g);
return bi;
}
}