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

时间:2015-04-02 12:11:02

标签: java arrays image grid jbutton

我正在尝试使用2D数组创建游戏以显示游戏的布局。

我很难将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);

}
}       
} 

3 个答案:

答案 0 :(得分:0)

您无法将JButton分配到int数组中。它需要是一个Object数组才能存储整数和JButtons

答案 1 :(得分:0)

我建议采用更面向对象的方法。您无法将Button转换为int。

尝试使用MVC(模型,视图,控制器),然后你可以使用一些包含2D int数组的对象作为模型,并通过显示你喜欢的按钮种类来解释这个数组。模型。

答案 2 :(得分:0)

考虑创建一个具有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;
    }
}

然后你可以通过创建一个新的MyButton对象来访问它,然后有一个像这样的数组列表......

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

然后你可以做你需要的。这是OOP和Java的类功能之美。这几天我很想念JavaScript。大声笑。希望这有助于...:)

相关问题