我无法显示卡片的图片。只是图像没有显示出来。其他一切都是正确的。 这是所有相关文件。 摘要播放器
public abstract class AbstractPlayer implements Playerable {
private ArrayList<Card> hand;
private int winCount;
public AbstractPlayer()
{
hand = new ArrayList<Card>();
winCount = 0;
}
public AbstractPlayer(int score)
{
winCount = score;
}
public void addCardToHand(Card temp)
{
hand.add(temp);
}
public void resetHand() {
hand.clear();
}
public void setWinCount(int numwins)
{
winCount = numwins;
}
public int getWinCount()
{
return winCount;
}
public int getHandSize()
{
return hand.size();
}
public ArrayList<Card> getHand()
{
return hand;
}
public int getHandValue()
{
int sum = 0;
for(Card c: hand)
sum = sum + c.getValue();
return sum;
}
public String toString()
{
return "hand = " + hand.toString() + " \n- # wins " + winCount;
}
public void drawHand(Graphics window, int x, int y)
{
window.create(x, y, 90, 120);
}
}
播放器 public class Player扩展了AbstractPlayer { //构造 公共玩家() { 超(); }
public Player(int score)
{
super(score);
}
public boolean hit()
{
//no code needed with GUI version
return false;
}
}
PlayerTestTwo
public class PlayerTestTwo extends JPanel implements KeyListener {
private Deck deck;
private Player player;
private Font font;
private boolean start;
private boolean hit;
private boolean finish;
private boolean show;
public PlayerTestTwo() {
setBackground(new Color(34, 139, 34));
deck = new Deck();
player = new Player();
font = new Font("TAHOMA", Font.BOLD, 12);
this.addKeyListener(this); //starts the key thread running
}
public void paintComponent(Graphics window) {
super.paintComponent(window);
window.setColor(Color.black);
window.setFont(font);
window.drawString("BlackJack Player Test", 25, 50);
window.drawString("PRESS B to add cards to your hand.", 25, 100);
if (start == true) {
deck.shuffle();
player.addCardToHand(deck.nextCard());
player.addCardToHand(deck.nextCard());
start = false;
show = false;
}
window.drawString("PLAYER ", 50, 165);
player.drawHand(window, 10, 185);
}
public void keyTyped(KeyEvent e) {
if (e.getKeyChar() == 'b' || e.getKeyChar() == 'B') {
start = true;
repaint();
}
if (Character.toLowerCase(e.getKeyChar()) == 'r'
|| Character.toLowerCase(e.getKeyChar()) == 'q') {
deck = new Deck();
player = new Player();
repaint();
}
}
public void keyPressed(KeyEvent e) {
}
public void keyReleased(KeyEvent e) {
}
}
Test File
import javax.swing.JFrame;
import java.awt.Component;
public class CardDeckPlayerGraphicsRunner extends JFrame {
private static final int WIDTH = 800;
private static final int HEIGHT = 600;
public CardDeckPlayerGraphicsRunner() {
super("BLACKJACK CardDeckPlayerRunner");
setSize(WIDTH, HEIGHT);
//test the Card class
CardTestTwo cardTest = new CardTestTwo();
getContentPane().add(cardTest);
//test the Deck class
DeckTestTwo deckTest = new DeckTestTwo();
((Component)deckTest).setFocusable(true);
getContentPane().add(deckTest);
//test the Player class
PlayerTestTwo playerTest = new PlayerTestTwo();
((Component)playerTest).setFocusable(true);
getContentPane().add(playerTest);
setVisible(true);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String args[]) {
CardDeckPlayerGraphicsRunner run = new CardDeckPlayerGraphicsRunner();
}
}