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:
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.
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.
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.
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.
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).
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)
).
Add a label to the GUI that displays the current status of the game:
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:
You can change the layout of the different components within the mainPanel
using the setLayout
method, for example as follows:
mainPanel.setLayout(new BoxLayout(mainPanel, BoxLayout.Y_AXIS));
buttonsPanel.setLayout(new GridLayout(0,1));
You can experiment with different layouts until you find one you like.
You can add a border with some text around a JPanel
using the setBorder
method. For example:
You can change the background color of a JPanel
using the setBackgroundColor
method. For example: