Question: Define your Applications attributes. TheConcentration program has three different kinds of fields that you must define before you can run the program successfully. Your class

Define your Applications attributes. TheConcentration program has three different kinds of fields that you must define before you can run the program successfully. Your class should be extended from JFrame and implement both ActionListener and MouseListener. Make sure you define your fields without warnings or errors, before you attempt to go further.

Here are the three kinds of fields:

1. Program Arrays

The Concentration program has two separate single dimensional arrays. Start your program by defining these two arrays:

  • The first of these is an array of JLabel objects. This array, named lblBoard, is used to display 16 JLabels that make up the concentration board
  • The second array is an array of int primitives. This array, named nums, is used to display the values that are to be matched. This means that your array should hold 16 ints. These ints are the values 1 to 8 repeated twice. To accomplish this you should use an initializer list. An example of this can be found in lecture 6A titled "Initilizing Array At Declaration" about 1/2 of the way down the page.

2. Program Arrays

There are three other variables that you will need to make the game work

  1. An int called firstChoice set it to negative 1. This will be used to determine if the first or second card has been selected.
  2. An int called tries set to 0. This will be used to keep track of the number of tries for the game.
  3. A reference to a JLabel called lblFirst. This will be used to hold the first JLabel selected so that it can be compared to the second one.

3. Graphical User Interface

In addition to the JLabels that make up the arrays named lblBoard and nums the Concentrationapplication contains the following user-interface elements. :

  • A JButton object named btnGame that is labeled "New Game". This button will be used to reset the game and the board back to the beginning.
  • A JLabel named lblTries which will be used to hold the number of guesses by the user. Set the text in this JLabel to "0".
  • A JPanel named pnlControls. Use the default constructor to create pnlControls; it will hold the controls used for the game.
  • A JPanel named pnlBoard. This will be used to hold the JLabels that make up the Concentration board. Use the default constructor to create this.
  • Create and initialize the container for the content area in the field.

Step 2

Create Your Methods

1. Create Labels

You need to create method called createLabels that takes no parameters and returns void. This method will be used to create all of the JLabels for the board, set properties of each JLabel, and add the newly created JLabels to the panel. Here are the steps to accomplish this task:

  • Create the method body. Again, this method should be called createLabels
  • Set the layout of pnlBoard to a GridLayout that is 4 rows by 4 columns with an hGap and vGap of 5.
  • Create a for loop that will run 16 times. We go 16 times because the board consists of 16 JLabels
  • Inside the body of the loop you are going to set each of the JLabel's properties. You will use the loop counter value for the index into the array called lblBoard. The loop allows us to only have to write one piece of code section that will initialize all 16 JLabels
  • Create each JLabel of lblBoard by calling the overloaded constructor and setting the text to "" (empty string) and it's alignment to center
  • Set the opacity of each JLabel to true by sending the setOpaque message.
  • Set the background color of each JLabel to a color of your choosing. I am using magenta in the example
  • Set the foreground color to of JLabel to the color of your choosing. I am using white in the example.
  • Set the font to each JLabel to something appropriate. The example is 24 point Helvetica Bold font.
  • Send the addMouseListener message to each JLabel. Provide this as an argument
  • Send the setName message to each JLabel providing the value of your loop variable as an argument. An example would be: lblBoard[i].setName("" + i );
  • Add each JLabel to pnlBoard. This is the last item of the loop.

2. Shuffle The Board

You will need to create a method called shuffle that will shuffle the array called nums. This method takes no arguments and returns void. Remember, you created the array that was 16 elements long and had the values 1 to 8 in the array twice. We want to mix these around so the game is random. There are many strategies you could use to shuffle the array, my opinion is that the best way to do it is to generate two random numbers between 0 and 15 (The max and min array indexes) then, simply swap the values at these two indexes. If you do this enough times your array will be sufficiently shuffled.

  • Create three int variables. Call them num1, num2, and temp. The values stored in num1 and num2 will be used to hold the random indexes into the array that will be swapped. The temp variable will be used in the swap procedure.
  • Create a new Random object called r.
  • Create for loop that runs 500 times. This could be any number but 500 swaps should be enough to mix the array around. In the loop body do the following:
  • Set num1 to a random value by sending the nextInt message to the random object called r. You want a value between 0 and 15 which means you could pass the value of 15 to the nextInt method but it might be better to pass the length of the array in case you want to change the size of the board at some later date. The name of your array is nums so nums.length is what you would use as the argument.
  • Do the same thing as above for num2
  • Swap the values at the indexes by setting tmp = nums[num1], nums[num1] = nums[num2], and nums[num2] = tmp.

Step 3

Write the event handler methods. Your application has implemented both ActionListenter and MouseListener. This means you will need to create methods for both.

1. actionPerformed

We start with the actionPerformed method. It handles the New Game button and has the least amount of code in it.

  1. The first thing to do is to call the shuffle method.
  2. Set the firstChoice variable to -1
  3. Create a for loop that runs from 0 to the length of array lblBoard. Again you would use lblBoard.length for this
  4. Inside the loop body send each JLabel element the setText message passing it the empty string ""
  5. After the loop exits set the variable tries to 0
  6. Send the JLabel lblTries the setText message setting it to the value of the variable tries.

2. Mouse Event Methods

The only mouse event we are interested in for the Concentration game is mouseClicked but we still have to create the rest of the methods even though we have no plans to use them. All of the methods have the same signature. They return void and have an argument of MouseEvent e. Add the following empty methods to your program:

  • mouseEntered
  • mouseExited
  • mousePressed
  • mouseReleased

3. mouseClicked event

This is the most complex method of the program and is going to require you to pay close attention to the detail. You will complete this method as follows:

Create reference to a JLabel called l (this is not the number one it is lower case L). You set this variable by sending the getSource message to the MouseEvent argument called e. The method getSource returns the object that generated the event so you want to assign this to l. The code will look something like this - JLabel l = (JLabel) e.getSource()

Create and if statement that checks to see if the variable firstChoice is equal to -1. If it is then do the following

  1. Set the text of the JLabel l to the value that is in the array called nums using the index theNumber. Something like this nums[theNumber].
  2. Set lblFirst = to the l
  3. Set firstChoice equal to theNumber

Create an else if statement that checks to see if nums[theNumber] != nums[firstChoice]. If this is true do the following:

  1. Send the setText message to l setting the text to nums[theNumber]
  2. Send the JPanel called pnlBoard the paintImmediately: p.paintImmediately(0,0, p.getWidth(), p.getHeight()). This will force the panel and controls contained in it to be painted.
  3. Create a try - catch block. The catch block should be catching an InterruptedException. We will do nothing with the error if it is caught.
  4. Inside the try block send a sleep message to Thread passing a value of 250. This basically says if the two cards do not match then turn them both back over after 250 milliseconds.
  5. After the catch block send lblFirst the setText message passing a value of empty string.
  6. Send the setText message to the l passing a value of empty string
  7. Set lblFirst to null
  8. Set the firstChoice variable to -1
  9. Increment the variable tries by one

Create an else block. Inside the else block do the following:

  1. Send the setText message to l passing nums[theNumber] as an argument
  2. Set the variable firstChoice to -1
  3. Increment the tries variable by 1

Finally, send a setText message to lblTries setting the text to the value of the variable tries.

Step 4

Of course you need a constructor. Here is what needs to be done in the constructor

  1. Create the JLabels
  2. Shuffle the nums array
  3. Set layout of pnlControls to a FlowLayout
  4. Add btnGame to pnlControls
  5. Add an informational JLabel to pnlControls with the text "Number of Tries:" align the label to the right
  6. Add lblTries to pnlControls
  7. Add pnlControls to the south of the content pane.
  8. Set the window size
  9. Set the default close operation
  10. Set the window visible.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!