Sorry for asking you so many questions.
Currently, when a pair is not established, a message is displayed to enable JButton
by pressing JButton
to hide the card, but the following code is added to JButton
after activating JButton
.
gameDate.getCard_one().setOpen(false);
gameDate.getCard_two().setOpen(false);
gameDate.setCard_one(null); // Reset Card_one
gameDate.setCard_two(null); // Reset Card_two
If you use ActionCommand
, it will be on the back when you press Yes in the message.
I apologize for the inconvenience, but I would appreciate it if you could let me know.
Cardclick_Listener.java
package pair_matching;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import javax.swing.JoptionPane;
public class Cardclick_Listener implements MouseListener{
private CardLabel card_label;
private GameDate gameDate;
private CardList cardList;
publicCardclick_Listener(CardLabel card_label, GameDate gameDate, CardList cardList) {
This.card_label=card_label;
This.gameDate=gameDate;
This.cardList =cardList;
}
@ Override
public void mouseClicked (MouseEvente) {
boolean open=card_label.isOpen();
int Pair=gameDate.getPairCount();
// card face-to-face check
if(open==true){
; // Don't turn it on the back
} else if (open==false) {
card_label.setOpen(true);
if(gameDate.getCard_one()==null){
// null —Stored in Card_one in GameDate
gameDate.setCard_one(card_label);
return;
} else {
// non-null: stored in Card_two in GameDate
gameDate.setCard_two(card_label);
}
}
/* Pairing*/
if(gameDate.getCard_two().getNumber()==gameDate.getCard_one().getNumber()){
if(open==true){
gameDate.setPairCount(Pair+=0);// Prevents 2 from being counted when pressing a card in the table
} else {
/* Conditions for clearing the game
* Clear when 52 sheets are ready
* */
gameDate.setPairCount (Pair+=2); // Count the number of sheets
gameDate.setCard_one(null); // Reset Card_one
gameDate.setCard_two(null); // Reset Card_two
if(Pair==52){
intoption=JoptionPane.showConfirmDialog(null, "GAME CREA!!!" Press Yes to resume the game.", "GAME CREA", JOPTIONPane.YES_NO_OPTION, JOPTIONPane.PLAIN_MESSAGE);
if(option==JoptionPane.YES_OPTION){
cardList.Restart_cards(); // Turn all cards to the back
gameDate.setPairCount(0); // Reset Count Count
} else {
System.exit(0);// End of Game
}
}
}
/* Pair failed*/
} else {
/*
* Added action to activate cardList revers_btn
* */
int missmatch=JOptionPane.showConfirmDialog(null,"Cards do not match.", "Card mismatch", JoptionPane.OK_CANCEL_OPTION, JoptionPane.PLAIN_MESSAGE);
if(missmatch==JoptionPane.OK_OPTION){
cardList.revers_btn.setEnabled(true);
} else {
return;
}
/*
* Added action when cardList revers_btn is pressed
* */
// Return Card_one and Card_two to the back
}
}
@ Override
public void mousePressed (MouseEvent) {
}
@ Override
public void mouseReleased (MouseEvent) {
}
@ Override
public void mouseEntered (MouseEvent) {
}
@ Override
public void mouseExited (MouseEvent) {
}
}
CardList.java
package pair_matching;
import java.awt.Dimension;
import java.util.ArrayList;
import java.util.Collections;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JPanel;
public class CardList extensions JPanel {
private GameDate gameDate = new GameDate();
JButton reverses_btn;
// List Labels - > Set ImageIcon for each label in the list - > Collect List Shuffle
boolean flg = false;
final int club = 0;
final int diamond = 1;
final int heart = 2;
final int spade=3;
/* Card image*/
// reverse side
ImageIcon card_back=new ImageIcon("image/card_back.png");
// surface
ImageIcon[][]card_array=new ImageIcon[4][13];
String filename;
// the number of j
int[] number = {
1,2,3,4,5,6,7,8,9,10,11,12,13,
1,2,3,4,5,6,7,8,9,10,11,12,13,
1,2,3,4,5,6,7,8,9,10,11,12,13,
1,2,3,4,5,6,7,8,9,10,11,12,13
};
// List cards
ArrayList<CardLabel>cards=new ArrayList<>();
publicCardList(){
setPreferredSize (new Dimension (1150,600));
Start_card_list();
Revers_button();
}
public void Start_card_list(){
// Card_image
for(inti=0;i<card_array.length;i++){
for(int j=0;j<card_array[i].length;j++){
filename = "image/card";
switch(i){
case club:
filename = filename + "_club_";
break;
case diamond:
filename = filename + "_diamond_";
break;
case heart:
filename = filename + "_heart_";
break;
case spade:
filename = filename + "_spade_";
break;
}
// Add a number to the file name -> Add an extension
filename = String.format(filename + "%02d", j+1);
filename = filename + ".png";
card_array[i][j] = new ImageIcon(filename);
CardLabel tmp_label = new CardLabel(card_array[i][j], card_back, number[j]);
// If you press tmp_label, the tmp_label turns over.
tmp_label.addMouseListener(new Cardclick_Listener(tmp_label, gameDate, this));
add(tmp_label);
cards.add(tmp_label);
Collections.shuffle (cards);
}
}
// card shuffle
for (CardLabel card_shuffle:cards) {
add(card_shuffle);
}
}
// Turn the card to the back when restarting.
public void Restart_cards() {
for(inti=0;i<cards.size();i++){
cards.get(i).setOpen(flg);
}
}
// a button on which a card is placed down
public void Revers_button(){
revers_btn = new JButton("Card Down";
revers_btn.setEnabled(false);
add(revers_btn);
}
}
I have solved it myself, so I will post a reply.
We have added the ActionListener
class as follows, and the JButton
processing has been added to it.
The card was processed by MouseListener
, so it seems to have been mixed up.
package pair_matching;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
public class reverses_button_click implements ActionListener {
private GameDate gameDate;
private CardList cardList;
public reverses_button_click(GameDate gameDate, CardList cardList) {
This.gameDate=gameDate;
This.cardList =cardList;
}
@ Override
public void actionPerformed (ActionEvent) {
/* Turn Card_one and Card_two on the back.
* If Card_one and Card_two are turned to the back, the down button is deactivated.
* */
cardList.revers_btn.setActionCommand("revers");
String cmd = cardList.revers_btn.getActionCommand();
if(cmd.equals("revers"){
gameDate.getCard_one().setOpen(false);
gameDate.getCard_two().setOpen(false);
gameDate.setCard_one(null); // Reset Card_one
gameDate.setCard_two(null); // Reset Card_two
cardList.revers_btn.setEnabled(false); // Revert Button to Inactive
}
}
}
© 2024 OneMinuteCode. All rights reserved.