In this lab, you will get your View to correctly display the Model and the game will work with basic features implemented.
Your first goal is to have your View display the model. In the previous lab, you already chose a type of View container to display images. The two most common are:
The View is one large Group that displays the board by calculating the position of each tile, and then draws whatever image is required at each location based on the Model data. When the user clicks the Group, you must calculate which cell was clicked and tell the Model to reveal that cell, Flag it, etc. This is the style that was used in GridModel for the Life program.
The View is a custom GridPane class that stores a 2D array of ImageView objects that know their (row, col) position in the GridPane. In this style, you would update View by updating each ImageView to reflect the Model. When the user clicks any of the ImageView objects, they will automatically know their own location which you can then pass on to the Model. Or, instead of using custom ImageView objects you can use standard ImageView objects with GridPane.getRowIndex()
and GridPane.getColumnIndex()
on the ImageView that was selected.
First you need to decide if you want to add mouse listeners to your entire View itself or to each ImageView within the View. It can be done eithe way.
Once you know where the user clicked you can ask the Model questions and take action. For example, if the cell clicked is not revealed and is not a flag or question mark, then call model.reveal(row, col)
on that cell. Then call updateView()
to update what the user sees.
Now it's time to connect your View and Model via the Controller.
void updateView() {
loop through each row/col of the View (double loop)
// Ask the model questions and update the View. For example:
if (model.isCovered(row, col)) {
if (model.isFlagged(row, col))
Set the image at (row, col) to FLAG image in the View
You can change an ImageView's image using setImage()
else set image to blank tile
}
else if (!model.isCovered(row, col) {
...
Note 1
When changing images, don't create a new ImageView. Just update the image of the existing ImageView at that location using the setImage() method of ImageView.
Note 2
If you can't ask the View what ImageView is at a (row, col) location then keep your ImageViews in a separate 2D array of ImageView objects. Then you can always access them.
EventHandler Tips
MouseEvent Tips
If you have one EventHandler handling multiple types of MouseEvents (ex: mouseReleased vs mouseClicked) you can distinguish them by asking what kind of event the parameter is. For example:
if (e.getEventType() == MouseEvent.MOUSE_PRESSED)
You can find out which mouse button was used during a MouseEvent by asking something like
if (e.getButton() == MouseButton.PRIMARY)
Your program needs the required items listed below.
For this lab, you must implement an 'About game' option in the Help menu. Click here to see an example. To accomplish this, when the user selects 'About game' do the following:
.close
method of the Stage class.Pro Tip: If you want to block the user from interacting with the game window while the About window is showing, then use the .initOwner(Stage) method of the Stage class along with the .initModality(Modality.WINDOW_MODAL) method of the Stage class. This will force the user to press your 'Ok' button before continuing their game.
PX_LastName_FirstName_MSModelInterface.java
that specifies what methods the model must havePX_LastName_FirstName_MinesweeperModel.java
that stores the raw 2D data and handles operations on that dataPX_LastName_FirstName_MinesweeperGUI.java
that sets up the main GUI.View
is up to you.
MinesweeperGUI
Controller
code is up to you.
MinesweeperGUI
Controller Code
The Controller is the part of your code that handles input events and passes information between the Model and View. In the Console version of Minesweeper, your controller had a play() loop that asked for user input and then updated the model and view accordingly. In the GUI version, you don't need a loop. The GUI itself is the loop and it already sits there waiting for you to handle user input. You should get rid of your old play() loop and copy the same ideas into your GUI version. For example, when the user left-clicks on the board, you should call reveal(row, col)
where the user clicked if that cell is in bounds and hasn't been revealed.
PX_LastName_FirstName_MinesweeperBasics.zip
If your file size is over the limit, then then submit just your .java files and talk to your teacher about why your project is so large. You can probably resolve this yourself by simply scaling your images to reasonable sizes using an image editor (there are free image editors online).
You must Sign In to submit to this assignment
Last modified: March 29, 2024
Back to Lab 7.3 Minesweeper Gui Template