Paolo Di Biase
Paolo Di Biase

Reputation: 53

I can't set the size of an image on Java

as i write on the title, i can't set the size of an image on java and i don't know why. So, that's the code:

package it.unimol.monopoli.gui;

import java.util.ArrayList;
import java.util.List;

import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;

import it.unimol.monopoli.app.Player;

public class GameBoard extends JPanel{
    private List<Player> players;
    private JLabel image;

    public GameBoard(List<Player> players) {
        this.boxes = new ArrayList<>();
        
        this.players = players;
        this.setBounds(0,0,500,500);
        image = new JLabel(new ImageIcon("G:\\Il mio Drive\\Università\\Programmazione II\\21-22\\progetto\\Tabellone.jpg"));
        this.add(image);
        inizializeSquares();
    }
}

the class is not complete, but the thing that doesn't work is the 'setBuonds()' because the immage is on all the frame that has a dimension of 1000x1000. For any information, all the panel are with the defoult layout.

Ask for any questions, i'll wait for yous answers.

Upvotes: -1

Views: 64

Answers (1)

hfontanez
hfontanez

Reputation: 6168

Resizing the panel or the label you are placing the image in doesn't work. Think of it this way, an ImageIcon is not a typical Swing component; like a panel or label. Therefore, it does not "obey" resizing rules like proper Java Swing components do. I believe the main reason is because image icons are not containers and those rules (which tie to layouts and layout managers) are applicable to containers more than to components. At any rate, that's not important. For ImageIcon objects to resize, what you need to do is scale the image and then placed the scaled image on the container (in this case JLabel).

I adjusted the code so I could run it on my environment and tested it with an image of my own. To post it here, I switched it back to your image.

public class GameBoard extends JPanel{
  private List<Player> players;
  private List<?> boxes; // not sure what this is
  private JLabel image;

  public GameBoard(List<Player> players) {
      this.boxes = new ArrayList<>();
        
      this.players = players;      
      this.setBounds(0,0,500,500);
      ImageIcon icon = new ImageIcon("G:\\Il mio Drive\\Università\\Programmazione II\\21-22\\progetto\\Tabellone.jpg");

      // this is the magic sauce (image scaled to 100x100) - adjust to your needs
      icon.setImage(icon.getImage().getScaledInstance(100, 100,Image.SCALE_DEFAULT));

      image = new JLabel(icon);
      this.add(image);
  }
  
  public static void main(String[] args) {
    GameBoard board = new GameBoard(null);
    JFrame frame = new JFrame();
    frame.setBounds(100, 100, 1000, 1000);
    frame.getContentPane().add(board);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    frame.setVisible(true);
  }
}

Basically, you are getting the current image and using that instance, you will set back in it a scaled version of itself. Then, you place the scaled image in a Swing container, like a JLabel like you did here.

If you think about it, this is quite genius. You do not want your images affected by the effects of layout managers that would distort the image when resizing components based on layout rules. By not allowing Swing containers to resize images, you will be free to use whichever layout manager you would want (or even absolute positioning) without ever worrying about how the images (or icons) in your application might be affected. These last comments are my opinions and not some industry consensus, but it makes sense; doesn't it?

UPDATE:

Image.SCALE_DEFAULT is the chosen algorithm to do the resizing. I don't know much about these algorithms, but this is the one most examples on the web use to do the scaling. If you are that curious, you should look into this and select (perhaps) an algorithm that is better suited for your particular case. I expect, even the default scaling algorithm, to have drawbacks. Otherwise, why have other algorithms to do the same thing? Anyway, I thought it was prudent to add this final comment on this post.

Upvotes: 1

Related Questions