简体   繁体   English

Java 2D阵列支持的网格创建游戏板

[英]Java 2D array backed grid to create gameboard

I am trying to create a game using a 2D array to display the layout of the game. 我正在尝试使用2D数组创建游戏以显示游戏的布局。

I'm struggling to change an int to a JButton with a different image assigned to it depending on what number is display in the grid: 我正在努力将一个int更改为一个JButton ,并为其分配不同的图像,具体取决于网格中显示的数字:

private void PlayPanel() {
    try 
    {
        iconBlank=new ImageIcon(Toolkit.getDefaultToolkit().createImage(GUI.class.getResource("white32x32.jpg")));
    }
    catch (Exception e)
            {
                System.err.println("Blank Icon ImageIcon" +e);
            }
    try 
    {
        iconSand=new ImageIcon(Toolkit.getDefaultToolkit().createImage(GUI.class.getResource("sand.jpg")));
    }
    catch (Exception e)
            {
                System.err.println("Sand Icon ImageIcon" +e);
            } 
       try 
    {
        iconBall=new ImageIcon(Toolkit.getDefaultToolkit().createImage(GUI.class.getResource("sand60x60.png")));
    }
    catch (Exception e)
            {
                System.err.println("Ball Icon ImageIcon" +e);
            }
        try 
    {
        iconEnd=new ImageIcon(Toolkit.getDefaultToolkit().createImage(GUI.class.getResource("sandstone.jpg")));
    }
    catch (Exception e)
            {
                System.err.println("End Icon ImageIcon" +e);
            }



 pPlayScreen =new JPanel();
    pPlayScreen.setPreferredSize(new Dimension(550,520));
    pPlayScreen.setBorder( BorderFactory.createRaisedBevelBorder() );
    pPlayScreen.setBackground(Color.white);
    pPlayScreen.setLayout (new GridLayout (13,16));

int[][] playButtons = {
{ 1, 1, 1, 1 ,1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2 },
{ 1, 1, 1, 1 ,1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2 },
{ 1, 1, 1, 1 ,1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2 },
{ 1, 1, 1, 1 ,1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2 },
{ 1, 1, 1, 1 ,1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2 },
{ 1, 1, 1, 1 ,1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2 },
{ 1, 1, 1, 1 ,1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2 },
{ 1, 1, 1, 1 ,1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2 },
{ 1, 1, 1, 1 ,1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2 },
{ 1, 1, 1, 1 ,1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2 },
{ 1, 1, 1, 1 ,1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2 },
{ 1, 1, 1, 1 ,1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2 },
{ 3, 1, 1, 1 ,1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 }
};


   for (int rows = 0 ; rows < 16 ; rows++) 
{ 
for (int cols = 0 ; cols < 13 ; cols++) 
{ 
if(playButtons [rows][cols]==0){
// ???
}
playButton [rows] [cols] = new JButton (); 
playButton [rows] [cols].addActionListener (new Play()); 

pPlayScreen.add (playButton [rows] [cols]);
pMain.add(pPlayScreen);

}
}       
} 

You can't assign a JButton into an int array. 您不能将JButton分配给int数组。 It would need to be an Object array to be able to store both Integers and JButtons 它必须是一个Object数组,才能存储Integers和JButtons

I would suggest taking a more object oriented approach. 我建议采取一种更加面向对象的方法。 You are not able to convert a Button to an int. 您无法将Button转换为int。

Try for example to use MVC (model, view, controller), then you may just use some object containing the 2D int array as a model and a view interpreting this array by displaying the kinds of buttons that you like based on the model. 例如,尝试使用MVC(模型,视图,控制器),则可以只使用包含2D int数组的对象作为模型,并通过显示基于模型的按钮类型来解释该数组的视图。

Consider making a custom class that has properties for both JButton and int, like so... 考虑制作一个同时具有JButton和int属性的自定义类,如下所示...

public class MyButton {

    private int n;
    private JButton button; 

    // Constructor
    public MyButton() {

    }

    // Accessors and mutators
    private void setN(int n)
        this.n =n;
    }

    private int getN() {
        return this.n;
    }

    private void setButton(JButton button) {
        this.button = button;
    }

    private JButton getButton() {
        return this.button;
    }
}

And then you can access it by creating a new MyButton object, and then have an array list of them like so... 然后您可以通过创建一个新的MyButton对象来访问它,然后像这样拥有它们的数组列表...

ArrayList<MyButton> buttons = new ArrayList<MyButton>();

And then you can do what you need. 然后,您可以执行所需的操作。 This is the beauty of OOP and Java's class capabilities. 这就是OOP和Java的类功能的美。 I miss this a lot doing JavaScript these days. 这些天,我非常想念JavaScript。 Lol. 大声笑。 Hope this helps... :) 希望这可以帮助... :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM