我用纸牌写游戏。而且我可以用字符串输出。但是,我想将字符串的显示更改为图像。我可以使用 JFrame 并GridLayout
在 2*2 图像中显示四张卡片。
我的问题是我有一个用于卡片组的 .png,我怎样才能相应地获得单独的卡片?
班级Card
package Chapter7;
public class Card {
Suit suit;
Rank rank;
public Card(Suit suit, Rank rank) {
this.suit = suit;
this.rank = rank;
}
public Rank getRank() {
return this.rank;
}
public Suit getSuit() {
return this.suit;
}
public String toString() {
return this.rank + " of " + this.suit;
}
public enum Suit {
CLUBS(1), SPADE(2), HEARTS(3), DIAMONDS(4);
int value;
Suit(int v) {
this.value = v;
}
int getSuit() {
return this.value;
}
}
public enum Rank {
ACE(1), DEUCE(2), THREE(3), FOUR(4), FIVE(5), SIX(6), SEVEN(7), EIGHT(8), NINE(9), TEN(10), JACK(11), QUEEN(12), KING(13);
int rank;
Rank(int num) {
this.rank = num;
}
int getNumber() {
return this.rank;
}
}
}
班级Deck
package Chapter7;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import Chapter7.Card.Rank;
import Chapter7.Card.Suit;
public class Deck {
List <Card> deck = new ArrayList <Card> ();
Deck() {
for(Suit s : Suit.values()) {
for(Rank r : Rank.values()) {
deck.add(new Card(s, r));
}
}
// System.out.println("A new deck created!!!");
// for(Card c : this.deck) {
// System.out.println(c.toString());
// }
}
public void shuffle(){
this.deck.clear();
Deck ret = new Deck();
for(int i = 0; i < 52; i++) {
int j = (int) (Math.random() * (52-i));
deck.add(ret.deck.remove(j));
}
// System.out.println("After shuffle!!!");
// for(Card c : this.deck) {
// System.out.println(c.toString());
// }
}
}
package Chapter7;
import java.io.IOException;
public class Game {
public static void main(String args[]) throws IOException {
Deck deck = new Deck();
deck.shuffle();
int aGame = 4;
for(int i = 0; i < 52; i++) {
if(aGame == 1) {
System.out.println(deck.deck.get(i).toString());
System.out.println("Start!!!");
while (true) {
char c = (char) System.in.read();
if (c == '\n') {
break;
}
}
aGame = 4;
// if(i == 51) {
// deck.shuffle();
// i = -1;
// }
}
else if(aGame == 4) {
System.out.println("Calculate based on the following four cards!");
System.out.println(deck.deck.get(i).toString());
aGame --;
}
else {
System.out.println(deck.deck.get(i).toString());
aGame --;
}
}
}
}
班级LoadImageApp
在 2*2 中加载图像的类GridLayout
。
package Chapter7;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.io.*;
import javax.imageio.*;
import javax.swing.*;
/**
* This class demonstrates how to load an Image from an external file
*/
public class LoadImageApp extends Component {
BufferedImage img;
public void paint(Graphics g) {
g.drawImage(img, 0, 0, null);
}
public LoadImageApp(String s) {
try {
img = ImageIO.read(new File(s));
} catch (IOException e) {
}
}
public Dimension getPreferredSize() {
if (img == null) {
return new Dimension(100,100);
} else {
return new Dimension(img.getWidth(null), img.getHeight(null));
}
}
public static void main(String[] args) {
JFrame f = new JFrame("Load Image Sample");
GridLayout grid = new GridLayout(2, 2);
f.setLayout(grid);
f.addWindowListener(new WindowAdapter(){
public void windowClosing(WindowEvent e) {
System.exit(0);
}
});
f.add(new LoadImageApp("27.jpg"));
f.add(new LoadImageApp("hw6.jpg"));
f.add(new LoadImageApp("27.jpg"));
f.add(new LoadImageApp("hw6.jpg"));
f.pack();
f.setVisible(true);
}
}
我应该如何更改这两个类来显示?
带有纸牌的 .png 在这里。