{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Gui - package sjm Project Simple Java [email protected]

Info icon This preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
package sjm; /** * Project Simple Java Minesweeper * @author Laurens Van Keer | www.LaVaKe.be * @version 1.1 */ import javax.swing.*; import java.awt.*; import java.awt.event.*; i /** * Gui is part of the "Simple Java Minesweeper" (SJM) application. * * Gui is the "Graphical User Interface" of SJM. * This class handles all graphical stuff. */ public class Gui extends JFrame implements MouseListener { p private static final long serialVersionUID = -7860274770183768002L; p // The game and it's settings. private Game game; p // The buttons. private JButton[][] buttons = new JButton[10][10]; p // Time stuff. private JLabel timeLeftLabel; private Thread timer; p // Scores. private JLabel[][] lastScoresLabel; p // Frame settings private final String FRAME_TITLE = "Simple Java Minesweeper ~ By Laurens Van Keer (www.lavake.be)"; 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; p /** * 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); s // Initialize the time left label. setTimeLeftLabel( new JLabel(Integer.toString(this.game.getTimeLeft()) + " seconds") ); +
Image of page 1

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

View Full Document Right Arrow Icon
showWelcomeMessage(); s // The layout of the frame: Container contentPane; JPanel gameBoard; JPanel controlPanel; JPanel timePanel; JPanel scorePanel; JPanel buttonPanel; J // Set frame. setSize(FRAME_WIDTH, FRAME_HEIGHT); setTitle(FRAME_TITLE); setLocation(FRAME_LOC_X, FRAME_LOC_Y); s // Set layout. contentPane = getContentPane(); contentPane.setLayout(new BorderLayout(10,0)); c // 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()); c // Build the "timepanel". timePanel = new JPanel(); timePanel.setBorder(BorderFactory.createTitledBorder("Time left:")); timePanel.add(timeLeftLabel); controlPanel.add(timePanel, BorderLayout.NORTH); c // Build the "scorepanel" (part of controlPanel). scorePanel = new JPanel(); scorePanel.setBorder(BorderFactory.createTitledBorder("Last scores:")); scorePanel.setLayout(new GridLayout(4,2)); s // Add and initialize last scores labels. lastScoresLabel = new JLabel[4][2]; initScores(); i for(int i=0; i<lastScoresLabel.length; i++) {
Image of page 2
scorePanel.add(lastScoresLabel[i][0]); scorePanel.add(lastScoresLabel[i][1]); } controlPanel.add(scorePanel, BorderLayout.CENTER); c // Build the "buttonpanel" (part of controlPanel). buttonPanel = new JPanel(); b // Add "new game" button. JButton newGameButton = new JButton("New game"); newGameButton.setName("newGameButton"); newGameButton.addMouseListener(this); buttonPanel.add(newGameButton); b // Add "quit game" button.
Image of page 3

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

View Full Document Right Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern