(Java)怎么换图(War卡牌游戏)
(Java)How to change picture(War Card Game)
好的,我想做的是制作一个名为 hidden 的图像,它将是一张面朝下的卡片。现在我想把它变成一张正面朝上的卡片,我想做的是为每队三张六张卡片制作一张图片。
"p1, p2, p3" 和 "o1, o2, o3" 这些是我给卡片面朝下的名称,我希望隐藏的 JLabel 更改或等于这些名称,然后更改为正面朝上的卡片我只放了这部分代码,因为我以为这就是您所需要的,但如果您需要查看更多代码,我可以将其放在此处。
任何帮助,将不胜感激。
public class Cards {
static int[] fullDeck = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 52, 51, 50, 49, 48,
47, 46, 45, 44, 43, 42, 41, 40, 39, 38, 37, 36, 35, 34, 33, 32, 31,
30, 29, 28, 27, };
static int oDeck;
static int pDeck;
static int pTurn;
static int oTurn;
private JFrame frame;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Cards window = new Cards();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public Cards() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JLabel hidden = new JLabel("");
JLabel p1 = new JLabel("New label");
p1.setBounds(47, 24, 82, 82);
frame.getContentPane().add(p1);
JLabel p2 = new JLabel("New label");
p2.setBounds(139, 24, 82, 82);
frame.getContentPane().add(p2);
JLabel p3 = new JLabel("New label");
p3.setBounds(211, 24, 82, 82);
frame.getContentPane().add(p3);
JLabel o1 = new JLabel("New label");
o1.setBounds(47, 95, 82, 82);
frame.getContentPane().add(o1);
JLabel o2 = new JLabel("New label");
o2.setBounds(139, 95, 82, 82);
frame.getContentPane().add(o2);
JLabel o3 = new JLabel("New label");
o3.setBounds(211, 95, 82, 82);
frame.getContentPane().add(o3);
//This creates the players hand and the opponents hand
ArrayList<Integer> playerDeck = new ArrayList<>();
ArrayList<Integer> opponentDeck = new ArrayList<>();
for (int i = 0; i <= fullDeck.length; i++) {
if (i % 2 == 0) {
playerDeck.add(i);
} else {
opponentDeck.add(i);
}
Collections.shuffle(playerDeck);
Collections.shuffle(opponentDeck);
JButton btnNewButton = new JButton("New button");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {for (int i = 0; i < 4; i++) {
/*if (i == 1){
pDeck = playerDeck.get(1);
hidden = p1;
hidden.setBounds(20, 200, 106, 61);
System.out.println("Check1");
}
else if (i == 2){
pDeck = playerDeck.get(2);
hidden = p2;
hidden.setBounds(162, 200, 106, 61);
System.out.println("Check2");
}
else if (i == 3){
pDeck = playerDeck.get(3);
hidden = p3;
System.out.println("Check3");
}*/
pDeck = playerDeck.get(1);
hidden = p1;
switch (pDeck) {
case 1:
Image img = new ImageIcon(this.getClass()
.getResource("/bin/1.png")).getImage();
hidden.setIcon(new ImageIcon(img));
pTurn = 14;
break;
case 2:
Image img2 = new ImageIcon(this.getClass()
.getResource("/bin/2.png")).getImage();
hidden.setIcon(new ImageIcon(img2));
pTurn = 14;
break;
case 3:
Image img3 = new ImageIcon(this.getClass()
.getResource("/bin/3.png")).getImage();
hidden.setIcon(new ImageIcon(img3));
pTurn = 14;
break;
case 4:
Image img4 = new ImageIcon(this.getClass()
.getResource("/bin/4.png")).getImage();
hidden.setIcon(new ImageIcon(img4));
pTurn = 14;
break;
case 5:
Image img5 = new ImageIcon(this.getClass()
.getResource("/bin/5.png")).getImage();
hidden.setIcon(new ImageIcon(img5));
pTurn = 13;
break;
case 6:
Image img6 = new ImageIcon(this.getClass()
.getResource("/bin/6.png")).getImage();
hidden.setIcon(new ImageIcon(img6));
pTurn = 13;
case 7:
Image img7 = new ImageIcon(this.getClass()
.getResource("/bin/7.png")).getImage();
hidden.setIcon(new ImageIcon(img7));
pTurn = 13;
break;
case 8:
Image img8 = new ImageIcon(this.getClass()
.getResource("/bin/8.png")).getImage();
hidden.setIcon(new ImageIcon(img8));
pTurn = 13;
break;
case 9:
Image img9 = new ImageIcon(this.getClass()
.getResource("/bin/9.png")).getImage();
hidden.setIcon(new ImageIcon(img9));
pTurn = 12;
break;
case 10:
Image img10 = new ImageIcon(this.getClass()
.getResource("/bin/10.png")).getImage();
hidden.setIcon(new ImageIcon(img10));
pTurn = 12;
break;
case 11:
Image img11 = new ImageIcon(this.getClass()
.getResource("/bin/11.png")).getImage();
hidden.setIcon(new ImageIcon(img11));
pTurn = 12;
break;
case 12:
Image img12 = new ImageIcon(this.getClass()
.getResource("/bin/12.png")).getImage();
hidden.setIcon(new ImageIcon(img12));
pTurn = 12;
break;
case 13:
Image img13 = new ImageIcon(this.getClass()
.getResource("/bin/13.png")).getImage();
hidden.setIcon(new ImageIcon(img13));
pTurn = 11;
break;
case 14:
Image img14 = new ImageIcon(this.getClass()
.getResource("/bin/14.png")).getImage();
hidden.setIcon(new ImageIcon(img14));
pTurn = 11;
break;
case 15:
Image img15 = new ImageIcon(this.getClass()
.getResource("/bin/15.png")).getImage();
hidden.setIcon(new ImageIcon(img15));
pTurn = 11;
break;
case 16:
Image img16 = new ImageIcon(this.getClass()
.getResource("/bin/16.png")).getImage();
hidden.setIcon(new ImageIcon(img16));
pTurn = 11;
break;
case 17:
Image img17 = new ImageIcon(this.getClass()
.getResource("/bin/17.png")).getImage();
hidden.setIcon(new ImageIcon(img17));
pTurn = 10;
break;
case 18:
Image img18 = new ImageIcon(this.getClass()
.getResource("/bin/18.png")).getImage();
hidden.setIcon(new ImageIcon(img18));
pTurn = 10;
break;
case 19:
Image img19 = new ImageIcon(this.getClass()
.getResource("/bin/19.png")).getImage();
hidden.setIcon(new ImageIcon(img19));
pTurn = 10;
break;
case 20:
Image img20 = new ImageIcon(this.getClass()
.getResource("/bin/20.png")).getImage();
hidden.setIcon(new ImageIcon(img20));
pTurn = 10;
break;
case 21:
Image img21 = new ImageIcon(this.getClass()
.getResource("/bin/21.png")).getImage();
hidden.setIcon(new ImageIcon(img21));
pTurn = 9;
break;
case 22:
Image img22 = new ImageIcon(this.getClass()
.getResource("/bin/22.png")).getImage();
hidden.setIcon(new ImageIcon(img22));
pTurn = 9;
break;
case 23:
Image img23 = new ImageIcon(this.getClass()
.getResource("/bin/23.png")).getImage();
hidden.setIcon(new ImageIcon(img23));
pTurn = 9;
break;
case 24:
Image img24 = new ImageIcon(this.getClass()
.getResource("/bin/24.png")).getImage();
hidden.setIcon(new ImageIcon(img24));
pTurn = 9;
break;
case 25:
Image img25 = new ImageIcon(this.getClass()
.getResource("/bin/25.png")).getImage();
hidden.setIcon(new ImageIcon(img25));
pTurn = 8;
break;
case 26:
Image img26 = new ImageIcon(this.getClass()
.getResource("/bin/26.png")).getImage();
hidden.setIcon(new ImageIcon(img26));
pTurn = 8;
break;
case 27:
Image img27 = new ImageIcon(this.getClass()
.getResource("/bin/27.png")).getImage();
hidden.setIcon(new ImageIcon(img27));
pTurn = 8;
break;
case 28:
Image img28 = new ImageIcon(this.getClass()
.getResource("/bin/28.png")).getImage();
hidden.setIcon(new ImageIcon(img28));
pTurn = 8;
break;
case 29:
Image img29 = new ImageIcon(this.getClass()
.getResource("/bin/29.png")).getImage();
hidden.setIcon(new ImageIcon(img29));
pTurn = 7;
break;
case 30:
Image img30 = new ImageIcon(this.getClass()
.getResource("/bin/30.png")).getImage();
hidden.setIcon(new ImageIcon(img30));
pTurn = 7;
break;
case 31:
Image img31 = new ImageIcon(this.getClass()
.getResource("/bin/31.png")).getImage();
hidden.setIcon(new ImageIcon(img31));
pTurn = 7;
break;
case 32:
Image img32 = new ImageIcon(this.getClass()
.getResource("/bin/32.png")).getImage();
hidden.setIcon(new ImageIcon(img32));
pTurn = 7;
break;
case 33:
Image img33 = new ImageIcon(this.getClass()
.getResource("/bin/33.png")).getImage();
hidden.setIcon(new ImageIcon(img33));
pTurn = 6;
break;
case 34:
Image img34 = new ImageIcon(this.getClass()
.getResource("/bin/34.png")).getImage();
hidden.setIcon(new ImageIcon(img34));
pTurn = 6;
break;
case 35:
Image img35 = new ImageIcon(this.getClass()
.getResource("/bin/35.png")).getImage();
hidden.setIcon(new ImageIcon(img35));
pTurn = 6;
break;
case 36:
Image img36 = new ImageIcon(this.getClass()
.getResource("/bin/36.png")).getImage();
hidden.setIcon(new ImageIcon(img36));
pTurn = 6;
break;
case 37:
Image img37 = new ImageIcon(this.getClass()
.getResource("/bin/37.png")).getImage();
hidden.setIcon(new ImageIcon(img37));
pTurn = 5;
break;
case 38:
Image img38 = new ImageIcon(this.getClass()
.getResource("/bin/38.png")).getImage();
hidden.setIcon(new ImageIcon(img38));
pTurn = 5;
break;
case 39:
Image img39 = new ImageIcon(this.getClass()
.getResource("/bin/39.png")).getImage();
hidden.setIcon(new ImageIcon(img39));
pTurn = 5;
break;
case 40:
Image img40 = new ImageIcon(this.getClass()
.getResource("/bin/40.png")).getImage();
hidden.setIcon(new ImageIcon(img40));
pTurn = 5;
break;
case 41:
Image img41 = new ImageIcon(this.getClass()
.getResource("/bin/41.png")).getImage();
hidden.setIcon(new ImageIcon(img41));
pTurn = 4;
break;
case 42:
Image img42 = new ImageIcon(this.getClass()
.getResource("/bin/42.png")).getImage();
hidden.setIcon(new ImageIcon(img42));
pTurn = 4;
break;
case 43:
Image img43 = new ImageIcon(this.getClass()
.getResource("/bin/43.png")).getImage();
hidden.setIcon(new ImageIcon(img43));
pTurn = 4;
break;
case 44:
Image img44 = new ImageIcon(this.getClass()
.getResource("/bin/44.png")).getImage();
hidden.setIcon(new ImageIcon(img44));
pTurn = 4;
break;
case 45:
Image img45 = new ImageIcon(this.getClass()
.getResource("/bin/45.png")).getImage();
hidden.setIcon(new ImageIcon(img45));
pTurn = 3;
break;
case 46:
Image img46 = new ImageIcon(this.getClass()
.getResource("/bin/46.png")).getImage();
hidden.setIcon(new ImageIcon(img46));
pTurn = 3;
break;
case 47:
Image img47 = new ImageIcon(this.getClass()
.getResource("/bin/47.png")).getImage();
hidden.setIcon(new ImageIcon(img47));
pTurn = 3;
break;
case 48:
Image img48 = new ImageIcon(this.getClass()
.getResource("/bin/48.png")).getImage();
hidden.setIcon(new ImageIcon(img48));
pTurn = 3;
break;
case 49:
Image img49 = new ImageIcon(this.getClass()
.getResource("/bin/49.png")).getImage();
hidden.setIcon(new ImageIcon(img49));
pTurn = 2;
break;
case 50:
Image img50 = new ImageIcon(this.getClass()
.getResource("/bin/50.png")).getImage();
hidden.setIcon(new ImageIcon(img50));
pTurn = 2;
break;
case 51:
Image img51 = new ImageIcon(this.getClass()
.getResource("/bin/51.png")).getImage();
hidden.setIcon(new ImageIcon(img51));
pTurn = 2;
break;
case 52:
Image img52 = new ImageIcon(this.getClass()
.getResource("/bin/52.png")).getImage();
hidden.setIcon(new ImageIcon(img52));
pTurn = 2;
break;
default:
}
if (i == 1){
oDeck = opponentDeck.get(1);
JLabel hidden = o1;
}
else if (i == 2){
oDeck = opponentDeck.get(2);
JLabel hidden = o2;
}
else if (i == 3){
oDeck = opponentDeck.get(3);
JLabel hidden = o3;
}
switch (oDeck) {
case 1:
Image img = new ImageIcon(this.getClass()
.getResource("/bin/1.png")).getImage();
hidden.setIcon(new ImageIcon(img));
oTurn = 14;
break;
case 2:
Image img2 = new ImageIcon(this.getClass()
.getResource("/bin/2.png")).getImage();
hidden.setIcon(new ImageIcon(img2));
oTurn = 14;
break;
case 3:
Image img3 = new ImageIcon(this.getClass()
.getResource("/bin/3.png")).getImage();
hidden.setIcon(new ImageIcon(img3));
oTurn = 14;
break;
case 4:
Image img4 = new ImageIcon(this.getClass()
.getResource("/bin/4.png")).getImage();
hidden.setIcon(new ImageIcon(img4));
oTurn = 14;
break;
case 5:
Image img5 = new ImageIcon(this.getClass()
.getResource("/bin/5.png")).getImage();
hidden.setIcon(new ImageIcon(img5));
oTurn = 13;
break;
case 6:
Image img6 = new ImageIcon(this.getClass()
.getResource("/bin/6.png")).getImage();
hidden.setIcon(new ImageIcon(img6));
oTurn = 13;
case 7:
Image img7 = new ImageIcon(this.getClass()
.getResource("/bin/7.png")).getImage();
hidden.setIcon(new ImageIcon(img7));
oTurn = 13;
break;
case 8:
Image img8 = new ImageIcon(this.getClass()
.getResource("/bin/8.png")).getImage();
hidden.setIcon(new ImageIcon(img8));
oTurn = 13;
break;
case 9:
Image img9 = new ImageIcon(this.getClass()
.getResource("/bin/9.png")).getImage();
hidden.setIcon(new ImageIcon(img9));
oTurn = 12;
break;
case 10:
Image img10 = new ImageIcon(this.getClass()
.getResource("/bin/10.png")).getImage();
hidden.setIcon(new ImageIcon(img10));
oTurn = 12;
break;
case 11:
Image img11 = new ImageIcon(this.getClass()
.getResource("/bin/11.png")).getImage();
hidden.setIcon(new ImageIcon(img11));
oTurn = 12;
break;
case 12:
Image img12 = new ImageIcon(this.getClass()
.getResource("/bin/12.png")).getImage();
hidden.setIcon(new ImageIcon(img12));
oTurn = 12;
break;
case 13:
Image img13 = new ImageIcon(this.getClass()
.getResource("/bin/13.png")).getImage();
hidden.setIcon(new ImageIcon(img13));
oTurn = 11;
break;
case 14:
Image img14 = new ImageIcon(this.getClass()
.getResource("/bin/14.png")).getImage();
hidden.setIcon(new ImageIcon(img14));
oTurn = 11;
break;
case 15:
Image img15 = new ImageIcon(this.getClass()
.getResource("/bin/15.png")).getImage();
hidden.setIcon(new ImageIcon(img15));
oTurn = 11;
break;
case 16:
Image img16 = new ImageIcon(this.getClass()
.getResource("/bin/16.png")).getImage();
hidden.setIcon(new ImageIcon(img16));
oTurn = 11;
break;
case 17:
Image img17 = new ImageIcon(this.getClass()
.getResource("/bin/17.png")).getImage();
hidden.setIcon(new ImageIcon(img17));
oTurn = 10;
break;
case 18:
Image img18 = new ImageIcon(this.getClass()
.getResource("/bin/18.png")).getImage();
hidden.setIcon(new ImageIcon(img18));
oTurn = 10;
break;
case 19:
Image img19 = new ImageIcon(this.getClass()
.getResource("/bin/19.png")).getImage();
hidden.setIcon(new ImageIcon(img19));
oTurn = 10;
break;
case 20:
Image img20 = new ImageIcon(this.getClass()
.getResource("/bin/20.png")).getImage();
hidden.setIcon(new ImageIcon(img20));
oTurn = 10;
break;
case 21:
Image img21 = new ImageIcon(this.getClass()
.getResource("/bin/21.png")).getImage();
hidden.setIcon(new ImageIcon(img21));
oTurn = 9;
break;
case 22:
Image img22 = new ImageIcon(this.getClass()
.getResource("/bin/22.png")).getImage();
hidden.setIcon(new ImageIcon(img22));
oTurn = 9;
break;
case 23:
Image img23 = new ImageIcon(this.getClass()
.getResource("/bin/23.png")).getImage();
hidden.setIcon(new ImageIcon(img23));
oTurn = 9;
break;
case 24:
Image img24 = new ImageIcon(this.getClass()
.getResource("/bin/24.png")).getImage();
hidden.setIcon(new ImageIcon(img24));
oTurn = 9;
break;
case 25:
Image img25 = new ImageIcon(this.getClass()
.getResource("/bin/25.png")).getImage();
hidden.setIcon(new ImageIcon(img25));
oTurn = 8;
break;
case 26:
Image img26 = new ImageIcon(this.getClass()
.getResource("/bin/26.png")).getImage();
hidden.setIcon(new ImageIcon(img26));
oTurn = 8;
break;
case 27:
Image img27 = new ImageIcon(this.getClass()
.getResource("/bin/27.png")).getImage();
hidden.setIcon(new ImageIcon(img27));
oTurn = 8;
break;
case 28:
Image img28 = new ImageIcon(this.getClass()
.getResource("/bin/28.png")).getImage();
hidden.setIcon(new ImageIcon(img28));
oTurn = 8;
break;
case 29:
Image img29 = new ImageIcon(this.getClass()
.getResource("/bin/29.png")).getImage();
hidden.setIcon(new ImageIcon(img29));
oTurn = 7;
break;
case 30:
Image img30 = new ImageIcon(this.getClass()
.getResource("/bin/30.png")).getImage();
hidden.setIcon(new ImageIcon(img30));
oTurn = 7;
break;
case 31:
Image img31 = new ImageIcon(this.getClass()
.getResource("/bin/31.png")).getImage();
hidden.setIcon(new ImageIcon(img31));
oTurn = 7;
break;
case 32:
Image img32 = new ImageIcon(this.getClass()
.getResource("/bin/32.png")).getImage();
hidden.setIcon(new ImageIcon(img32));
oTurn = 7;
break;
case 33:
Image img33 = new ImageIcon(this.getClass()
.getResource("/bin/33.png")).getImage();
hidden.setIcon(new ImageIcon(img33));
oTurn = 6;
break;
case 34:
Image img34 = new ImageIcon(this.getClass()
.getResource("/bin/34.png")).getImage();
hidden.setIcon(new ImageIcon(img34));
oTurn = 6;
break;
case 35:
Image img35 = new ImageIcon(this.getClass()
.getResource("/bin/35.png")).getImage();
hidden.setIcon(new ImageIcon(img35));
oTurn = 6;
break;
case 36:
Image img36 = new ImageIcon(this.getClass()
.getResource("/bin/36.png")).getImage();
hidden.setIcon(new ImageIcon(img36));
oTurn = 6;
break;
case 37:
Image img37 = new ImageIcon(this.getClass()
.getResource("/bin/37.png")).getImage();
hidden.setIcon(new ImageIcon(img37));
oTurn = 5;
break;
case 38:
Image img38 = new ImageIcon(this.getClass()
.getResource("/bin/38.png")).getImage();
hidden.setIcon(new ImageIcon(img38));
oTurn = 5;
break;
case 39:
Image img39 = new ImageIcon(this.getClass()
.getResource("/bin/39.png")).getImage();
hidden.setIcon(new ImageIcon(img39));
oTurn = 5;
break;
case 40:
Image img40 = new ImageIcon(this.getClass()
.getResource("/bin/40.png")).getImage();
hidden.setIcon(new ImageIcon(img40));
oTurn = 5;
break;
case 41:
Image img41 = new ImageIcon(this.getClass()
.getResource("/bin/41.png")).getImage();
hidden.setIcon(new ImageIcon(img41));
oTurn = 4;
break;
case 42:
Image img42 = new ImageIcon(this.getClass()
.getResource("/bin/42.png")).getImage();
hidden.setIcon(new ImageIcon(img42));
oTurn = 4;
break;
case 43:
Image img43 = new ImageIcon(this.getClass()
.getResource("/bin/43.png")).getImage();
hidden.setIcon(new ImageIcon(img43));
oTurn = 4;
break;
case 44:
Image img44 = new ImageIcon(this.getClass()
.getResource("/bin/44.png")).getImage();
hidden.setIcon(new ImageIcon(img44));
oTurn = 4;
break;
case 45:
Image img45 = new ImageIcon(this.getClass()
.getResource("/bin/45.png")).getImage();
hidden.setIcon(new ImageIcon(img45));
oTurn = 3;
break;
case 46:
Image img46 = new ImageIcon(this.getClass()
.getResource("/bin/46.png")).getImage();
hidden.setIcon(new ImageIcon(img46));
oTurn = 3;
break;
case 47:
Image img47 = new ImageIcon(this.getClass()
.getResource("/bin/47.png")).getImage();
hidden.setIcon(new ImageIcon(img47));
oTurn = 3;
break;
case 48:
Image img48 = new ImageIcon(this.getClass()
.getResource("/bin/48.png")).getImage();
hidden.setIcon(new ImageIcon(img48));
oTurn = 3;
break;
case 49:
Image img49 = new ImageIcon(this.getClass()
.getResource("/bin/49.png")).getImage();
hidden.setIcon(new ImageIcon(img49));
oTurn = 2;
break;
case 50:
Image img50 = new ImageIcon(this.getClass()
.getResource("/bin/50.png")).getImage();
hidden.setIcon(new ImageIcon(img50));
oTurn = 2;
break;
case 51:
Image img51 = new ImageIcon(this.getClass()
.getResource("/bin/51.png")).getImage();
hidden.setIcon(new ImageIcon(img51));
oTurn = 2;
break;
case 52:
Image img52 = new ImageIcon(this.getClass()
.getResource("/bin/52.png")).getImage();
hidden.setIcon(new ImageIcon(img52));
oTurn = 2;
break;
default:
}
}
}
});
btnNewButton.setBounds(171, 227, 89, 23);
frame.getContentPane().add(btnNewButton);
}
}
}
根据您的代码,我猜您声明了一个 hidden
实例变量,并且在您的 switch
语句中,您正在向此 JLabel 添加 icon
。
但是在你的 if
条件下,你没有使用这个 hidden
变量,而是创建了一个新变量。
修复:
在您的 for 循环中, 搜索所有 JLabel hidden
字符串并将其替换为 hidden
并查看您的问题是否已解决。
我不确定您希望什么时候翻转卡片,但如果是我,我会做完全不同的事情,并且会尝试以更多 OOP 的方式做事。首先,我会创建一个 Card 对象,它由花色和等级字段组成,两者都是枚举:
public enum Suit {
CLUBS, DIAMONDS, HEARTS, SPADES
}
public enum Rank {
ACE(1), TWO(2), THREE(3), FOUR(4), FIVE(5), SIX(6), SEVEN(7), EIGHT(8), NINE(
9), TEN(10), JACK(11), QUEEN(12), KING(13);
private int value;
private Rank(int value) {
this.value = value;
}
public int getValue() {
return value;
}
}
Card.java class 将有一个 equals 和 hashCode 方法,这样我就可以在 HashMap 中使用它,从而将一个 ImageIcon 与每张卡片相关联。
public class Card {
private Suit suit;
private Rank rank;
public Card(Suit suit, Rank rank) {
this.suit = suit;
this.rank = rank;
}
public Suit getSuit() {
return suit;
}
public Rank getRank() {
return rank;
}
@Override
public String toString() {
return "Card [suit=" + suit + ", rank=" + rank + "]";
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((rank == null) ? 0 : rank.hashCode());
result = prime * result + ((suit == null) ? 0 : suit.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Card other = (Card) obj;
if (rank != other.rank)
return false;
if (suit != other.suit)
return false;
return true;
}
}
我会创建一个 Deck.java class 来容纳和洗牌所有 52 张牌,可以重新生成并可以发牌:
public class Deck {
private List<Card> cards = new ArrayList<>();
public Deck() {
regenerate();
}
public void regenerate() {
cards.clear();
for (Suit suit : Suit.values()) {
for (Rank rank : Rank.values()) {
cards.add(new Card(suit, rank));
}
}
Collections.shuffle(cards);
}
public Card deal() {
if (cards.size() > 0) {
return cards.remove(0);
} else {
// or throw exception
return null;
}
}
}
然后我会创建一个方法来获取我的卡片图像(为此我使用了公开可用的 sprite sheet)并在 HashMap 中将适当的图标分配给适当的卡片:
public static Map<Card, Icon> createCardIconMap(String pathToDeck)
throws IOException {
BufferedImage fullDeckImg = ImageIO.read(new URL(pathToDeck));
int width = fullDeckImg.getWidth();
int height = fullDeckImg.getHeight();
Map<Card, Icon> cardIconMap = new HashMap<>();
for (int suitInt = 0; suitInt < Suit.values().length; suitInt++) {
for (int rankInt = 0; rankInt < Rank.values().length; rankInt++) {
int x = (rankInt * width) / Rank.values().length;
int y = (suitInt * height) / Suit.values().length;
int w = width / Rank.values().length;
int h = height / Suit.values().length;
BufferedImage cardImg = fullDeckImg.getSubimage(x, y, w, h);
Suit suit = Suit.values()[suitInt];
Rank rank = Rank.values()[rankInt];
cardIconMap.put(new Card(suit, rank), new ImageIcon(cardImg));
}
}
return cardIconMap;
}
然后我会创建一个用于操作卡片的 GUI。如果我想用鼠标按下来翻转卡片,我会在我的 JLabel 中添加一个 MouseListener 来保存卡片图像图标。例如,
private class LabelListener extends MouseAdapter {
@Override
public void mousePressed(MouseEvent e) {
// get the pressed JLabel
JLabel label = (JLabel) e.getSource();
// find out where it is in our array of JLabel
int index = -1;
for (int i = 0; i < labels.length; i++) {
if (labels[i] == label) {
// here it is!
index = i;
break;
}
}
if (index < 0) {
// if index not found, get out of here
return;
}
// get the label's current icon
Icon icon = label.getIcon();
if (icon == null) {
// if it has none, get out of here!
return;
} else if (icon == cardBackIcon) {
// if the icon is the card-back
// then get the corresponding Card from our "model"
// here simply an array of Card called dealtCards
Card card = dealtCards[index];
if (card == null) {
// if array has nulls, get out of here!
return;
}
// else set the current label's icon to
// the icon represented by the current card
label.setIcon(cardIconMap.get(card));
} else {
// else a card is already showing, so let's show
// a card-back instead
label.setIcon(cardBackIcon);
}
}
}
运行ning 程序可能如下所示:
整个代码可能看起来像这样(请随意编译 运行 下面的程序以了解我的意思):
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.imageio.ImageIO;
import javax.swing.*;
public class CardProg {
public static final String CARD_IMAGES = "http://www.jfitz.com/cards/classic-playing-cards.png";
public static final String CARD_BACK_IMG = "http://www.jfitz.com/cards/b1fv.png";
private static void createAndShowGui() {
Map<Card, Icon> cardIconMap = null;
Icon cardBackIcon = null;
try {
cardIconMap = CreateCardFaces.createCardIconMap(CARD_IMAGES);
cardBackIcon = new ImageIcon(ImageIO.read(new URL(CARD_BACK_IMG)));
} catch (IOException e) {
e.printStackTrace();
System.exit(-1);
}
TablePanel mainPanel = new TablePanel(cardBackIcon, cardIconMap);
JFrame frame = new JFrame("CardProg");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.setResizable(false);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
@SuppressWarnings("serial")
class TablePanel extends JPanel {
private static final int PREF_W = 600;
private static final int PREF_H = 400;
private static final Color BASE_COLOR = new Color(0, 80, 0);
private static final int ROWS = 2;
private static final int COLS = 6;
private Icon cardBackIcon;
private Map<Card, Icon> cardIconMap;
private JLabel[] labels = new JLabel[ROWS * COLS];
private Card[] dealtCards = new Card[ROWS * COLS];
private Deck deck = new Deck();
public TablePanel(Icon cardBackIcon, Map<Card, Icon> cardIconMap) {
this.cardBackIcon = cardBackIcon;
this.cardIconMap = cardIconMap;
JPanel centerPanel = new JPanel(new GridLayout(ROWS, COLS));
centerPanel.setOpaque(false);
for (int i = 0; i < labels.length; i++) {
labels[i] = new JLabel();
JPanel lPanel = new JPanel(new GridBagLayout());
lPanel.setOpaque(false);
lPanel.add(labels[i]);
centerPanel.add(lPanel);
labels[i].addMouseListener(new LabelListener());;
}
JButton dealButton = new JButton(new DealAction("Deal"));
JPanel bottomPanel = new JPanel();
bottomPanel.setOpaque(false);
bottomPanel.add(dealButton);
setBackground(BASE_COLOR);
setLayout(new BorderLayout());
add(centerPanel, BorderLayout.CENTER);
add(bottomPanel, BorderLayout.PAGE_END);
}
@Override
public Dimension getPreferredSize() {
if (isPreferredSizeSet()) {
return super.getPreferredSize();
}
return new Dimension(PREF_W, PREF_H);
}
private class LabelListener extends MouseAdapter {
@Override
public void mousePressed(MouseEvent e) {
// get the pressed JLabel
JLabel label = (JLabel) e.getSource();
// find out where it is in our array of JLabel
int index = -1;
for (int i = 0; i < labels.length; i++) {
if (labels[i] == label) {
// here it is!
index = i;
break;
}
}
if (index < 0) {
// if index not found, get out of here
return;
}
// get the label's current icon
Icon icon = label.getIcon();
if (icon == null) {
// if it has none, get out of here!
return;
} else if (icon == cardBackIcon) {
// if the icon is the card-back
// then get the corresponding Card from our "model"
// here simply an array of Card called dealtCards
Card card = dealtCards[index];
if (card == null) {
// if array has nulls, get out of here!
return;
}
// else set the current label's icon to
// the icon represented by the current card
label.setIcon(cardIconMap.get(card));
} else {
// else a card is already showing, so let's show
// a card-back instead
label.setIcon(cardBackIcon);
}
}
}
private class DealAction extends AbstractAction {
public DealAction(String name) {
super(name);
int mnemonic = (int) name.charAt(0);
}
@Override
public void actionPerformed(ActionEvent e) {
deck.regenerate();
for (int i = 0; i < dealtCards.length; i++) {
dealtCards[i] = deck.deal();
labels[i].setIcon(cardBackIcon);
}
}
}
}
class Deck {
private List<Card> cards = new ArrayList<>();
public Deck() {
regenerate();
}
public void regenerate() {
cards.clear();
for (Suit suit : Suit.values()) {
for (Rank rank : Rank.values()) {
cards.add(new Card(suit, rank));
}
}
Collections.shuffle(cards);
}
public Card deal() {
if (cards.size() > 0) {
return cards.remove(0);
} else {
// or throw exception
return null;
}
}
}
enum Suit {
CLUBS, DIAMONDS, HEARTS, SPADES
}
enum Rank {
ACE(1), TWO(2), THREE(3), FOUR(4), FIVE(5), SIX(6), SEVEN(7), EIGHT(8), NINE(
9), TEN(10), JACK(11), QUEEN(12), KING(13);
private int value;
private Rank(int value) {
this.value = value;
}
public int getValue() {
return value;
}
}
class Card {
private Suit suit;
private Rank rank;
public Card(Suit suit, Rank rank) {
this.suit = suit;
this.rank = rank;
}
public Suit getSuit() {
return suit;
}
public Rank getRank() {
return rank;
}
@Override
public String toString() {
return "Card [suit=" + suit + ", rank=" + rank + "]";
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((rank == null) ? 0 : rank.hashCode());
result = prime * result + ((suit == null) ? 0 : suit.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Card other = (Card) obj;
if (rank != other.rank)
return false;
if (suit != other.suit)
return false;
return true;
}
}
class CreateCardFaces {
// method only works with
// http://www.jfitz.com/cards/classic-playing-cards.png
// sprite sheet
public static Map<Card, Icon> createCardIconMap(String pathToDeck)
throws IOException {
BufferedImage fullDeckImg = ImageIO.read(new URL(pathToDeck));
int width = fullDeckImg.getWidth();
int height = fullDeckImg.getHeight();
Map<Card, Icon> cardIconMap = new HashMap<>();
// the sprite sheet that I'm using uses this order of suits:
Suit[] suitOrder = {Suit.CLUBS, Suit.SPADES, Suit.HEARTS, Suit.DIAMONDS};
for (int suitInt = 0; suitInt < suitOrder.length; suitInt++) {
for (int rankInt = 0; rankInt < Rank.values().length; rankInt++) {
int x = (rankInt * width) / Rank.values().length;
int y = (suitInt * height) / suitOrder.length;
int w = width / Rank.values().length;
int h = height / suitOrder.length;
BufferedImage cardImg = fullDeckImg.getSubimage(x, y, w, h);
Suit suit = suitOrder[suitInt];
Rank rank = Rank.values()[rankInt];
cardIconMap.put(new Card(suit, rank), new ImageIcon(cardImg));
}
}
return cardIconMap;
}
}
好的,我想做的是制作一个名为 hidden 的图像,它将是一张面朝下的卡片。现在我想把它变成一张正面朝上的卡片,我想做的是为每队三张六张卡片制作一张图片。 "p1, p2, p3" 和 "o1, o2, o3" 这些是我给卡片面朝下的名称,我希望隐藏的 JLabel 更改或等于这些名称,然后更改为正面朝上的卡片我只放了这部分代码,因为我以为这就是您所需要的,但如果您需要查看更多代码,我可以将其放在此处。 任何帮助,将不胜感激。
public class Cards {
static int[] fullDeck = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14,
15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 52, 51, 50, 49, 48,
47, 46, 45, 44, 43, 42, 41, 40, 39, 38, 37, 36, 35, 34, 33, 32, 31,
30, 29, 28, 27, };
static int oDeck;
static int pDeck;
static int pTurn;
static int oTurn;
private JFrame frame;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Cards window = new Cards();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public Cards() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(null);
JLabel hidden = new JLabel("");
JLabel p1 = new JLabel("New label");
p1.setBounds(47, 24, 82, 82);
frame.getContentPane().add(p1);
JLabel p2 = new JLabel("New label");
p2.setBounds(139, 24, 82, 82);
frame.getContentPane().add(p2);
JLabel p3 = new JLabel("New label");
p3.setBounds(211, 24, 82, 82);
frame.getContentPane().add(p3);
JLabel o1 = new JLabel("New label");
o1.setBounds(47, 95, 82, 82);
frame.getContentPane().add(o1);
JLabel o2 = new JLabel("New label");
o2.setBounds(139, 95, 82, 82);
frame.getContentPane().add(o2);
JLabel o3 = new JLabel("New label");
o3.setBounds(211, 95, 82, 82);
frame.getContentPane().add(o3);
//This creates the players hand and the opponents hand
ArrayList<Integer> playerDeck = new ArrayList<>();
ArrayList<Integer> opponentDeck = new ArrayList<>();
for (int i = 0; i <= fullDeck.length; i++) {
if (i % 2 == 0) {
playerDeck.add(i);
} else {
opponentDeck.add(i);
}
Collections.shuffle(playerDeck);
Collections.shuffle(opponentDeck);
JButton btnNewButton = new JButton("New button");
btnNewButton.addActionListener(new ActionListener() {
public void actionPerformed(ActionEvent arg0) {for (int i = 0; i < 4; i++) {
/*if (i == 1){
pDeck = playerDeck.get(1);
hidden = p1;
hidden.setBounds(20, 200, 106, 61);
System.out.println("Check1");
}
else if (i == 2){
pDeck = playerDeck.get(2);
hidden = p2;
hidden.setBounds(162, 200, 106, 61);
System.out.println("Check2");
}
else if (i == 3){
pDeck = playerDeck.get(3);
hidden = p3;
System.out.println("Check3");
}*/
pDeck = playerDeck.get(1);
hidden = p1;
switch (pDeck) {
case 1:
Image img = new ImageIcon(this.getClass()
.getResource("/bin/1.png")).getImage();
hidden.setIcon(new ImageIcon(img));
pTurn = 14;
break;
case 2:
Image img2 = new ImageIcon(this.getClass()
.getResource("/bin/2.png")).getImage();
hidden.setIcon(new ImageIcon(img2));
pTurn = 14;
break;
case 3:
Image img3 = new ImageIcon(this.getClass()
.getResource("/bin/3.png")).getImage();
hidden.setIcon(new ImageIcon(img3));
pTurn = 14;
break;
case 4:
Image img4 = new ImageIcon(this.getClass()
.getResource("/bin/4.png")).getImage();
hidden.setIcon(new ImageIcon(img4));
pTurn = 14;
break;
case 5:
Image img5 = new ImageIcon(this.getClass()
.getResource("/bin/5.png")).getImage();
hidden.setIcon(new ImageIcon(img5));
pTurn = 13;
break;
case 6:
Image img6 = new ImageIcon(this.getClass()
.getResource("/bin/6.png")).getImage();
hidden.setIcon(new ImageIcon(img6));
pTurn = 13;
case 7:
Image img7 = new ImageIcon(this.getClass()
.getResource("/bin/7.png")).getImage();
hidden.setIcon(new ImageIcon(img7));
pTurn = 13;
break;
case 8:
Image img8 = new ImageIcon(this.getClass()
.getResource("/bin/8.png")).getImage();
hidden.setIcon(new ImageIcon(img8));
pTurn = 13;
break;
case 9:
Image img9 = new ImageIcon(this.getClass()
.getResource("/bin/9.png")).getImage();
hidden.setIcon(new ImageIcon(img9));
pTurn = 12;
break;
case 10:
Image img10 = new ImageIcon(this.getClass()
.getResource("/bin/10.png")).getImage();
hidden.setIcon(new ImageIcon(img10));
pTurn = 12;
break;
case 11:
Image img11 = new ImageIcon(this.getClass()
.getResource("/bin/11.png")).getImage();
hidden.setIcon(new ImageIcon(img11));
pTurn = 12;
break;
case 12:
Image img12 = new ImageIcon(this.getClass()
.getResource("/bin/12.png")).getImage();
hidden.setIcon(new ImageIcon(img12));
pTurn = 12;
break;
case 13:
Image img13 = new ImageIcon(this.getClass()
.getResource("/bin/13.png")).getImage();
hidden.setIcon(new ImageIcon(img13));
pTurn = 11;
break;
case 14:
Image img14 = new ImageIcon(this.getClass()
.getResource("/bin/14.png")).getImage();
hidden.setIcon(new ImageIcon(img14));
pTurn = 11;
break;
case 15:
Image img15 = new ImageIcon(this.getClass()
.getResource("/bin/15.png")).getImage();
hidden.setIcon(new ImageIcon(img15));
pTurn = 11;
break;
case 16:
Image img16 = new ImageIcon(this.getClass()
.getResource("/bin/16.png")).getImage();
hidden.setIcon(new ImageIcon(img16));
pTurn = 11;
break;
case 17:
Image img17 = new ImageIcon(this.getClass()
.getResource("/bin/17.png")).getImage();
hidden.setIcon(new ImageIcon(img17));
pTurn = 10;
break;
case 18:
Image img18 = new ImageIcon(this.getClass()
.getResource("/bin/18.png")).getImage();
hidden.setIcon(new ImageIcon(img18));
pTurn = 10;
break;
case 19:
Image img19 = new ImageIcon(this.getClass()
.getResource("/bin/19.png")).getImage();
hidden.setIcon(new ImageIcon(img19));
pTurn = 10;
break;
case 20:
Image img20 = new ImageIcon(this.getClass()
.getResource("/bin/20.png")).getImage();
hidden.setIcon(new ImageIcon(img20));
pTurn = 10;
break;
case 21:
Image img21 = new ImageIcon(this.getClass()
.getResource("/bin/21.png")).getImage();
hidden.setIcon(new ImageIcon(img21));
pTurn = 9;
break;
case 22:
Image img22 = new ImageIcon(this.getClass()
.getResource("/bin/22.png")).getImage();
hidden.setIcon(new ImageIcon(img22));
pTurn = 9;
break;
case 23:
Image img23 = new ImageIcon(this.getClass()
.getResource("/bin/23.png")).getImage();
hidden.setIcon(new ImageIcon(img23));
pTurn = 9;
break;
case 24:
Image img24 = new ImageIcon(this.getClass()
.getResource("/bin/24.png")).getImage();
hidden.setIcon(new ImageIcon(img24));
pTurn = 9;
break;
case 25:
Image img25 = new ImageIcon(this.getClass()
.getResource("/bin/25.png")).getImage();
hidden.setIcon(new ImageIcon(img25));
pTurn = 8;
break;
case 26:
Image img26 = new ImageIcon(this.getClass()
.getResource("/bin/26.png")).getImage();
hidden.setIcon(new ImageIcon(img26));
pTurn = 8;
break;
case 27:
Image img27 = new ImageIcon(this.getClass()
.getResource("/bin/27.png")).getImage();
hidden.setIcon(new ImageIcon(img27));
pTurn = 8;
break;
case 28:
Image img28 = new ImageIcon(this.getClass()
.getResource("/bin/28.png")).getImage();
hidden.setIcon(new ImageIcon(img28));
pTurn = 8;
break;
case 29:
Image img29 = new ImageIcon(this.getClass()
.getResource("/bin/29.png")).getImage();
hidden.setIcon(new ImageIcon(img29));
pTurn = 7;
break;
case 30:
Image img30 = new ImageIcon(this.getClass()
.getResource("/bin/30.png")).getImage();
hidden.setIcon(new ImageIcon(img30));
pTurn = 7;
break;
case 31:
Image img31 = new ImageIcon(this.getClass()
.getResource("/bin/31.png")).getImage();
hidden.setIcon(new ImageIcon(img31));
pTurn = 7;
break;
case 32:
Image img32 = new ImageIcon(this.getClass()
.getResource("/bin/32.png")).getImage();
hidden.setIcon(new ImageIcon(img32));
pTurn = 7;
break;
case 33:
Image img33 = new ImageIcon(this.getClass()
.getResource("/bin/33.png")).getImage();
hidden.setIcon(new ImageIcon(img33));
pTurn = 6;
break;
case 34:
Image img34 = new ImageIcon(this.getClass()
.getResource("/bin/34.png")).getImage();
hidden.setIcon(new ImageIcon(img34));
pTurn = 6;
break;
case 35:
Image img35 = new ImageIcon(this.getClass()
.getResource("/bin/35.png")).getImage();
hidden.setIcon(new ImageIcon(img35));
pTurn = 6;
break;
case 36:
Image img36 = new ImageIcon(this.getClass()
.getResource("/bin/36.png")).getImage();
hidden.setIcon(new ImageIcon(img36));
pTurn = 6;
break;
case 37:
Image img37 = new ImageIcon(this.getClass()
.getResource("/bin/37.png")).getImage();
hidden.setIcon(new ImageIcon(img37));
pTurn = 5;
break;
case 38:
Image img38 = new ImageIcon(this.getClass()
.getResource("/bin/38.png")).getImage();
hidden.setIcon(new ImageIcon(img38));
pTurn = 5;
break;
case 39:
Image img39 = new ImageIcon(this.getClass()
.getResource("/bin/39.png")).getImage();
hidden.setIcon(new ImageIcon(img39));
pTurn = 5;
break;
case 40:
Image img40 = new ImageIcon(this.getClass()
.getResource("/bin/40.png")).getImage();
hidden.setIcon(new ImageIcon(img40));
pTurn = 5;
break;
case 41:
Image img41 = new ImageIcon(this.getClass()
.getResource("/bin/41.png")).getImage();
hidden.setIcon(new ImageIcon(img41));
pTurn = 4;
break;
case 42:
Image img42 = new ImageIcon(this.getClass()
.getResource("/bin/42.png")).getImage();
hidden.setIcon(new ImageIcon(img42));
pTurn = 4;
break;
case 43:
Image img43 = new ImageIcon(this.getClass()
.getResource("/bin/43.png")).getImage();
hidden.setIcon(new ImageIcon(img43));
pTurn = 4;
break;
case 44:
Image img44 = new ImageIcon(this.getClass()
.getResource("/bin/44.png")).getImage();
hidden.setIcon(new ImageIcon(img44));
pTurn = 4;
break;
case 45:
Image img45 = new ImageIcon(this.getClass()
.getResource("/bin/45.png")).getImage();
hidden.setIcon(new ImageIcon(img45));
pTurn = 3;
break;
case 46:
Image img46 = new ImageIcon(this.getClass()
.getResource("/bin/46.png")).getImage();
hidden.setIcon(new ImageIcon(img46));
pTurn = 3;
break;
case 47:
Image img47 = new ImageIcon(this.getClass()
.getResource("/bin/47.png")).getImage();
hidden.setIcon(new ImageIcon(img47));
pTurn = 3;
break;
case 48:
Image img48 = new ImageIcon(this.getClass()
.getResource("/bin/48.png")).getImage();
hidden.setIcon(new ImageIcon(img48));
pTurn = 3;
break;
case 49:
Image img49 = new ImageIcon(this.getClass()
.getResource("/bin/49.png")).getImage();
hidden.setIcon(new ImageIcon(img49));
pTurn = 2;
break;
case 50:
Image img50 = new ImageIcon(this.getClass()
.getResource("/bin/50.png")).getImage();
hidden.setIcon(new ImageIcon(img50));
pTurn = 2;
break;
case 51:
Image img51 = new ImageIcon(this.getClass()
.getResource("/bin/51.png")).getImage();
hidden.setIcon(new ImageIcon(img51));
pTurn = 2;
break;
case 52:
Image img52 = new ImageIcon(this.getClass()
.getResource("/bin/52.png")).getImage();
hidden.setIcon(new ImageIcon(img52));
pTurn = 2;
break;
default:
}
if (i == 1){
oDeck = opponentDeck.get(1);
JLabel hidden = o1;
}
else if (i == 2){
oDeck = opponentDeck.get(2);
JLabel hidden = o2;
}
else if (i == 3){
oDeck = opponentDeck.get(3);
JLabel hidden = o3;
}
switch (oDeck) {
case 1:
Image img = new ImageIcon(this.getClass()
.getResource("/bin/1.png")).getImage();
hidden.setIcon(new ImageIcon(img));
oTurn = 14;
break;
case 2:
Image img2 = new ImageIcon(this.getClass()
.getResource("/bin/2.png")).getImage();
hidden.setIcon(new ImageIcon(img2));
oTurn = 14;
break;
case 3:
Image img3 = new ImageIcon(this.getClass()
.getResource("/bin/3.png")).getImage();
hidden.setIcon(new ImageIcon(img3));
oTurn = 14;
break;
case 4:
Image img4 = new ImageIcon(this.getClass()
.getResource("/bin/4.png")).getImage();
hidden.setIcon(new ImageIcon(img4));
oTurn = 14;
break;
case 5:
Image img5 = new ImageIcon(this.getClass()
.getResource("/bin/5.png")).getImage();
hidden.setIcon(new ImageIcon(img5));
oTurn = 13;
break;
case 6:
Image img6 = new ImageIcon(this.getClass()
.getResource("/bin/6.png")).getImage();
hidden.setIcon(new ImageIcon(img6));
oTurn = 13;
case 7:
Image img7 = new ImageIcon(this.getClass()
.getResource("/bin/7.png")).getImage();
hidden.setIcon(new ImageIcon(img7));
oTurn = 13;
break;
case 8:
Image img8 = new ImageIcon(this.getClass()
.getResource("/bin/8.png")).getImage();
hidden.setIcon(new ImageIcon(img8));
oTurn = 13;
break;
case 9:
Image img9 = new ImageIcon(this.getClass()
.getResource("/bin/9.png")).getImage();
hidden.setIcon(new ImageIcon(img9));
oTurn = 12;
break;
case 10:
Image img10 = new ImageIcon(this.getClass()
.getResource("/bin/10.png")).getImage();
hidden.setIcon(new ImageIcon(img10));
oTurn = 12;
break;
case 11:
Image img11 = new ImageIcon(this.getClass()
.getResource("/bin/11.png")).getImage();
hidden.setIcon(new ImageIcon(img11));
oTurn = 12;
break;
case 12:
Image img12 = new ImageIcon(this.getClass()
.getResource("/bin/12.png")).getImage();
hidden.setIcon(new ImageIcon(img12));
oTurn = 12;
break;
case 13:
Image img13 = new ImageIcon(this.getClass()
.getResource("/bin/13.png")).getImage();
hidden.setIcon(new ImageIcon(img13));
oTurn = 11;
break;
case 14:
Image img14 = new ImageIcon(this.getClass()
.getResource("/bin/14.png")).getImage();
hidden.setIcon(new ImageIcon(img14));
oTurn = 11;
break;
case 15:
Image img15 = new ImageIcon(this.getClass()
.getResource("/bin/15.png")).getImage();
hidden.setIcon(new ImageIcon(img15));
oTurn = 11;
break;
case 16:
Image img16 = new ImageIcon(this.getClass()
.getResource("/bin/16.png")).getImage();
hidden.setIcon(new ImageIcon(img16));
oTurn = 11;
break;
case 17:
Image img17 = new ImageIcon(this.getClass()
.getResource("/bin/17.png")).getImage();
hidden.setIcon(new ImageIcon(img17));
oTurn = 10;
break;
case 18:
Image img18 = new ImageIcon(this.getClass()
.getResource("/bin/18.png")).getImage();
hidden.setIcon(new ImageIcon(img18));
oTurn = 10;
break;
case 19:
Image img19 = new ImageIcon(this.getClass()
.getResource("/bin/19.png")).getImage();
hidden.setIcon(new ImageIcon(img19));
oTurn = 10;
break;
case 20:
Image img20 = new ImageIcon(this.getClass()
.getResource("/bin/20.png")).getImage();
hidden.setIcon(new ImageIcon(img20));
oTurn = 10;
break;
case 21:
Image img21 = new ImageIcon(this.getClass()
.getResource("/bin/21.png")).getImage();
hidden.setIcon(new ImageIcon(img21));
oTurn = 9;
break;
case 22:
Image img22 = new ImageIcon(this.getClass()
.getResource("/bin/22.png")).getImage();
hidden.setIcon(new ImageIcon(img22));
oTurn = 9;
break;
case 23:
Image img23 = new ImageIcon(this.getClass()
.getResource("/bin/23.png")).getImage();
hidden.setIcon(new ImageIcon(img23));
oTurn = 9;
break;
case 24:
Image img24 = new ImageIcon(this.getClass()
.getResource("/bin/24.png")).getImage();
hidden.setIcon(new ImageIcon(img24));
oTurn = 9;
break;
case 25:
Image img25 = new ImageIcon(this.getClass()
.getResource("/bin/25.png")).getImage();
hidden.setIcon(new ImageIcon(img25));
oTurn = 8;
break;
case 26:
Image img26 = new ImageIcon(this.getClass()
.getResource("/bin/26.png")).getImage();
hidden.setIcon(new ImageIcon(img26));
oTurn = 8;
break;
case 27:
Image img27 = new ImageIcon(this.getClass()
.getResource("/bin/27.png")).getImage();
hidden.setIcon(new ImageIcon(img27));
oTurn = 8;
break;
case 28:
Image img28 = new ImageIcon(this.getClass()
.getResource("/bin/28.png")).getImage();
hidden.setIcon(new ImageIcon(img28));
oTurn = 8;
break;
case 29:
Image img29 = new ImageIcon(this.getClass()
.getResource("/bin/29.png")).getImage();
hidden.setIcon(new ImageIcon(img29));
oTurn = 7;
break;
case 30:
Image img30 = new ImageIcon(this.getClass()
.getResource("/bin/30.png")).getImage();
hidden.setIcon(new ImageIcon(img30));
oTurn = 7;
break;
case 31:
Image img31 = new ImageIcon(this.getClass()
.getResource("/bin/31.png")).getImage();
hidden.setIcon(new ImageIcon(img31));
oTurn = 7;
break;
case 32:
Image img32 = new ImageIcon(this.getClass()
.getResource("/bin/32.png")).getImage();
hidden.setIcon(new ImageIcon(img32));
oTurn = 7;
break;
case 33:
Image img33 = new ImageIcon(this.getClass()
.getResource("/bin/33.png")).getImage();
hidden.setIcon(new ImageIcon(img33));
oTurn = 6;
break;
case 34:
Image img34 = new ImageIcon(this.getClass()
.getResource("/bin/34.png")).getImage();
hidden.setIcon(new ImageIcon(img34));
oTurn = 6;
break;
case 35:
Image img35 = new ImageIcon(this.getClass()
.getResource("/bin/35.png")).getImage();
hidden.setIcon(new ImageIcon(img35));
oTurn = 6;
break;
case 36:
Image img36 = new ImageIcon(this.getClass()
.getResource("/bin/36.png")).getImage();
hidden.setIcon(new ImageIcon(img36));
oTurn = 6;
break;
case 37:
Image img37 = new ImageIcon(this.getClass()
.getResource("/bin/37.png")).getImage();
hidden.setIcon(new ImageIcon(img37));
oTurn = 5;
break;
case 38:
Image img38 = new ImageIcon(this.getClass()
.getResource("/bin/38.png")).getImage();
hidden.setIcon(new ImageIcon(img38));
oTurn = 5;
break;
case 39:
Image img39 = new ImageIcon(this.getClass()
.getResource("/bin/39.png")).getImage();
hidden.setIcon(new ImageIcon(img39));
oTurn = 5;
break;
case 40:
Image img40 = new ImageIcon(this.getClass()
.getResource("/bin/40.png")).getImage();
hidden.setIcon(new ImageIcon(img40));
oTurn = 5;
break;
case 41:
Image img41 = new ImageIcon(this.getClass()
.getResource("/bin/41.png")).getImage();
hidden.setIcon(new ImageIcon(img41));
oTurn = 4;
break;
case 42:
Image img42 = new ImageIcon(this.getClass()
.getResource("/bin/42.png")).getImage();
hidden.setIcon(new ImageIcon(img42));
oTurn = 4;
break;
case 43:
Image img43 = new ImageIcon(this.getClass()
.getResource("/bin/43.png")).getImage();
hidden.setIcon(new ImageIcon(img43));
oTurn = 4;
break;
case 44:
Image img44 = new ImageIcon(this.getClass()
.getResource("/bin/44.png")).getImage();
hidden.setIcon(new ImageIcon(img44));
oTurn = 4;
break;
case 45:
Image img45 = new ImageIcon(this.getClass()
.getResource("/bin/45.png")).getImage();
hidden.setIcon(new ImageIcon(img45));
oTurn = 3;
break;
case 46:
Image img46 = new ImageIcon(this.getClass()
.getResource("/bin/46.png")).getImage();
hidden.setIcon(new ImageIcon(img46));
oTurn = 3;
break;
case 47:
Image img47 = new ImageIcon(this.getClass()
.getResource("/bin/47.png")).getImage();
hidden.setIcon(new ImageIcon(img47));
oTurn = 3;
break;
case 48:
Image img48 = new ImageIcon(this.getClass()
.getResource("/bin/48.png")).getImage();
hidden.setIcon(new ImageIcon(img48));
oTurn = 3;
break;
case 49:
Image img49 = new ImageIcon(this.getClass()
.getResource("/bin/49.png")).getImage();
hidden.setIcon(new ImageIcon(img49));
oTurn = 2;
break;
case 50:
Image img50 = new ImageIcon(this.getClass()
.getResource("/bin/50.png")).getImage();
hidden.setIcon(new ImageIcon(img50));
oTurn = 2;
break;
case 51:
Image img51 = new ImageIcon(this.getClass()
.getResource("/bin/51.png")).getImage();
hidden.setIcon(new ImageIcon(img51));
oTurn = 2;
break;
case 52:
Image img52 = new ImageIcon(this.getClass()
.getResource("/bin/52.png")).getImage();
hidden.setIcon(new ImageIcon(img52));
oTurn = 2;
break;
default:
}
}
}
});
btnNewButton.setBounds(171, 227, 89, 23);
frame.getContentPane().add(btnNewButton);
}
} }
根据您的代码,我猜您声明了一个 hidden
实例变量,并且在您的 switch
语句中,您正在向此 JLabel 添加 icon
。
但是在你的 if
条件下,你没有使用这个 hidden
变量,而是创建了一个新变量。
修复:
在您的 for 循环中, 搜索所有 JLabel hidden
字符串并将其替换为 hidden
并查看您的问题是否已解决。
我不确定您希望什么时候翻转卡片,但如果是我,我会做完全不同的事情,并且会尝试以更多 OOP 的方式做事。首先,我会创建一个 Card 对象,它由花色和等级字段组成,两者都是枚举:
public enum Suit {
CLUBS, DIAMONDS, HEARTS, SPADES
}
public enum Rank {
ACE(1), TWO(2), THREE(3), FOUR(4), FIVE(5), SIX(6), SEVEN(7), EIGHT(8), NINE(
9), TEN(10), JACK(11), QUEEN(12), KING(13);
private int value;
private Rank(int value) {
this.value = value;
}
public int getValue() {
return value;
}
}
Card.java class 将有一个 equals 和 hashCode 方法,这样我就可以在 HashMap 中使用它,从而将一个 ImageIcon 与每张卡片相关联。
public class Card {
private Suit suit;
private Rank rank;
public Card(Suit suit, Rank rank) {
this.suit = suit;
this.rank = rank;
}
public Suit getSuit() {
return suit;
}
public Rank getRank() {
return rank;
}
@Override
public String toString() {
return "Card [suit=" + suit + ", rank=" + rank + "]";
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((rank == null) ? 0 : rank.hashCode());
result = prime * result + ((suit == null) ? 0 : suit.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Card other = (Card) obj;
if (rank != other.rank)
return false;
if (suit != other.suit)
return false;
return true;
}
}
我会创建一个 Deck.java class 来容纳和洗牌所有 52 张牌,可以重新生成并可以发牌:
public class Deck {
private List<Card> cards = new ArrayList<>();
public Deck() {
regenerate();
}
public void regenerate() {
cards.clear();
for (Suit suit : Suit.values()) {
for (Rank rank : Rank.values()) {
cards.add(new Card(suit, rank));
}
}
Collections.shuffle(cards);
}
public Card deal() {
if (cards.size() > 0) {
return cards.remove(0);
} else {
// or throw exception
return null;
}
}
}
然后我会创建一个方法来获取我的卡片图像(为此我使用了公开可用的 sprite sheet)并在 HashMap 中将适当的图标分配给适当的卡片:
public static Map<Card, Icon> createCardIconMap(String pathToDeck)
throws IOException {
BufferedImage fullDeckImg = ImageIO.read(new URL(pathToDeck));
int width = fullDeckImg.getWidth();
int height = fullDeckImg.getHeight();
Map<Card, Icon> cardIconMap = new HashMap<>();
for (int suitInt = 0; suitInt < Suit.values().length; suitInt++) {
for (int rankInt = 0; rankInt < Rank.values().length; rankInt++) {
int x = (rankInt * width) / Rank.values().length;
int y = (suitInt * height) / Suit.values().length;
int w = width / Rank.values().length;
int h = height / Suit.values().length;
BufferedImage cardImg = fullDeckImg.getSubimage(x, y, w, h);
Suit suit = Suit.values()[suitInt];
Rank rank = Rank.values()[rankInt];
cardIconMap.put(new Card(suit, rank), new ImageIcon(cardImg));
}
}
return cardIconMap;
}
然后我会创建一个用于操作卡片的 GUI。如果我想用鼠标按下来翻转卡片,我会在我的 JLabel 中添加一个 MouseListener 来保存卡片图像图标。例如,
private class LabelListener extends MouseAdapter {
@Override
public void mousePressed(MouseEvent e) {
// get the pressed JLabel
JLabel label = (JLabel) e.getSource();
// find out where it is in our array of JLabel
int index = -1;
for (int i = 0; i < labels.length; i++) {
if (labels[i] == label) {
// here it is!
index = i;
break;
}
}
if (index < 0) {
// if index not found, get out of here
return;
}
// get the label's current icon
Icon icon = label.getIcon();
if (icon == null) {
// if it has none, get out of here!
return;
} else if (icon == cardBackIcon) {
// if the icon is the card-back
// then get the corresponding Card from our "model"
// here simply an array of Card called dealtCards
Card card = dealtCards[index];
if (card == null) {
// if array has nulls, get out of here!
return;
}
// else set the current label's icon to
// the icon represented by the current card
label.setIcon(cardIconMap.get(card));
} else {
// else a card is already showing, so let's show
// a card-back instead
label.setIcon(cardBackIcon);
}
}
}
运行ning 程序可能如下所示:
整个代码可能看起来像这样(请随意编译 运行 下面的程序以了解我的意思):
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.GridBagLayout;
import java.awt.GridLayout;
import java.awt.event.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.imageio.ImageIO;
import javax.swing.*;
public class CardProg {
public static final String CARD_IMAGES = "http://www.jfitz.com/cards/classic-playing-cards.png";
public static final String CARD_BACK_IMG = "http://www.jfitz.com/cards/b1fv.png";
private static void createAndShowGui() {
Map<Card, Icon> cardIconMap = null;
Icon cardBackIcon = null;
try {
cardIconMap = CreateCardFaces.createCardIconMap(CARD_IMAGES);
cardBackIcon = new ImageIcon(ImageIO.read(new URL(CARD_BACK_IMG)));
} catch (IOException e) {
e.printStackTrace();
System.exit(-1);
}
TablePanel mainPanel = new TablePanel(cardBackIcon, cardIconMap);
JFrame frame = new JFrame("CardProg");
frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
frame.getContentPane().add(mainPanel);
frame.setResizable(false);
frame.pack();
frame.setLocationByPlatform(true);
frame.setVisible(true);
}
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
createAndShowGui();
}
});
}
}
@SuppressWarnings("serial")
class TablePanel extends JPanel {
private static final int PREF_W = 600;
private static final int PREF_H = 400;
private static final Color BASE_COLOR = new Color(0, 80, 0);
private static final int ROWS = 2;
private static final int COLS = 6;
private Icon cardBackIcon;
private Map<Card, Icon> cardIconMap;
private JLabel[] labels = new JLabel[ROWS * COLS];
private Card[] dealtCards = new Card[ROWS * COLS];
private Deck deck = new Deck();
public TablePanel(Icon cardBackIcon, Map<Card, Icon> cardIconMap) {
this.cardBackIcon = cardBackIcon;
this.cardIconMap = cardIconMap;
JPanel centerPanel = new JPanel(new GridLayout(ROWS, COLS));
centerPanel.setOpaque(false);
for (int i = 0; i < labels.length; i++) {
labels[i] = new JLabel();
JPanel lPanel = new JPanel(new GridBagLayout());
lPanel.setOpaque(false);
lPanel.add(labels[i]);
centerPanel.add(lPanel);
labels[i].addMouseListener(new LabelListener());;
}
JButton dealButton = new JButton(new DealAction("Deal"));
JPanel bottomPanel = new JPanel();
bottomPanel.setOpaque(false);
bottomPanel.add(dealButton);
setBackground(BASE_COLOR);
setLayout(new BorderLayout());
add(centerPanel, BorderLayout.CENTER);
add(bottomPanel, BorderLayout.PAGE_END);
}
@Override
public Dimension getPreferredSize() {
if (isPreferredSizeSet()) {
return super.getPreferredSize();
}
return new Dimension(PREF_W, PREF_H);
}
private class LabelListener extends MouseAdapter {
@Override
public void mousePressed(MouseEvent e) {
// get the pressed JLabel
JLabel label = (JLabel) e.getSource();
// find out where it is in our array of JLabel
int index = -1;
for (int i = 0; i < labels.length; i++) {
if (labels[i] == label) {
// here it is!
index = i;
break;
}
}
if (index < 0) {
// if index not found, get out of here
return;
}
// get the label's current icon
Icon icon = label.getIcon();
if (icon == null) {
// if it has none, get out of here!
return;
} else if (icon == cardBackIcon) {
// if the icon is the card-back
// then get the corresponding Card from our "model"
// here simply an array of Card called dealtCards
Card card = dealtCards[index];
if (card == null) {
// if array has nulls, get out of here!
return;
}
// else set the current label's icon to
// the icon represented by the current card
label.setIcon(cardIconMap.get(card));
} else {
// else a card is already showing, so let's show
// a card-back instead
label.setIcon(cardBackIcon);
}
}
}
private class DealAction extends AbstractAction {
public DealAction(String name) {
super(name);
int mnemonic = (int) name.charAt(0);
}
@Override
public void actionPerformed(ActionEvent e) {
deck.regenerate();
for (int i = 0; i < dealtCards.length; i++) {
dealtCards[i] = deck.deal();
labels[i].setIcon(cardBackIcon);
}
}
}
}
class Deck {
private List<Card> cards = new ArrayList<>();
public Deck() {
regenerate();
}
public void regenerate() {
cards.clear();
for (Suit suit : Suit.values()) {
for (Rank rank : Rank.values()) {
cards.add(new Card(suit, rank));
}
}
Collections.shuffle(cards);
}
public Card deal() {
if (cards.size() > 0) {
return cards.remove(0);
} else {
// or throw exception
return null;
}
}
}
enum Suit {
CLUBS, DIAMONDS, HEARTS, SPADES
}
enum Rank {
ACE(1), TWO(2), THREE(3), FOUR(4), FIVE(5), SIX(6), SEVEN(7), EIGHT(8), NINE(
9), TEN(10), JACK(11), QUEEN(12), KING(13);
private int value;
private Rank(int value) {
this.value = value;
}
public int getValue() {
return value;
}
}
class Card {
private Suit suit;
private Rank rank;
public Card(Suit suit, Rank rank) {
this.suit = suit;
this.rank = rank;
}
public Suit getSuit() {
return suit;
}
public Rank getRank() {
return rank;
}
@Override
public String toString() {
return "Card [suit=" + suit + ", rank=" + rank + "]";
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((rank == null) ? 0 : rank.hashCode());
result = prime * result + ((suit == null) ? 0 : suit.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Card other = (Card) obj;
if (rank != other.rank)
return false;
if (suit != other.suit)
return false;
return true;
}
}
class CreateCardFaces {
// method only works with
// http://www.jfitz.com/cards/classic-playing-cards.png
// sprite sheet
public static Map<Card, Icon> createCardIconMap(String pathToDeck)
throws IOException {
BufferedImage fullDeckImg = ImageIO.read(new URL(pathToDeck));
int width = fullDeckImg.getWidth();
int height = fullDeckImg.getHeight();
Map<Card, Icon> cardIconMap = new HashMap<>();
// the sprite sheet that I'm using uses this order of suits:
Suit[] suitOrder = {Suit.CLUBS, Suit.SPADES, Suit.HEARTS, Suit.DIAMONDS};
for (int suitInt = 0; suitInt < suitOrder.length; suitInt++) {
for (int rankInt = 0; rankInt < Rank.values().length; rankInt++) {
int x = (rankInt * width) / Rank.values().length;
int y = (suitInt * height) / suitOrder.length;
int w = width / Rank.values().length;
int h = height / suitOrder.length;
BufferedImage cardImg = fullDeckImg.getSubimage(x, y, w, h);
Suit suit = suitOrder[suitInt];
Rank rank = Rank.values()[rankInt];
cardIconMap.put(new Card(suit, rank), new ImageIcon(cardImg));
}
}
return cardIconMap;
}
}