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);
}
}
}
You can't assign a JButton into an int array. It would need to be an Object array to be able to store both Integers and JButtons
I would suggest taking a more object oriented approach. You are not able to convert a Button to an 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.
Consider making a custom class that has properties for both JButton and int, like so...
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...
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. I miss this a lot doing JavaScript these days. Lol. Hope this helps... :)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.