/** * Constructor that takes model as a parameter * * @param model */ public BoardFrame(Model model) { this.model = model; data = model.getData(); gameOver = false; initScreen(); setLayout(new BorderLayout()); setLocation(300, 200); // undoBtn is a controller that resets the turn and last state of board undoBtn = new JButton("Undo : " + this.model.getUndoCounter()); undoBtn.setPreferredSize(new Dimension(80, 50)); undoBtn.addActionListener( new ActionListener() { public void actionPerformed(ActionEvent e) { if (BoardFrame.this.model.getUndoCounter() != 0) { BoardFrame.this.model.undo(); undoBtn.setText("Undo : " + BoardFrame.this.model.getUndoCounter()); } else ; } }); JPanel undoPanel = new JPanel(); undoPanel.add(undoBtn); // end of undoBtn code add(boardPanel, BorderLayout.CENTER); add(undoPanel, BorderLayout.SOUTH); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); pack(); setResizable(false); setVisible(true); }
/** * Is called from the model when data is changed, updates members and view of this class. Checks * if the game is over. * * @param e ChangeEvent object */ public void stateChanged(ChangeEvent e) { // update the view: accessors and repaint data = model.getData(); repaint(); if (model.isFinish() && !gameOver) { gameOver = true; JFrame frame = new JFrame("Declare Winner"); if (model.declareWinner() == 1) JOptionPane.showMessageDialog(frame, "A is the winner!!!"); else if (model.declareWinner() == 2) JOptionPane.showMessageDialog(frame, "B is the winner!!!"); else JOptionPane.showMessageDialog(frame, "It is a tie!!!"); } }