如何在記憶游戲中添加圖片?我把它做成了一個數字游戲,現在想添加圖片,以便玩家可以玩圖片翻轉記憶游戲。我附上了代碼,Board.java 是需要更改的地方。我真的堅持這一點,任何幫助將不勝感激。
代碼.java
import javax.swing.JButton;
public class Card extends JButton{
private String id;
private boolean matched = false;
public void setId(String val){
this.id = val;
}
public String getId(){
return this.id;
}
public void setMatched(boolean matched){
this.matched = matched;
}
public boolean getMatched(){
return this.matched;
}
}
游戲.java
import java.awt.Dimension;
import javax.swing.JFrame;
public class Game{
public static void main(String[] args){
Board b = new Board();
b.setPreferredSize(new Dimension(500,500)); //need to use this instead of setSize
b.setLocation(500, 250);
b.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
b.pack();
b.setVisible(true);
}
}
Board.java
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.Timer;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Collections;
public class Board extends JFrame{
private List<Card> cards;
private Card selectedCard;
private Card c1;
private Card c2;
private Timer t;
static String fruits[] = {"pear.jpg", "peach.jpg", "pineapple.jpg", "/apple.jpg",
"avocado.jpg", "/greenapple.jpg"};
static String files[] = fruits;
public Board(){
int pairs = 14;
List<Card> cardsList = new ArrayList<Card>();
List<String> cardVals = new ArrayList<String>();
for (int i = 0; i < pairs; i ){
cardVals.add(i, fruits[i]);
cardVals.add(i, fruits[i]);
}
Collections.shuffle(cardVals);
for (String val : cardVals){
Card c = new Card();
c.setId(val);
c.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
selectedCard = c;
doTurn();
}
});
cardsList.add(c);
}
this.cards = cardsList;
//set up the timer
t = new Timer(750, new ActionListener(){
public void actionPerformed(ActionEvent ae){
checkCards();
}
});
t.setRepeats(false);
//set up the board itself
Container pane = getContentPane();
pane.setLayout(new GridLayout(4, 5));
for (Card c : cards){
pane.add(c);
}
}
public void doTurn(){
if (c1 == null && c2 == null){
c1 = selectedCard;
c1.setText(String.valueOf(c1.getId()));
}
if (c1 != null && c1 != selectedCard && c2 == null){
c2 = selectedCard;
c2.setText(String.valueOf(c2.getId()));
t.start();
}
}
public void checkCards(){
if (c1.getId() == c2.getId()){//match condition
c1.setEnabled(false); //disables the button
c2.setEnabled(false);
c1.setMatched(true); //flags the button as having been matched
c2.setMatched(true);
if (this.isGameWon()){
JOptionPane.showMessageDialog(this, "You win!");
System.exit(0);
}
}
else{
c1.setText(""); //'hides' text
c2.setText("");
}
c1 = null; //reset c1 and c2
c2 = null;
}
public boolean isGameWon(){
for(Card c: this.cards){
if (c.getMatched() == false){
return false;
}
}
return true;
}
}
uj5u.com熱心網友回復:
方法的第一行checkCards是:
if (c1.getId() == c2.getId()){//match condition
它應該是:
if (c1.getId().equals(c2.getId())){
請參閱如何在 Java 中比較字串?
您不需要擴展JButton的類。一個組件有一個名字,所以使用方法setName(String)而getName()不是添加一個 ID 欄位。此外,JComponent支持自定義屬性,因此將matched標志設為自定義屬性。
第一個for回圈在 class 的建構式中Board拋出,ArrayIndexOutOfBoundsException因為 array 中只有六個影像fruits。我假設您希望每個影像出現在兩張不同的卡片上。回圈應該是:
for (int i = 0; i < fruits.length; i ){
cardVals.add(fruits[i]);
cardVals.add(fruits[i]);
}
有關方法add(E)和add(int, E). _ 方法add(E)只是添加到串列的末尾。
所以現在你希望JButtons 顯示一個圖示而不是文本。所以首先你需要創建圖示。最初,沒有一個JButtons 有一個圖示。然后,當用戶單擊JButton您要為其添加適當的圖示時。您還需要能夠洗掉該圖示。你可以通過方法來做到這一點setIcon。如果方法引數為空,則洗掉圖示,當引數不為空時,將圖示添加到JButton.
我使用 aMap將 aJButton與其圖示相關聯。Map鍵是類中的ID屬性,值是實際圖示。CardMap
為了簡單起見,我將所有圖示檔案添加到與您的Board類相同的包中。有關在Swing中創建圖示的更多詳細資訊,請參閱如何使用圖示。
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.Timer;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Collections;
import java.util.HashMap;
public class Board extends JFrame {
private static final String MATCHED = "MATCHED";
private static final Map<String, Icon> ICONS;
private List<JButton> cards;
private JButton selectedCard;
private JButton c1;
private JButton c2;
private Timer t;
static String fruits[] = {"pear.png",
"peach.png",
"pineapple.png",
"apple.png",
"avocado.png",
"greenapple.png"};
static String files[] = fruits;
static {
ICONS = new HashMap<>(fruits.length);
Class<?> theClass = Board.class;
for (int i = 0; i < fruits.length; i ) {
ICONS.put(fruits[i], new ImageIcon(theClass.getResource(fruits[i])));
}
}
public Board() {
List<JButton> cardsList = new ArrayList<JButton>();
List<String> cardVals = new ArrayList<String>();
for (int i = 0; i < fruits.length; i ){
cardVals.add(fruits[i]);
cardVals.add(fruits[i]);
}
Collections.shuffle(cardVals);
for (String val : cardVals){
JButton c = new JButton();
c.setName(val);
c.putClientProperty(MATCHED, Boolean.FALSE);
c.addActionListener(new ActionListener(){
public void actionPerformed(ActionEvent ae){
selectedCard = c;
doTurn();
}
});
cardsList.add(c);
}
this.cards = cardsList;
//set up the timer
t = new Timer(750, new ActionListener(){
public void actionPerformed(ActionEvent ae){
checkCards();
}
});
t.setRepeats(false);
//set up the board itself
Container pane = getContentPane();
pane.setLayout(new GridLayout(4, 5));
for (JButton c : cards){
pane.add(c);
}
}
public void doTurn(){
if (c1 == null && c2 == null){
c1 = selectedCard;
c1.setIcon(ICONS.get(c1.getName()));
}
if (c1 != null && c1 != selectedCard && c2 == null){
c2 = selectedCard;
c2.setIcon(ICONS.get(c2.getName()));
t.start();
}
}
public void checkCards(){
if (c1.getName().equals(c2.getName())) { //match condition
c1.setEnabled(false); //disables the button
c2.setEnabled(false);
c1.putClientProperty(MATCHED, Boolean.TRUE); //flags the button as having been matched
c2.putClientProperty(MATCHED, Boolean.TRUE);
if (this.isGameWon()){
JOptionPane.showMessageDialog(this, "You win!");
System.exit(0);
}
}
else{
c1.setIcon(null); //'hides' text
c2.setIcon(null);
}
c1 = null; //reset c1 and c2
c2 = null;
}
public boolean isGameWon() {
for (JButton c : cards) {
if (c.getClientProperty(MATCHED) == Boolean.FALSE) {
return false;
}
}
return true;
}
public static void main(String[] args) {
Board b = new Board();
b.setPreferredSize(new Dimension(500,500)); //need to use this instead of setSize
b.setLocation(500, 250);
b.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
b.pack();
b.setVisible(true);
}
}
請注意,我從 Internet 下載了自己的圖示,因此名稱與您的名稱不完全匹配。Board.class另外,我將所有PNG 檔案與file (即編譯Java 源代碼時創建的檔案)放在同一目錄中。
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/508246.html
