import javax.swing.*; import java.awt.*; import java.awt.event.*; public class MineSwingGui implements MineGuiInterface { MineController mc; boolean hasWon = false; int rows, cols, mines; int minesMarked = 0; JButton[][] gridButtons; JLabel winText; MineSwingGui(MineController mc) { this.mc = mc; try { UIManager.setLookAndFeel( UIManager.getCrossPlatformLookAndFeelClassName() ); } catch (Exception e) { System.exit(1); } } /** * Det f?rste vinduet som vises, hvor brukeren kan velge st?rrelse * p? brettet og antall miner */ @Override public void start() { JFrame inputWindow = new JFrame("Minesveiper"); inputWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel buttonPanel = new JPanel(new GridLayout(1, 3)); JPanel textColumn = new JPanel(new GridLayout(3, 1)); JLabel rowText = new JLabel("Antall rader:"); JLabel colText = new JLabel("Antall kolonner:"); JLabel mineText = new JLabel("Antall miner:"); textColumn.add(rowText); textColumn.add(colText); textColumn.add(mineText); buttonPanel.add(textColumn); JPanel inputColumn = new JPanel(new GridLayout(3, 1)); JTextField rowsField = new JTextField("1"); JTextField colsField = new JTextField("1"); JTextField minesField = new JTextField("1"); inputColumn.add(rowsField); inputColumn.add(colsField); inputColumn.add(minesField); buttonPanel.add(inputColumn); JButton startButton = new JButton("Start"); startButton.addActionListener(new StartKnapp(inputWindow, rowsField, colsField, minesField)); buttonPanel.add(startButton); inputWindow.add(buttonPanel); inputWindow.pack(); inputWindow.setLocationRelativeTo(null); inputWindow.setVisible(true); } class StartKnapp implements ActionListener { JFrame startWindow; JTextField rowsField, colsField, minesField; StartKnapp(JFrame startWindow, JTextField rows, JTextField cols, JTextField mines) { this.startWindow = startWindow; this.rowsField = rows; this.colsField = cols; this.minesField = mines; } @Override public void actionPerformed(ActionEvent e) { try { rows = Integer.parseInt(rowsField.getText()); cols = Integer.parseInt(colsField.getText()); mines = Integer.parseInt(minesField.getText()); } catch (Exception ex) {} startWindow.dispose(); gameScreen(); } } /** * Vinduet som inneholder selve spillet */ public void gameScreen() { mc.createModel(rows, cols, mines); JFrame gameWindow = new JFrame("Minesveiper"); gameWindow.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); JPanel gamePanel = new JPanel(new BorderLayout()); JPanel textPanel = new JPanel(new GridLayout(1, 2)); JLabel minesMarkedText = new JLabel(String.format("%d/%d miner markert", minesMarked, mines)); winText = new JLabel("..."); textPanel.add(minesMarkedText); textPanel.add(winText); gamePanel.add(textPanel, BorderLayout.NORTH); JPanel grid = new JPanel(new GridLayout(rows, cols)); gridButtons = new JButton[rows][cols]; for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { gridButtons[i][j] = new JButton(mc.getSquareText(i, j)); gridButtons[i][j].setPreferredSize(new Dimension(60, 60)); gridButtons[i][j].addActionListener(new GridButton(i, j, minesMarkedText)); grid.add(gridButtons[i][j]); } } gamePanel.add(grid, BorderLayout.SOUTH); gameWindow.add(gamePanel); gameWindow.pack(); gameWindow.setLocationRelativeTo(null); gameWindow.setVisible(true); } class GridButton implements ActionListener { int row, col; boolean marked = false; boolean opened = false; JLabel minesMarkedText; GridButton(int row, int col, JLabel minesMarkedText) { this.row = row; this.col = col; this.minesMarkedText = minesMarkedText; } @Override public void actionPerformed(ActionEvent e) { if (!opened && ((e.getModifiers() & ActionEvent.CTRL_MASK) == ActionEvent.CTRL_MASK)) { if (marked) { marked = false; minesMarked--; } else { marked = true; minesMarked++; } mc.toggleSquare(row, col); minesMarkedText.setText(String.format("%d/%d miner markert", minesMarked, mines)); } else if (!opened) { mc.openSquare(row, col); } updateButtonTexts(); } } /** * Oppdaterer teksten p? alle spillknappene */ void updateButtonTexts() { for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { gridButtons[i][j].setText(mc.getSquareText(i, j)); } } } /** * Deaktiverer spillknappene ved spillets slutt */ void disableButtons() { for (int i = 0; i < rows; i++) { for (int j = 0; j < cols; j++) { if(mc.isMine(i, j)) { mc.setStatus(i, j, 2); } gridButtons[i][j].setEnabled(false); } } } /** * Aktiveres ved spillets slutt */ @Override public void hasWon(boolean status) { disableButtons(); if (status) { winText.setText("Gratulerer!"); } else { winText.setText("Game over."); } } }