Buttons in JAVA IN JAVA jGrasp MyMatchingGame.java I will rate well if the code
ID: 3842305 • Letter: B
Question
Buttons in JAVA IN JAVA jGrasp
MyMatchingGame.java I will rate well if the code is written correctly.
In this game a student must match two pictures together like the bottom example of matching the mascot to its school.
( I want to match movies to the main characters in the movies but I don't know how to add images to a button in jGrasp ).
Panel
Write a matching game program. Use BorderLayout with 3 panels. The north panel will have 5-6 buttons with an icon or words on each. Use Paint to resize all the pictures to the same dimensions. The center panel will have a title and/or directions. The south panel will have 5-6 buttons that can be “matched” to the north buttons. The order in the south will be different than the north. Output a statement or sound when the match is correct or not.
The Logic
In the panel you will need to keep track of the first button pressed, so when the second button is pressed, you’ll know if you have a match. Below is the easiest way to do it.
Create a private int, called choice, above the constructor (with the components).
Attach each button to its own Listener. When a top row button is selected, set choice to a number representing that button. In the matching bottom row button’s Listener, check choice and determine if the match was correct or not. Play your correct sound or incorrect sound. Then reset choice to 0.
You must select a top row button first, and then a bottom row button. See below:
private class NCSUListener implements ActionListener //attached to the NCSU logo in top row
{
public void actionPerformed (ActionEvent e)
{
choice = 1; //NCSU will be #1
}
}
private class NCSUMascotListener implements ActionListener //attached to NCSU mascot in bottom row
{
public void actionPerformed (ActionEvent e)
{
if (choice = = 1)
goodSound1.play(); //a match
else
badSound1.play(); //not a match
choice = 0; //reset for next attempt
} }
Explanation / Answer
package cs435; import java.awt.*; import java.awt.event.ActionEvent; import java.awt.event.ActionListener; import javax.swing.*; /** * A Java example for CS435, M. Wainer * Sample java code: Match game starter, Aug 28, 2008: * Do not regard this code as a perfect example (it isn't) */ public class MatchGame implements ActionListener { JPanel p; JFrame f; String[][] matchList = { {"A.I", "436"}, {"Graphics", "485"}, {"Algorithms", "455" }, {"Database", "430"}, {"Networking", "440"}, {"HCI", "484"}, {"Software", "435" }}; JButton[][] buttons; int i = 0; boolean flipping = true; int cardOne; int secIndex; public static void main(String[] args) { //Schedule a job for the event-dispatching thread //to create application and display its GUI javax.swing.SwingUtilities.invokeLater(new Runnable() { public void run() { MatchGame app = new MatchGame(); app.makeGUI(); } }); } public void dealCards(JPanel panel) { buttons = new JButton[3][]; // array of buttons used to represent cards // should really shuffle cards for (int i= 0; i< 3*4; i++) { // initialize 3 rows with 4 columns each if (i%4 == 0) buttons[i/4] = new JButton[4]; buttons[i/4][i%4] = new JButton("-Match-"); // show face down buttons[i/4][i%4].addActionListener(this); panel.add(buttons[i/4][i%4]); } } public void updateMatchList(String a, String b, boolean add) { int i,j; String[][] courseList; int oldLen = matchList.length; if (add) { // add the new item to the list courseList = new String[oldLen+1][]; courseList[0] = new String[2]; courseList[0][0] = new String(a); // new first course courseList[0][1] = new String(b); // new first course num for (int item=1; itemRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.