0

I am attempting to make a pseudo candy crush style game. I am currently attempting to add images contained in an ArrayList to JButtons that are in an ArrayList as well. I am unsure how to go about this and what I currently have, clearly, doesn't work, as it does not display the images within the JButtons. Here is my UI code:

package code.ui;

import java.awt.GridLayout;
import java.awt.Image;    
import java.util.ArrayList;
import java.util.Collections;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;


import code.model.Model;

public class gameUI implements Runnable {

private JFrame _frame;
private Model _model;

private ArrayList<JButton> _buttons;
private JButton _spin;

@Override public void run() {
    _frame = new JFrame("Switcher");
    _frame.getContentPane().setLayout(new GridLayout(5,5));

    _model = new Model();  // create the model for this UI
    _model.addObserver(this);  



    _buttons = new ArrayList<JButton>();
    for (int i=0; i<25; i++) {
        JButton button = new JButton();
        _buttons.add(button);
        _frame.getContentPane().add(button);
        _buttons.get(i).setIcon(new ImageIcon("Images/"+_model.getImageFileName(i)));
        Collections.shuffle(_buttons);
    }


    _frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    _frame.pack();
    _frame.setVisible(true);
}



}

This is my code for the model:

package code.model;

import java.util.ArrayList;
import java.util.Random;

import javax.swing.ImageIcon;


public class Model {

    private Random _rand;  
    private ArrayList<String> _imageFileNames;  
    private ArrayList<String> _imageCurrentValues;  

    public Model() {
        _rand = new Random();

        _imageFileNames = new ArrayList<String>();
        _imageFileNames.add("Red.png");
        _imageFileNames.add("Green.png");
        _imageFileNames.add("Purple.png");
        _imageFileNames.add("Tile-0.png");
        _imageFileNames.add("Tile-1.png");
        _imageFileNames.add("Tile-2.png");
        _imageFileNames.add("Tile-3.png");
        _imageFileNames.add("Tile-4.png");
        _imageFileNames.add("Tile-5.png");

        _imageCurrentValues = new ArrayList<String>();
        for(int i=0; i<25; i=i+1) {
            _imageCurrentValues.add(i,null);
        }

    }


    public String getImageFileName(int i) {
        return _imageCurrentValues.get(i);
    }

}
sqwert
  • 13
  • 4
  • "clearly doesn't work." In what way? – JitterbugChew Nov 22 '16 at 21:36
  • It displays the proper 5 x 5 JButtons, but does not apply the images to the buttons. They are just blank, grey, squares. – sqwert Nov 22 '16 at 21:46
  • I assume you have the `Images` folder in your build path? – JitterbugChew Nov 22 '16 at 21:48
  • Yes, I use it in the UI here: _buttons.get(i).setIcon(new ImageIcon("Images/"+_model.getImageFileName(i))); I'm not sure if I'm using it correctly, though. – sqwert Nov 22 '16 at 21:53
  • Try `ImageIcon imgIcon = new ImageIcon("Images/"+_model.getImageFileName(i))`, set a breakpoint on that line and see if something is assigned to it or not. Maybe even pull out the function call `getImageFileName(i)` and assign that to a temp variable too. Then you can see the value of it in debug to make sure it is what you think it is. – JitterbugChew Nov 22 '16 at 22:00
  • http://stackoverflow.com/questions/4801386/how-do-i-add-an-image-to-a-jbutton see this too just to be sure you're loading the image resources correctly – JitterbugChew Nov 22 '16 at 22:02
  • I'm unsure where to place that within my code, but upon just putting it within my UI class, it is not assigned to any variable. As for the image loading, I tried some of the code provided from that link to no avail, it gave back an error of "Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: input == null!" – sqwert Nov 23 '16 at 01:39

0 Answers0