ICT-4361 Homework 4


This exercise will familiarize you with the basic graphics functionality of Java, using AWT and Swing.
Our approach will be to display the dice throws from our previous homework, and display them graphically.
For this exercise, we will restrict ourselves to displaying one face of 6-side dice. To extend the exercise, think about how you might handle other kinds of dice!

What to Hand In

Please hand in a listing for each program requested, formatted in an easy-to-read style.
Ensure your name, and the name of the file is available in a comment at the top of the file.
Also, ensure that you have a sample of the output from the program.
If your program fails to compile, hand in your error listing as your output.
For each question asked, provide one or two sentences summarizing your answer. Please be both complete and succinct.


  1. Make a graphical dice panel. If properly made, this Dice can be used and embedded in any of your game applications.
    Remember, you already have a Dice class, and this panel will leverage it.
    1. Create a class called DiceView which extends JPanel and implements ActionListener.
    2. Create a private data member to hold an object of type Dice
    3. Create a setter method for the Dice object.
    4. Create a public method for the ActionListener called actionPerformed which does the following:
      • Checks if the ActionEvent action command is "Roll"
      • If so, check the Dice instance to ensure it is non-null
      • If the dice is non-null, roll() the dice
      • Call repaint()
    5. Create a public method for the component called paintComponent, which does the following:
      • Gets the size of the JPanel and sets local variables width and height
      • Clears the rectangular area of the JPanel
      • Draws a rectangle (or rounded rectangle), preferably a having equal sides, to fill most of the area in the JPanel (this represents the side of the dice)
      • If the dice is not null, displays the value of the dice within that rectangle
      • If the dice is null, displays the text string "No Dice" in that rectangle
    6. Create a main program to test out your JPanel. You may use the following program, or modify it for your own purposes:
      public static void main(String[] args) {
          JFrame frame = new JFrame();
          JButton button = new JButton("Roll");
          DiceView dv = new DiceView();
          Dice d = new Dice();
          // To test what happens with no dice, comment out the next line



Criteria Weight
Create a proper DiceView class 10%
Create a proper data member for dice 10%
Create a proper setter method for the dice 10%
Create a poper actionPerformed method 25%
Create a proper paintComponent method 25%
Create a proper main program and output 20%