Lab session 7

Assignment

The goal of this lab session is to implement a GUI (graphical user interface) for the LCR game you implemented during the previous assignment.

The GUI should consist of the following components:

Here are some examples of how the GUI should look at the end of this assignment:

It is not important that your GUI matches these images precisely, but it should display roughly the same information.

Tip: It is possible you need to add more accessors (‘getters’) to the classes you implemented in the previous assignment.

Tip: During the implementation, think about what is the best way to organize your code to avoid repetition and make the code easy to understand.

Step 1: Creating a window

Start by creating a copy of your solution to session 6. Create a new class GraphicalLCR that extends JFrame. In the constructor, you should set the window size, title, default close operation, and visibility.

Create a new JPanel called mainPanel and add it to the frame. All other components in the GUI you add in the next steps should be added to this mainPanel (using the add(…) method), either directly or indirectly.

Then create the main method and in it create a new object of type GraphicalLCR. If you run the program, it should now display an empty window.

Step 2: Adding the game state.

Add a (private) instance variable of type LCRGame to GraphicalLCR, which represents the current game state. The constructor for GraphicalLCR should take the initial value of this variable as an argument.

In the main method, create a new object of type LCRGame and pass it to the constructor.

Step 3: Displaying the players.

Now create a new JPanel called playersPanel and add it to the main panel. To the playersPanel, add labels displaying for each player their name and current score.

Step 4: Displaying the dice.

Repeat step 3 for the dice of the LCRGame: create a new JPanel called dicePanel and display the dice in it. Make sure you only display the dice that were actually rolled in the last round! (If this information is not available, you can change one of the classes you implemented previously to add this information).

Step 5: Adding the buttons.

Create a third panel called buttonsPanel and add two buttons to it: one labeled ‘Next round’ and one labeled ‘Exit’.

Create and add a new EventHandler to each of these buttons.

When the game is finished, the ‘Next round’-button should be disabled (using the method setEnabled(false)).

Step 6: Displaying the current status

Add a label to the GUI that displays the current status of the game:

Step 7 (optional): Make it look nice

Once you get the basic functionality of the GUI to work, you can try to improve the layout and display of the different elements. Here are a few things you could try:

Menu