Watch this space for later updates.
You are to create the GUI (Graphical User Interface) for a simple game involving colors. The goal for this homework is to gain some experience with the Java GUI and with event-driven programming.
We plan to grade the homework based on your electronic submission. The hardcopy is required mainly for backup.
You are given the code for ColorGame.java; this is a small class for a color-guessing game. When created, a ColorGame picks a random goal color. The isMatch() method checks to see if a guess color matches the goal color. The goal color and the most-recent guess color can be accessed from outside the class in order to create a GUI for this game. Colors are specified by giving their red, green, and blue components. The range field (also accessible from outside the class) indicates the maximum size int that can be used to specify a color component. Possible values for the range are 4, 8, or 16 depending on whether the user chose to start an easy, medium, or difficult game, respectively. Your job is to create a GUI for this game. Here is a picture of the GUI.
The idea is to move the sliders to make the color in the center square match the color surrounding it.
Most of the code that you will need to create is related to layout. The event-handling code (for the listeners) is about 80 lines long, including comments (and blank lines inserted for readability). The total code for the entire game (including about 60 lines for the ColorGame class) consists of about 320 lines.
You need to create the code for ColorGamePanel which extends JPanel and contains the GUI for a ColorGame The fields used by ColorGamePanel are described below. The main program for ColorGamePanel is also given below. The main program sets up a window (a JFrame) and places the ColorGamePanel within it. You need to provide (1) a ColorGamePanel constructor that does the layout of the game and (2) appropriate listeners to make the game components function.
These are the fields used by (and defined within) ColorGamePanel. You are welcome to introduce more fields if you find it necessary to do so.
JRadioButton easy,medium,difficult; // Game level (4, 8, or 16 color-levels) JSlider red,green,blue; // Color controls JPanel goalPanel; // Where goal color appears JPanel guessPanel; // Where guess color appears JLabel winLabel; // Used to indicate winning JLabel messageLabel; // Where messages appear ColorGame game; // Current color game
The sliders and the radio buttons correspond to the obvious components shown in the picture above. The goalPanel is the large outer square containing the goal color. The guessPanel is the small inner square. The winLabel does not appear above, but it shows up inside the guessPanel when someone wins the game (i.e., matches the goal color). The messageLabel contains the message that appears at the bottom of the window. The contents of the messageLabel change during the game in order to provide information to the player/user. The game represents the current ColorGame. Here's another picture of the game showing the winLabel in the guessPanel and showing a different message in the messageLabel.
The code for ColorGame is here. Be sure to look at the main program and the code fragments that appear in the Hints below.
// Prepare the win label winLabel = new JLabel("Winner!"); winLabel.setForeground(Color.black); winLabel.setOpaque(true); winLabel.setBorder(BorderFactory.createEmptyBorder(10,10,10,10));The foreground color is set to black; otherwise it uses the default color (dark blue). The setOpaque() method ensures that the label's background color shows up; otherwise the "Winner!" text might be invisible against a dark goal color. The BorderFactory is used to make a border around the label; otherwise the label's outline fits tightly around the text and it looks funny.
// Place the buttons in a ButtonGroup (only one at a time is on) ButtonGroup group = new ButtonGroup(); group.add(easy); group.add(medium); group.add(difficult);
JSlider slider = new JSlider(JSlider.VERTICAL,0,1,0); slider.setMajorTickSpacing(1); slider.setPaintTicks(true); slider.setSnapToTicks(true); slider.setBackground(color); slider.setBorder(BorderFactory.createLineBorder(this.getBackground(),5));There are three methods that are used here to handle the tick marks: setMajorTickSpacing, setPaintTicks, and setSnapToTicks. You should look up these methods in the API to see what they do. The background color is set to Color.red, Color.green, or Color.blue, as appropriate. The setBorder method is used to make some space around the slider using the background color of ColorGamePanel.
The actionPerformed() method must also create a new ColorGame. Note that the number of color values (4, 8, or 16) must also be passed to the ColorGame constructor so that an appropriate goalColor can be chosen.
// Listen for mouse clicks on the guess panel (cheating) guessPanel.addMouseListener(new MouseAdapter () { public void mousePressed (MouseEvent e) { messageLabel.setText("Guess: " + game.guessColor + " Goal: " + game.goalColor); } public void mouseReleased (MouseEvent e) { messageLabel.setText("You cheated!"); } });
public static void main (String[] args) { JFrame frame = new JFrame("Color Matching Game"); frame.addWindowListener(new WindowAdapter () { public void windowClosing (WindowEvent e) {System.exit(0);} }); frame.setSize(500,400); frame.getContentPane().add(new ColorGamePanel()); frame.setVisible(true); }
The code within addWindowListener sets up a WindowListener so that the window will disappear and the program will halt when the window-close box is clicked. The rest of the code creates a new ColorGamePanel, adds it to the frame and displays the frame.
A simple way to initialize the GUI: easy.doClick(). This generates an ActionEvent just as if the user clicked on the easy button.