user13746
user13746

Reputation: 870

Changing object parameter

I have this bit of code

        for (int j = 0; j < 2; j++) {

        // Shuffle images array
        Collections.shuffle(Arrays.asList(cards));

        for (int i = 0; i < cards.length; i++) {

            // Create new instance of card
            final Card card = new Card(cards[i].getCardID(), cards[i].getCardImage());

            ImageIcon icon = card.getDefIcon(); 
            card.setIcon(icon);
            this.add(card);


            card.addMouseListener(new MouseAdapter()  
            {
                card.clicked = true;
                public void mouseReleased(MouseEvent e)  
                {  


                }  
            });  

        }

    }

but the line with "card.clicked = true" has the error Syntax error on token "clicked", VariableDeclaratorld expected after this token

The Card class look like this

public class Card extends JLabel {
int cardID;
public boolean clicked = false;
BufferedImage cardImage;
ImageIcon defIcon = new ImageIcon("E:/Java Projects/UUR - Semestralka/resources/card.png");


Card (int cardID, BufferedImage cardImage) {
    this.cardID = cardID;
    this.cardImage = cardImage;
}

public int getCardID() {
    return cardID;
}

public BufferedImage getCardImage() {
    return cardImage;
}

public ImageIcon getDefIcon() {
    return defIcon;
}
}

does anyone know what am I doing wrong?

Upvotes: 0

Views: 74

Answers (4)

nikis
nikis

Reputation: 11254

You should move it to the mouseReleased method of your anonymous MouseAdapter

Upvotes: 1

darijan
darijan

Reputation: 9795

card.clicked = true must go into the method body (inside void mouseReleased(...))

Upvotes: 1

kmera
kmera

Reputation: 1745

card.addMouseListener(new MouseAdapter() {
    //card.clicked = true; <-- cant be here
    public void mouseReleased(MouseEvent e) {  
        card.clicked = true; //should go here
    }  
});

Upvotes: 0

Bhesh Gurung
Bhesh Gurung

Reputation: 51030

You need to put

card.clicked = true;

inside the method as follows

public void mouseReleased(MouseEvent e) 
{ 
    card.clicked = true;
} 

Upvotes: 2

Related Questions