Error in java swing: java.lang.IllegalArgumentException: Width (0) and height (0) must be non-zero when putting image on a button

0

I'm making a game with linked lists, I have a node class which inherits from JButton (because I want each node to be a button), it's a set of stairs and snakes.

At the moment I was indicating that the start and end of a staircase are two squares that have the same color (say white), the code was something like this:

list.getNodeWith(iniScala).setBackground(Color.white);

list.getNodeWith(iniScala) is a button so I mentioned earlier And when I was running the game I was giving it without any problem, but now what I want to do is put an image on a button when it is for example a staircase end, I put it like this:

ImageIcon portal = new ImageIcon("/PracticaUnoLogica/imagenes/portal.jpg");
list.getNodeWith(endScala).setIcon(
    new ImageIcon(
       portal.getImage().getScaledInstance(
          list.getNodeWith(endScala).getWidth(),
          list.getNodeWith(endScala).getHeight(),
          Image.SCALE_SMOOTH)));

I really do not know what the error is, because apparently when it reaches the height and width of the button it returns 0 and therefore does not let me scale the image.

Thank you very much

    
asked by Juan Gutiérrez 29.09.2018 в 16:57
source

0 answers