I am trying to create a java memory/ matching game and I am completely stuck.
I created the grid with buttons and it looks fine but I have no idea how to make the game work.
the first problem is that I do not know how to make java wait before unflipping the cards.
The second problem is that I am not sure how to flip the cards. I was thinking of sitting the icon to null and
then saving the icons in an array so that I could know which icon belongs to each button.
the third and most important problem is that I so not know how to use the ActionListener.
I do not know how to use it for two buttons at a time.
This is my code. I'm sorry if it seems so messy but it is only because i have experimented a lot.
import javax.swing.Icon;
import javax.swing.ImageIcon;
import java.awt.GridLayout;
import java.awt.BorderLayout;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JButton;
import java.util.ArrayList;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
public class Memory extends JFrame
{
private JPanel panel1;
private JPanel panel;
private JPanel panel2;
private JButton cards[];
private ArrayList<Icon> icons;
private Icon[] icon;
private JButton newGame;
private int numOfTries = 0;
private int pairsFound = 0;
private int numCards = 16;
private Icon icon1;
private Icon icon2;
private Icon icon3;
private Icon icon4;
private Icon icon5;
private Icon icon6;
private Icon icon7;
private Icon icon8;
private int num;
public Memory()
{
super( "Memory" );
cards = new JButton[16];
panel = new JPanel();
panel.setLayout(new GridLayout(4, 4));
// setLayout(new GridLayout(4, 4));
icons = new ArrayList<Icon>();
icon1 = new ImageIcon(getClass().getResource("1.gif"));
icon2 = new ImageIcon(getClass().getResource("2.gif"));
icon3 = new ImageIcon(getClass().getResource("3.gif"));
icon4 = new ImageIcon(getClass().getResource("4.gif"));
icon5 = new ImageIcon(getClass().getResource("5.gif"));
icon6 = new ImageIcon(getClass().getResource("6.gif"));
icon7 = new ImageIcon(getClass().getResource("7.gif"));
icon8 = new ImageIcon(getClass().getResource("8.gif"));
icons.add(icon1);
icons.add(icon1);
icons.add(icon2);
icons.add(icon2);
icons.add(icon3);
icons.add(icon3);
icons.add(icon4);
icons.add(icon4);
icons.add(icon5);
icons.add(icon5);
icons.add(icon6);
icons.add(icon6);
icons.add(icon7);
icons.add(icon7);
icons.add(icon8);
icons.add(icon8);
for ( int count = 0; count < cards.length; count++ )
{
num = (int)((16-count)*Math.random());
cards[ count ] = new JButton();
// cards[ count ].setIcon(icons.get(num));
cards[ count ].setPressedIcon(icons.get(num));
panel.add( cards[ count ] );
// add( cards[ count ] );
icons.remove(num);
}
add( panel, BorderLayout.CENTER); // add panel to JFrame
//////
// panel2 = new JPanel();
// newGame = new JButton("New Game");
// panel2.add(newGame);
// add( panel, BorderLayout.SOUTH);
//
// wait(1);
// for ( int count = 0; count < cards.length; count++ )
// {
// icon = cards[ count ].getIcon();
// cards[count].setIcon(null);
//
// }
}
public static void wait(int n)
{
long t0, t1;
t0 = System.currentTimeMillis();
do{
t1 = System.currentTimeMillis();
}
while ((t1 - t0) < (n * 1000));
}
// private class ButtonHandler implements ActionListener
// {
// public void actionPerformed(ActionEvent event)
// {
//
// }
// }
}
I really appreciate your help. I have only 2 days to submit my project.

New Topic/Question
Reply



MultiQuote





|