【发布时间】:2014-05-12 11:41:05
【问题描述】:
我已经在 Java 中开发了一个半星期的记忆游戏,知道它基本上完成了,但是我在面板上随机化我的 jbuttons 时遇到了问题,它们只出现在列表顺序中。那么,随机化或随机化设置到每个按钮的图像和值的最佳方法是什么?我一直想知道它是一个静态网格布局,所以我无法更改它们的位置,我想我在不久前得到了图像在板上随机更改,但我不知道如何将值与图像匹配,以便游戏正常运行。
package concentrate;
import static concentrate.HighscoreManager.scores;
import static concentrate.Score.nameField;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.*;
import java.util.logging.Logger;
import javax.swing.*;
public final class Concentrate extends JFrame implements ActionListener {
//below are all the variables declared for the gui and the game program itself
private JFrame window = new JFrame("Concentrate");
private static final int WINDOW_WIDTH = 330;
private static final int WINDOW_HEIGHT = 485;
private JButton exitBtn, scoresBtn;
ImageIcon ButtonIcon = createImageIcon("/resources/images/b1fv.gif");
ImageIcon[] ImageArray = new ImageIcon[16];
public static JButton[] gameBtn = new JButton[16];
private ArrayList<Integer> gameList = new ArrayList<Integer>();
public static int Point = 46;
private int counter = 0;
private int cards = 16;
private int[] btnID = new int[2];
private int[] btnValue = new int[2];
private JLabel Score;
private JLabel Fail;
private Panel gamePnl = new Panel();
private Panel buttonPnl = new Panel();
private Panel scorePnl = new Panel();
private Icon[] iconSet;
Random rand = new Random();
//this function below creates the image icon to be displayed on each card/button when it is enabled
protected static ImageIcon createImageIcon(String path) {
java.net.URL imgURL = Concentrate.class.getResource(path);
if (imgURL != null) {
return new ImageIcon(imgURL);
} else {
System.err.println("Couldn't find file: " + path);
return null;
}
}
//update score checks and sets the score on the main board
public void updateScore() {
Score.setText("Score: " + Point);
}
public Concentrate()
{
//below is calling the create gui function and set variables for it width and close operation
createGUI();
createpanels();
setArrayListText();
window.setTitle("Concentrate");
window.setDefaultCloseOperation(EXIT_ON_CLOSE);
window.setSize(WINDOW_WIDTH, WINDOW_HEIGHT);
window.setVisible(true);
//below creates the button faces for when they are disabled it will display the icon
gameBtn[0].setDisabledIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/images/c1.gif")));
gameBtn[1].setDisabledIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/images/c2.gif")));
gameBtn[2].setDisabledIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/images/c3.gif")));
gameBtn[3].setDisabledIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/images/c4.gif")));
gameBtn[4].setDisabledIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/images/c5.gif")));
gameBtn[5].setDisabledIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/images/c6.gif")));
gameBtn[6].setDisabledIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/images/c7.gif")));
gameBtn[7].setDisabledIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/images/c8.gif")));
gameBtn[8].setDisabledIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/images/d1.gif")));
gameBtn[9].setDisabledIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/images/d2.gif")));
gameBtn[10].setDisabledIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/images/d3.gif")));
gameBtn[11].setDisabledIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/images/d4.gif")));
gameBtn[12].setDisabledIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/images/d5.gif")));
gameBtn[13].setDisabledIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/images/d6.gif")));
gameBtn[14].setDisabledIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/images/d7.gif")));
gameBtn[15].setDisabledIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/images/d8.gif")));
}
//this is the create gui function which declares the gui variables and items to be set
public void createGUI()
{
for (int i = 0; i < gameBtn.length; i++)
{
gameBtn[i] = new JButton(ButtonIcon);
gameBtn[i].addActionListener(this);
}
Score = new JLabel("Score: " + Point);
exitBtn = new JButton("Exit");
exitBtn.addActionListener(this);
scoresBtn = new JButton("Leaderboard");
scoresBtn.addActionListener(this);
}
//createpanels sets diffrent panels within windows to add the buttons and score on organised from top, main and bottom
public void createpanels()
{
gamePnl.setLayout(new GridLayout(4, 4));
for (int i = 0; i < gameBtn.length; i++)
{
gamePnl.add(gameBtn[i]);
}
buttonPnl.add(scoresBtn);
buttonPnl.add(exitBtn);
window.setResizable(false);
buttonPnl.setLayout(new GridLayout(1, 0));
scorePnl.add(Score);
scorePnl.setLayout(new GridLayout(1, 0));
window.add(scorePnl, BorderLayout.NORTH);
window.add(gamePnl, BorderLayout.CENTER);
window.add(buttonPnl, BorderLayout.SOUTH);
}
// the function below checks the cards/buttons for when they are eqaul the same value
public boolean sameValues()
{
if (btnValue[0] == btnValue[1])
{
return true;
}else{
return false;
}
}
@Override
public void actionPerformed(ActionEvent e)
{
//this is the exit button displayed onthe bottomof the gui
if (exitBtn == e.getSource())
{
System.exit(0);
}
//this is the button at the botto of the gui which calls on the leaderboard frame
if (scoresBtn == e.getSource())
{
new Leaderboard().setVisible(true);
}
//this if statement checks for when the score is less than or equal to 0 exit the game
if (Point <= 0)
{
System.exit(0);
}
//within this for loop is what sets the cards/buttons out with the correct set values also
for (int i = 0; i < gameBtn.length; i++)
{
if (gameBtn[i] == e.getSource())
{
/* gameBtn[i].setText("" + gameList.get(i));*/
gameBtn[i].setEnabled(false);
counter++;
if (counter == 3)
{
//this if statement disables and hides the cards/buttons if they match
if (sameValues())
{
gameBtn[btnID[0]].setEnabled(false);
gameBtn[btnID[1]].setEnabled(false);
gameBtn[btnID[0]].setVisible(false);
gameBtn[btnID[1]].setVisible(false);
cards = cards -2;
}
else
{
//this else statement is for when a user fails to match the cards it will re-enable the cards and take off a point
gameBtn[btnID[0]].setEnabled(true);
gameBtn[btnID[0]].setText("");
gameBtn[btnID[1]].setEnabled(true);
gameBtn[btnID[1]].setText("");
Point = Point -1;
}
counter = 1;
}
//these if statements below set the gameBtns to a value when clicked and gets there value
if (counter == 1)
{
btnID[0] = i;
btnValue[0] = gameList.get(i);
}
if (counter == 2)
{
btnID[1] = i;
btnValue[1] = gameList.get(i);
}
//the if staement below brings up the nameprompt popup if the player wins the game
if (cards == 2){
NamePrompt promptForName = new NamePrompt();
promptForName.setVisible(true);
}
}
}
updateScore();
}
//below sets the values of the gameList to each button for example button 1 will have a value of one to match to
public void setArrayListText()
{
for (int i = 0; i < 2; i++)
{
for (int ii = 1; ii < (gameBtn.length / 2) + 1; ii++)
{
gameList.add(ii);
}
}
}
private static final Logger LOG = Logger.getLogger(Concentrate.class.getName());
public static void main(String[] args)
{
Concentrate concentrate = new Concentrate();
}
}
上面是我的主类,它还构建框架并设置面板上的按钮我将卡片图标存储在资源文件夹中,并定义了每个卡片图标在公共集中()类中的哪个按钮
gameBtn[0].setDisabledIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/images/c1.gif")));
gameBtn[1].setDisabledIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/images/c2.gif")));
gameBtn[2].setDisabledIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/images/c3.gif")));
gameBtn[3].setDisabledIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/images/c4.gif")));
gameBtn[4].setDisabledIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/images/c5.gif")));
gameBtn[5].setDisabledIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/images/c6.gif")));
gameBtn[6].setDisabledIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/images/c7.gif")));
gameBtn[7].setDisabledIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/images/c8.gif")));
gameBtn[8].setDisabledIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/images/d1.gif")));
gameBtn[9].setDisabledIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/images/d2.gif")));
gameBtn[10].setDisabledIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/images/d3.gif")));
gameBtn[11].setDisabledIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/images/d4.gif")));
gameBtn[12].setDisabledIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/images/d5.gif")));
gameBtn[13].setDisabledIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/images/d6.gif")));
gameBtn[14].setDisabledIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/images/d7.gif")));
gameBtn[15].setDisabledIcon(new javax.swing.ImageIcon(getClass().getResource("/resources/images/d8.gif")));
如果我描述性不够,会显示游戏图片但没有足够的声誉道歉,我对此很陌生,但希望有人能足够友善并花时间帮助我谢谢。
【问题讨论】:
-
我建议为您的游戏按钮扩展 JButton 类,并将按钮项目(在按钮上绘制的对象)作为类属性,然后当按钮项目更改时,您可以找到它的关联图像并将其设置为按钮图像。
-
是的,我确实尝试过那条路线,并让它在按钮上绘制图标,但无法用这些值设置它,尽管我会再次检查它,看看我能做什么谢谢,我相信有一种更有效的方法可以设置按钮值。
-
我同意@KristjanVeskimäe,扩展 JButton 或以其他方式让每个图块由一个类表示是可行的方法,这样您就可以存储按钮的状态,它应该使用的图像,等等等等。就洗牌而言,看看这个:en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle
标签: java swing memory random jbutton