-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGameView.java
More file actions
175 lines (136 loc) · 5.71 KB
/
GameView.java
File metadata and controls
175 lines (136 loc) · 5.71 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
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 implements ActionListener{
/**
* 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 JLabel scoreLabel;
/**
* references to buttons Undo/Redo
*/
private JButton buttonUndo, buttonRedo;
/**
* Constructor used for initializing the Frame
*
* @param model
* the model of the game (already initialized)
* @param gameController
* the controller
*/
public GameView(GameModel model, GameController gameController) {
super("Flood it -- the ITI 1121 version");
this.gameModel = model;
this.gameController = gameController;
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setBackground(Color.WHITE);
JPanel panel = new JPanel();
panel.setBackground(Color.WHITE);
panel.setLayout(new GridLayout(gameModel.getSize(), gameModel.getSize()));
panel.setBorder(BorderFactory.createEmptyBorder(5, 20, 5, 20));
board = new DotButton[gameModel.getSize()][gameModel.getSize()];
for (int row = 0; row < gameModel.getSize(); row++) {
for (int column = 0; column < gameModel.getSize(); column++) {
board[row][column] = new DotButton(row, column, gameModel.getColor(row,column),
(gameModel.getSize() < 26 ? DotButton.MEDIUM_SIZE : DotButton.SMALL_SIZE));
panel.add(board[row][column]);
board[row][column].addActionListener(gameController);
}
}
add(panel, BorderLayout.CENTER);
buttonUndo = new JButton("Undo");
buttonUndo.setFocusPainted(false);
buttonUndo.setEnabled(false);
buttonUndo.addActionListener(gameController);
buttonRedo = new JButton("Redo");
buttonRedo.setFocusPainted(false);
buttonRedo.setEnabled(false);
buttonRedo.addActionListener(gameController);
JButton buttonReset = new JButton("Reset");
buttonReset.setFocusPainted(false);
buttonReset.addActionListener(gameController);
JButton buttonExit = new JButton("Quit");
buttonExit.setFocusPainted(false);
buttonExit.addActionListener(gameController);
JPanel controlPanel = new JPanel();
controlPanel.setBackground(Color.WHITE);
scoreLabel = new JLabel();
controlPanel.add(scoreLabel);
controlPanel.add(buttonReset);
controlPanel.add(buttonExit);
add(controlPanel, BorderLayout.SOUTH);
JButton settings = new JButton("Settings");
settings.addActionListener(this);
JPanel northPanel = new JPanel();
northPanel.setBackground(Color.WHITE);
northPanel.add(buttonUndo);
northPanel.add(buttonRedo);
northPanel.add(settings);
add(northPanel, BorderLayout.NORTH);
pack();
//setResizable(false);
setVisible(true);
}
/**
* update the status of the board's DotButton instances based on the current game model
*/
public void update(){
for(int i = 0; i < gameModel.getSize(); i++){
for(int j = 0; j < gameModel.getSize(); j++){
board[i][j].setColor(gameModel.getColor(i,j));
}
}
if(!gameModel.isInitialSet()) {
scoreLabel.setText("Select initial dot");
} else {
scoreLabel.setText("Number of steps: " + gameModel.getNumberOfSteps());
}
buttonUndo.setEnabled(gameModel.canUndo());
buttonRedo.setEnabled(gameModel.canRedo());
repaint();
}
public void actionPerformed(ActionEvent e) {
if (e.getSource() instanceof JButton) {
JButton clicked = (JButton)(e.getSource());
if (clicked.getText().equals("Settings")) {
JPanel panel = new JPanel();
panel.setLayout(new GridLayout(6,1));
JRadioButton planeButton = new JRadioButton("Plane",!gameModel.isTorusMove());
planeButton.addActionListener(gameController);
JRadioButton torusButton = new JRadioButton("Torus",gameModel.isTorusMove());
torusButton.addActionListener(gameController);
JRadioButton ortButton = new JRadioButton("Orthogonal",!gameModel.isDiagMove());
ortButton.addActionListener(gameController);
JRadioButton diagButton = new JRadioButton("Diagonals",gameModel.isDiagMove());
diagButton.addActionListener(gameController);
ButtonGroup group1 = new ButtonGroup();
group1.add(planeButton);
group1.add(torusButton);
ButtonGroup group2 = new ButtonGroup();
group2.add(ortButton);
group2.add(diagButton);
panel.add(new JLabel("Play on plane or torus?"));
panel.add(planeButton);
panel.add(torusButton);
panel.add(new JLabel("\n Diagonal moves?"));
panel.add(ortButton);
panel.add(diagButton);
JOptionPane.showMessageDialog(this, panel);
}
}
}
}