所以本质上来说,问题是我完全有能力发行一张卡,但我无法在框架中显示该卡。应该发生的是,每当用户希望发卡时,它将显示在控制台窗口中,并且卡的图像也将显示在框架中。图像本身的标题为“ 5d.gif”或“ ah.gif”,它们与控制台窗口中的输出相对应,因为当用户要求发卡时,然后以“ 5d”或“啊”。我只是想念这张发牌的部分,我很有信心我将要获得解决方案,它是基于评论者在我上一篇文章中所建议的地图解决方案的。我在TheFrame类中遇到一些错误,cardDisplay方法中的“ cards”“无法解析为变量”。同样,在paintComponent方法中,“图像”也“无法解析为变量”。另外,在我的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);
	}

}





框架-



     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卡在框架上显示卡的解决方案。

最佳答案

这看起来很像作业……所以我将答案保持概念化。

基本上,您具有要从中删除卡片的卡片组,并且应用程序的其他部分可以在发生该事件时做出响应。通常有两种模式可以处理这种情况。

您可以使用消息传递框架,其中DealCard除去顶部的卡,然后将消息发布到某种总线上,然后让每个想要了解此消息的人:这是绘制的卡。然后,您将拥有TheFrame和其他可通过控制台处理的东西来处理卡。我假设这是一项家庭作业,所以可能有点过头了……如果不是,这是我喜欢的消息传递库(奖金在Android:http://square.github.io/otto/上很好用)

您可能想要做的另一件事是创建一个类似

public interface CardDrawnListener {
    void onCardDrawn(final Card card);
}


然后让您的牌组记录List<CardDrawnListeners>。然后,您需要在框架中添加一个新的CardDrawnListener和一些可以处理控制台的内容(您可以像使用它那样直接进行内联)。

现在,您的TheFrame会知道何时从卡组中抽出一张牌,并可以用它进行任何操作。使用这种模式时,请务必小心,不要在很多地方复制状态(跟踪卡中有哪些卡)... Deck是知道哪些卡在卡中以及哪些不在卡中的地方。

编译错误问题:


TheFrame类中的错误,cardDisplay方法中的“ cards”“无法解析为变量”


变量cards是传递给ThePanel的参数,并且不会在任何位置存储为成员变量...我假设您要么想拥有一个成员变量(private List<String> cards上的ThePanel,要么不想存储该状态...)


在paintComponent方法中,“图像”也“无法解析为变量”


没有变量image,因此您只需要在其中定义一个局部变量。应该像更改一样容易:

image = Toolkit.getDefaultToolkit().getImage(file);




Image image = Toolkit.getDefaultToolkit().getImage(file);



在我的游戏类中,在我的drawCards方法下,它说cardDisplay“未为TheFrame类型定义”


就是说,您正在尝试在cardDisplay对象上调用方法TheFrame,但这不是该对象上的方法。因此,您应该进入TheFrame并创建一个名为cardDisplay的方法。该方法可能只想调用ThePanelcardDisplay方法。

10-07 19:09