import java.awt.GridLayout; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.*; @SuppressWarnings("serial") public class GameGUIView extends JPanel implements ActionListener { GameModel model; GameController controller; int height, width; JCheckBox[][] checkBoxes; public GameGUIView(GameModel model, GameController controller) { this.model = model; this.controller = controller; boolean[][] field = model.getField(); height = field.length; width = field[0].length; this.setLayout(new GridLayout(height, width)); checkBoxes = new JCheckBox[height][width]; for (int i = 0; i < height; i++) { for (int j = 0; j < width; j++) { checkBoxes[i][j] = new JCheckBox(); this.add(checkBoxes[i][j]); checkBoxes[i][j].addActionListener(this); } } redraw(); } public void redraw() { boolean[][] field = model.getField(); for (int i = 0; i < height; i++) { for (int j = 0; j < width; j++) { checkBoxes[i][j].setSelected(field[i][j]); } } } @Override public void actionPerformed(ActionEvent e) { for (int i = 0; i < height; i++) { for (int j = 0; j < width; j++) { if (e.getSource() == checkBoxes[i][j]) { controller.receiveAction(i, j); } } } redraw(); } }