Gui - package minesweeper; import javax.swing.*; import...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
package minesweeper; import javax.swing.*; import java.awt.*; import java.awt.event.*; /** * * @author lloydwilkerson */ public class Gui JFrame implements MouseListener{ private static final long serialVersionUID = -7860274770183768002L; // The game and it's settings. private Game game; // The buttons. private JButton[][] buttons = new JButton[10][10]; // Time stuff. private JLabel timeLeftLabel; private Thread timer; // Scores. private JLabel[][] lastScoresLabel; // Frame settings private final String FRAME_TITLE = "CIS3023 Minesweeper"; private final int FRAME_WIDTH = 700; private final int FRAME_HEIGHT = 500; private final int FRAME_LOC_X = 100; private final int FRAME_LOC_Y = 100; /** * Gui constructor * A Game object needs to be specified. * The frame is initialized, but it's still up to the user (Main) to set it visible. * * @param minefield */ public Gui(Game game) { // Set the game. setGame(game); // Initialize the time left label. setTimeLeftLabel( new JLabel(Integer.toString(this.game.getTimeLeft()) + " seconds") ); showWelcomeMessage(); // The layout of the frame: Container contentPane; JPanel gameBoard; JPanel controlPanel; JPanel timePanel; JPanel scorePanel; JPanel buttonPanel;
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
setSize(FRAME_WIDTH, FRAME_HEIGHT); setTitle(FRAME_TITLE); setLocation(FRAME_LOC_X, FRAME_LOC_Y); // Set layout. contentPane = getContentPane(); contentPane.setLayout(new BorderLayout(10,0)); // Build the "gameboard". gameBoard = new JPanel(); gameBoard.setLayout(new GridLayout(10,10)); // Rows for( int y=0 ; y<10 ; y++ ) { // Columns for( int x=0 ; x<10 ; x++ ) { // Set button text. buttons[x][y] = new JButton(this.game.getMinefield().getMinefield()[x][y].getContent()); // Set button name (x,y). buttons[x][y].setName(Integer.toString(x) + "," + Integer.toString(y)); // Add mouseListener. buttons[x][y].addMouseListener(this); // Add this button to the gameboard. gameBoard.add(buttons[x][y]); } } // Build the "controlpanel". controlPanel = new JPanel(); controlPanel.setLayout(new BorderLayout()); // Build the "timepanel". timePanel = new JPanel(); timePanel.setBorder(BorderFactory.createTitledBorder("Time left:")); timePanel.add(timeLeftLabel); controlPanel.add(timePanel, BorderLayout.NORTH); // Build the "scorepanel" (part of controlPanel). scorePanel = new JPanel(); scorePanel.setBorder(BorderFactory.createTitledBorder("Last scores:")); scorePanel.setLayout(new GridLayout(4,2)); // Add and initialize last scores labels. lastScoresLabel = new JLabel[4][2]; initScores(); for(int i=0; i<lastScoresLabel.length; i++) { scorePanel.add(lastScoresLabel[i][0]); scorePanel.add(lastScoresLabel[i][1]); } controlPanel.add(scorePanel, BorderLayout.CENTER); // Build the "buttonpanel" (part of controlPanel). buttonPanel = new JPanel();
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 12

Gui - package minesweeper; import javax.swing.*; import...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online