我想創建一個使用二維數組來顯示遊戲佈局的遊戲。Java二維數組支持網格來創建遊戲板
我竭力要改變一個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);
}
}
}
你的問題是什麼? – 2015-04-02 12:24:40