-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGameView.java
executable file
·193 lines (142 loc) · 5.81 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
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
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.*;
/**
* The class <b>GameView</b> provides the current view of the entire Game. It extends
* <b>JFrame</b> and lays out an instance of <b>BoardView</b> (the actual game) and
* two instances of JButton. The action listener for the buttons is the controller.
*
* @author Guy-Vincent Jourdan, University of Ottawa
*/
public class GameView extends JFrame {
/**
* The board is a two dimensionnal array of DotButtons instances
*/
private DotButton[][] board;
/**
* Reference to the model of the game
*/
private GameModel gameModel ;
private GameController gameController ;
private JPanel panel ;
private JLabel scoreLabel ;
private JButton undo ;
private JButton redo ;
private int iconSize ;
/**
* Constructor used for initializing the Frame
*
* @param model
* the model of the game (already initialized)
* @param gameController
* the controller
*/
public GameView(GameModel gameModel, GameController gameController) {
super("Flood it -- the ITI 1121 version");
this.gameModel = gameModel;
this.gameController = gameController;
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBackground(Color.WHITE);
panel = new JPanel();
panel.setBackground(Color.WHITE);
panel.setLayout(new GridLayout(gameModel.getSize(), gameModel.getSize()));
panel.setBorder(BorderFactory.createEmptyBorder(20, 20, 0, 20));
board = new DotButton[gameModel.getSize()][gameModel.getSize()];
for (int row = 0; row < gameModel.getSize(); row++) {
for (int column = 0; column < gameModel.getSize(); column++) {
iconSize = (gameModel.getSize() < 26 ? DotButton.MEDIUM_SIZE : DotButton.SMALL_SIZE) ;
board[row][column] = new DotButton(row, column, gameModel.getColor(row,column), iconSize);
board[row][column].addActionListener(gameController);
panel.add(board[row][column]);
}
}
add(panel, BorderLayout.CENTER);
JButton buttonReset = new JButton("Reset");
buttonReset.setFocusPainted(false);
buttonReset.addActionListener(gameController);
JButton buttonExit = new JButton("Quit");
buttonExit.setFocusPainted(false);
buttonExit.addActionListener(gameController);
JButton settings = new JButton("Settings");
settings.setFocusPainted(false);
settings.addActionListener(gameController);
undo = new JButton("Undo");
undo.setFocusPainted(false);
undo.addActionListener(gameController);
redo = new JButton("Redo");
redo.setFocusPainted(false);
redo.addActionListener(gameController);
JPanel control = new JPanel();
control.setBackground(Color.WHITE);
scoreLabel = new JLabel();
control.add(scoreLabel);
control.add(buttonReset);
control.add(buttonExit);
JPanel upperControl = new JPanel();
upperControl.add(undo) ;
upperControl.add(redo) ;
upperControl.add(settings) ;
upperControl.setBackground(Color.WHITE) ;
add(upperControl, BorderLayout.NORTH) ;
JPanel southPanel = new JPanel();
southPanel.setLayout(new GridLayout(2,1));
southPanel.add(control);
southPanel.setBorder(BorderFactory.createEmptyBorder(20, 10, 0, 10));
southPanel.setBackground(Color.WHITE);
add(southPanel, BorderLayout.SOUTH);
pack();
setVisible(true);
}
/**
* update the status of the board's DotButton instances based on the current game model
*/
public void update() {
gameModel = gameController.getGameModel() ;
if (!gameController.getUndone() && !gameController.getRedone()) {
System.out.println("modifying... (performing new modification)") ;
for(int i = 0; i < gameModel.getSize(); i++) {
for(int j = 0; j < gameModel.getSize(); j++) {
board[i][j].setColor(gameModel.getColor(i,j)) ;
}
}
}
else {
if (gameController.getModelStack().getSize() > 1) {
System.out.println("modifying... (loading last state)") ;
for(int i = 0; i < gameModel.getSize(); i++) {
for(int j = 0; j < gameModel.getSize(); j++) {
if (gameModel.isCaptured(i, j)) {
board[i][j].setColor(gameModel.getColor(i,j)) ;
}
}
}
}
}
System.out.println("---------------------------------------------------------------") ;
// System.out.println("change: " + gameController.getChange()) ;
// System.out.println(gameController.getModelStack().getSize()) ;
if (gameController.getModelStack().getSize() != 1) {
undo.setEnabled(true) ;
}
else {
undo.setEnabled(false) ;
}
if (!gameController.getUndoModelStack().isEmpty() && !gameController.getChange()) {
redo.setEnabled(true) ;
}
else {
redo.setEnabled(false) ;
}
// System.out.println(gameModel.getNumberOfSteps()) ;
System.out.println("NumberOfSteps: " + gameController.getGameModel().getNumberOfSteps()) ;
if (gameModel.getNumberOfSteps() == -1) {
scoreLabel.setText("Select initial dot ") ;
}
else {
scoreLabel.setText("Number of steps: " + gameModel.getNumberOfSteps()) ;
}
repaint() ;
System.out.println("**************************************************************") ;
}
}