I am trying to create a game using a 2D array to display the layout of the game. 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
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);
}
}
}
Aucun commentaire:
Enregistrer un commentaire