I've looked through some other question of the same type and trying to find the answer but I wasn't able to see what is wrong, if someone could point it out for me would be great!
So basically I'm creating a "Memory Game" in which I have to set up a default background image to each card, but this image has to change each time I run it again. Also I gotta create and array list with the foreground images (which are 21) but I'll only use 8 per game. They also have to change each time I run the program. That said, what I got so far is:
public class GameBoard extends javax.swing.JPanel implements Runnable {
private JLabel[] _Labels = new JLabel[8];
private Card[] _Cards = new Card[16];
public GameBoard(){
setLayout(new GridLayout(4, 4));
for(int i = 0; i<8; i=i+1){
_Labels[i] = new JLabel();
}
for(int i = 0; i<16; i=i+2){
_Cards[i] = new Card(_Labels[i]);
_Cards[i+1] = new Card(_Labels[i]);
add(_Cards[i].getLabel());
add(_Cards[i+1].getLabel());
_Labels[i].addMouseListener(new LabelListener(_Cards[i]));
_Labels[i+1].addMouseListener(new LabelListener(_Cards[i+1]));
}
}
@Override
public void run() {
}
}
The card class is:
public class Card{
private JLabel _Label;
ImageIcon imageOne;
ImageIcon imageTwo;
public Card(JLabel once){
_Label = once;
//-----------------------Foreground--------------------------------
ArrayList<String> foregroundCard = new ArrayList<String>();
int i = 1;
while(i < 23){
if(i > 9){
String addGreaterNine = "F" + i + ".png";
foregroundCard.add(addGreaterNine);
}
else{
String addLesserNine = "F0" + i +".png";
foregroundCard.add(addLesserNine);
}
i = i + 1;
}
//-----------------Add 8---------------------
int a = 0;
while(a < 8){
Collections.shuffle(foregroundCard);
ArrayList<String> stringsGoingToBeUsed = new ArrayList<String>();
stringsGoingToBeUsed.add(foregroundCard.get(0));
stringsGoingToBeUsed.add(foregroundCard.get(0));
for(int l = 0; l<16; l=l+1){
Collections.shuffle(stringsGoingToBeUsed);
String frontName = "Images/" + stringsGoingToBeUsed.get(0);
imageOne = new ImageIcon(frontName);
}
a = a + 1;
}
//------------------------Background--------------------------------
ArrayList<String> backgroundCard = new ArrayList<String>();
backgroundCard.add("B01.png");
backgroundCard.add("B02.png");
backgroundCard.add("B03.png");
Collections.shuffle(backgroundCard);
String location = "Images/" + backgroundCard.get(0);
imageTwo = new ImageIcon(location);
//-------------------------------------------------------------------
_Label.setIcon(imageTwo);
}
public void TurnCard (){
ImageIcon temp;
temp = imageTwo;
imageTwo = imageOne;
imageOne = temp;
_Label.setIcon(imageTwo);
}
public JLabel getLabel(){
return _Label;
}
}
And the LabelListener class is:
public class LabelListener implements MouseListener {
private Card _Card;
public LabelListener(Card c){
_Card = c;
}
@Override
public void mouseClicked(MouseEvent arg0) {
_Card.TurnCard();
}
@Override
public void mouseEntered(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseExited(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mousePressed(MouseEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public void mouseReleased(MouseEvent arg0) {
// TODO Auto-generated method stub
}
}
The main method is:
public class Game {
public static void main(String[] args) {
JFrame main = new JFrame("Memory Game");
main.add(new GameBoard());
main.pack();
main.setVisible(true);
main.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
I suspect it's because _Labels
is an array with 8 elements, however you're attempting to reference up to 16 in the following code:
for(int i = 0; i<16; i=i+2){
...
_Labels[i+1].addMouseListener(new LabelListener(_Cards[i+1]));
...
for(int i = 0; i<16; i=i+2){
_Cards[i] = new Card(_Labels[i]);
_Cards[i+1] = new Card(_Labels[i]);
add(_Cards[i].getLabel());
add(_Cards[i+1].getLabel());
_Labels[i].addMouseListener(new LabelListener(_Cards[i]));
_Labels[i+1].addMouseListener(new LabelListener(_Cards[i+1]));
}
Doesnt work as _Labels is only size 8 a possible solution would be to split this into separate loops or change the program architecture.
As there is a mismatch between the number of Card
and JLabel
array items which produces the ArrayIndexOutOfBoundsException
. You could add another index j
to account for this:
for (int i = 0, j = 0; i < 16; i += 2, j++) {
_Cards[i] = new Card(_Labels[j]); // AIOOBE was happening here
_Cards[i + 1] = new Card(_Labels[j]);
add(_Cards[i].getLabel());
add(_Cards[i + 1].getLabel());
_Labels[j].addMouseListener(new LabelListener(_Cards[i]));
// not necessary
// _Labels[j + 1].addMouseListener(new LabelListener(_Cards[i + 1]));
}
Aside: Use Java naming conventions for naming variables, such as cards
in place of _Cards
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.