基本上问题是我完全能够处理一张卡片,我只是无法在框架中显示所述卡片。应该发生的是每次用户想要发卡时,它将显示在控制台窗口中,并且卡的图像也将显示在框架中。图像本身标题为“5d.gif”或“ah.gif”,它们与控制台窗口中的输出相对应,因为当用户要求发卡时,发出的卡然后以“5d”或“格式”显示啊”。我只是错过了卡片处理的部分,我相信我已经接近获得解决方案,基于评论者在我上一篇文章中提出的地图解决方案。我在TheFrame类中遇到一些错误,cardDisplay方法中的“cards”“无法解析为变量”。此外,在paintComponent方法中,“image”也“无法解析为变量”。另外,在我的Game类中,在我的drawCards方法下,它表示cardDisplay“未定义类型TheFrame”。我已将下面的4个课程更好地展示了正在发生的事情:
卡 -
package uk.ac.aber.dcs.cs12320.cards;
public class Card {
public String number;
public String suit;
public Card(String n, String s) {
number = n;
suit = s;
}
@Override
public String toString() {
return number + suit;
}
}
游戏 -
import java.io.IOException;
import java.util.ArrayList;
import java.util.Scanner;
import uk.ac.aber.dcs.cs12320.cards.Card;
import uk.ac.aber.dcs.cs12320.cards.gui.TheFrame;
public class Game {
private Scanner scan;
private Deck deck;
private TheFrame frame;
private ArrayList<Card> onTable = new ArrayList<Card>();
public Game() {
frame = new TheFrame();
deck = new Deck();
try {
deck.buildDeck();
} catch (IOException e) {
System.err.println("Error reading in deck...");
System.exit(-1);
}
}
private void runMenu() throws IOException {
String response;
do {
printMenu();
System.out.println("What would you like to do:");
scan = new Scanner(System.in);
response = scan.nextLine().toUpperCase();
switch (response) {
case "1":
PrintDeck();
break;
case "2":
ShuffleCards();
break;
case "3":
DealCard();
break;
case "4":
MoveToPrevious();
break;
case "5":
Move2PilesBack();
break;
case "6":
AmalgamateInMiddle();
break;
case "7":
PlayforMe();
break;
case "8":
ShowLowScores();
case "Q":
break;
default:
System.out.println("Try again");
}
drawCards();
} while (!(response.equals("Q")));
}
private void ShowLowScores() {
// TODO Auto-generated method stub
}
private void PlayforMe() {
// TODO Auto-generated method stub
}
private void AmalgamateInMiddle() {
// TODO Auto-generated method stub
}
private void Move2PilesBack() {
// TODO Auto-generated method stub
}
private void MoveToPrevious() {
// TODO Auto-generated method stub
}
private void DealCard() {
Card c = deck.removeTopCard();
System.out.println(c);
}
private void ShuffleCards() {
deck.shuffle();
}
private void drawCards() {
ArrayList<String> visibleCards = new ArrayList<String>();
for (Card card : onTable) {
visibleCards.add(card.number + card.suit + ".gif");
}
frame.cardDisplay(visibleCards);
}
private void PrintDeck() throws IOException {
for (Card card : deck.getDeck()) {
System.out.println(card);
}
}
private void printMenu() {
System.out.println("1 - Print the pack ");
System.out.println("2 - Shuffle");
System.out.println("3 - Deal a card");
System.out.println("4 - Move last pile onto previous one");
System.out.println("5 - Move last pile back over two piles");
System.out.println("6 - Amalgamate piles in the middle");
System.out.println("7 - Play for me");
System.out.println("8 - Show low scores");
System.out.println("q - Quit");
}
public static void main(String args[]) throws IOException {
System.out.println("****Welcome to patience is virtue****");
Game cardsgame = new Game();
cardsgame.runMenu();
System.out.println("****Thanks for playing****");
}
}
甲板 -
import java.util.Collections;
import java.util.List;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import uk.ac.aber.dcs.cs12320.cards.Card;
public class Deck {
private ArrayList<Card> cards;
public Deck() {
cards = new ArrayList<Card>();
}
public void buildDeck() throws IOException {
List<String> cardLines = Files.readAllLines(Paths.get("cards.txt"));
for (int i = 0; i < cardLines.size(); i += 2) {
// System.out.println()
cards.add(new Card(cardLines.get(i), cardLines.get(i + 1)));
}
}
public Card removeTopCard() {
return cards.remove(0);
}
public List<Card> getDeck() {
return cards;
}
public void shuffle() {
Collections.shuffle(cards);
System.out.println(cards);
}
}
TheFrame -
package uk.ac.aber.dcs.cs12320.cards.gui;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.Toolkit;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import javax.swing.JFrame;
import javax.swing.JPanel;
import uk.ac.aber.dcs.cs12320.cards.Card;
public class TheFrame extends JFrame {
public static boolean paintComponent;
private ThePanel canvas;
/**
* The constructor creates a Frame ready to display the cards
*/
public TheFrame() {
// Calls the constructor in the JFrame superclass passing up the name to
// display in the title
super("Becky's Patience");
// When you click on the close window button the window will be closed
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// This has North, East, South, West and Center positions for components
setLayout(new BorderLayout());
// This is what we will draw on (see the inner class below)
canvas = new ThePanel(null);
setSize(700, 300);
this.add(canvas, BorderLayout.CENTER);
setVisible(true); // Display the window
}
/**
* Displays all cards
*
* @param cards
* an arraylist of strings of the form 3h.gif for 3 of hearts
*/
/*public void cardDisplay(ArrayList<String> cards) {
canvas.cardDisplay(cards);
}*/
/**
* Call before cardDisplay at end of game (takes away the unused pile)
*/
public void allDone() {
canvas.allDone();
}
// /////////////////////////////////////////////////
/*
* This is an example of an inner class (like Russian dolls)
* It private so can only be seen by the outer class. It's part
* of the implementation of TheFrame. Because it extends JPanel we
* can draw on it
*/
Map<Card, Image> loadCards(ArrayList<Card> cards)
{
Map<Card, Image> cardImages = new HashMap<>();
for (Card card : cards)
{
String file = "cards/" + card.number + card.suit + ".gif";
Image image = Toolkit.getDefaultToolkit().getImage(file);
cardImages.put(card, image);
}
return cardImages;
}
private class ThePanel extends JPanel {
private Map<Card, Image> cardImages;
private ArrayList<Card> currentCardDisplayed;
private boolean done;
private ThePanel(ArrayList<Card> cards) {
setBackground(Color.cyan);
done = false;
cardImages = loadCards(cards);
}
private void cardDisplay(ArrayList<String> c) {
cards = c;
repaint();
}
private void allDone() {
done = true;
}
/**
* This is called automatically by Java when it want to draw this panel.
* So we have to put our drawing command in here.
* @param g Is the graphics object on which we draw.
*/
@Override
public void paintComponent(Graphics g) {
// Always do this. It's giving the JPanel superclass a change to
// paint its parts before we paint ours. E.g. we don't draw the
// edge of the window, one of the super-classes does that.
super.paintComponent(g);
int x = 20;
int y = 50;
// Loop through all the cards get each image in turn
for (Card card: currentCardDisplayed) {
g.drawImage(cardImages.get(card), x, y, 70, 100, this);
x += 72; // The x position is moved on in order to position the next card
// This could be improved by having a horizontal scroll bar
}
if (!done) {
// Draws the face-down top card of our pack of cards
String file = "cards/b.gif";
image = Toolkit.getDefaultToolkit().getImage(file);
g.drawImage(image, 100, 152, 70, 100, this);
}
}
} // ThePanel inner class
这是我试图映射GIF的地方: 以下是当我在我的GUI中按案例3来处理一张卡时出现的情况(但由于错误而当前未运行):
感谢您在修复我目前所遇到的错误方面提供的任何帮助,我相信我已经接近了基于已经处理的GUI卡在框架上显示卡片的解决方案。
答案 0 :(得分:1)
这看起来很像很多像家庭作业......所以我会保持我的答案有点概念性。
基本上你有这副卡片要从中删除卡片,并让应用程序的其他部分在事件发生时作出响应。通常有两种模式来处理这种情况。
您可以使用DealCard
移除顶部卡片的消息传递框架,然后将消息发布到某种类型的总线,然后让所有想要了解此消息的人:这是卡片画。然后你有TheFrame
和其他处理控制台处理卡片的东西。我假设这是一个家庭作业,所以也许这有点超过顶部...如果它不在这里我喜欢的消息库(奖金适用于Android: http://square.github.io/otto/)
您可能想要做的另一件事是创建类似
的界面public interface CardDrawnListener {
void onCardDrawn(final Card card);
}
然后让您的牌组跟踪List<CardDrawnListeners>
。然后你需要让框架添加一个新的CardDrawnListener
和一些处理控制台的东西(你可以像你一样在线进行)。
现在你的TheFrame
知道什么时候从牌组中抽出一张牌并且可以用它做任何想做的事情。使用这种模式时要非常小心,不要在很多地方复制状态(跟踪卡片中的哪些卡片)... Deck
是知道卡片中哪些卡片在哪个卡片中的位置
编译错误问题:
TheFrame类中的错误,&#34;卡&#34;在cardDisplay方法&#34;无法解析为变量&#34;
变量cards
是传递给ThePanel
的参数,并没有作为成员变量存储在任何地方......我假设你要么拥有一个成员变量private List<String> cards
ThePanel
1}}或者你想不存储那个状态......)
在paintComponent方法中,&#34; image&#34;也&#34;无法解决变量&#34;
没有变量image
,所以你只需要在那里定义一个局部变量。应该像改变一样简单:
image = Toolkit.getDefaultToolkit().getImage(file);
到
Image image = Toolkit.getDefaultToolkit().getImage(file);
在我的Game类中,在我的drawCards方法下,它表示cardDisplay&#34;未定义类型TheFrame&#34;
它说您试图在cardDisplay
对象上调用TheFrame
方法,但这不是该对象的方法。因此,您应该进入TheFrame
并制作名为cardDisplay
的方法。该方法可能只想调用ThePanel
s cardDisplay
方法。