Java集中有一种方法来洗牌jbuttons

时间:2014-05-12 11:41:06

标签: java swing memory random jbutton

我一直在使用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"))); 

会显示游戏形象,但如果我没有足够的描述性,就没有足够的声誉道歉,我对此很新,但希望有人能够善待并花时间帮助我感谢。

1 个答案:

答案 0 :(得分:1)

  

“那么对每个按钮设置的图像和值进行随机化或随机播放的最佳方法是什么”

只需使用Collections.shuffle()来移动按钮数组。它需要一个List参数,但您可以使用Arrays.toList()。在对阵列进行混洗之后,使用按钮重新布局面板。

参见示例

import java.awt.BorderLayout;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.Arrays;
import java.util.Collections;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.SwingUtilities;

public class ShuffleButtons {

    private JButton[] buttons;
    private JPanel gridPanel;

    public ShuffleButtons() {
        gridPanel = new JPanel(new GridLayout(4, 4));
        buttons = getButtons(16);
        layoutButtons();

        JFrame frame = new JFrame("Shuffle Buttons");
        frame.add(gridPanel);
        frame.add(getResetButton(), BorderLayout.SOUTH);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.pack();
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
    }

    public void shuffleButtons() {
        if (buttons != null) {
            Collections.shuffle(Arrays.asList(buttons));
            layoutButtons();
        }
    }

    public void layoutButtons() {
        gridPanel.removeAll();
        for (JButton button : buttons) {
            gridPanel.add(button);
        }
        gridPanel.revalidate();
        gridPanel.repaint();
    }

    private JButton[] getButtons(int size) {
        JButton[] buttons = new JButton[size];
        for (int i = 0; i < size; i++) {
            final JButton button = new JButton(" [ " + i + " ] ");
            button.addActionListener(new ActionListener(){
                public void actionPerformed(ActionEvent e) {
                    System.out.println("Button " + button.getText() + " pressed");
                }
            });
            buttons[i] = button;
        }
        return buttons;
    }

    private JButton getResetButton() {
        JButton button = new JButton("RESET");
        button.addActionListener(new ActionListener(){
            public void actionPerformed(ActionEvent e) {
                shuffleButtons();
            }
        });
        return button;
    }

    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable(){
            public void run() {
                new ShuffleButtons();
            }
        });
    }
}