简体   繁体   English

我无法在 Java 上设置图像的大小

[英]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.正如我在标题上写的,我无法在 java 上设置图像的大小,我不知道为什么。 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. class 不完整,但不起作用的是“setBuonds()”,因为图像位于所有尺寸为 1000x1000 的框架上。 For any information, all the panel are with the defoult layout.对于任何信息,所有面板都具有默认布局。

Ask for any questions, i'll wait for yous answers.问任何问题,我会等你的答案。

Resizing the panel or the label you are placing the image in doesn't work.调整面板或放置图像的 label 的大小不起作用。 Think of it this way, an ImageIcon is not a typical Swing component;这样想, ImageIcon不是典型的 Swing 组件; like a panel or label.像面板或 label。 Therefore, it does not "obey" resizing rules like proper Java Swing components do.因此,它不会像正确的 Java Swing 组件那样“遵守”调整大小的规则。 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 ).要调整ImageIcon对象的大小,您需要做的是缩放图像,然后将缩放后的图像放在容器上(在本例中为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.然后,您将缩放后的图像放入 Swing 容器中,就像您在此处所做的JLabel一样。

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.通过不允许 Swing 容器调整图像大小,您将可以自由使用您想要的任何布局管理器(甚至是绝对定位),而不必担心应用程序中的图像(或图标)可能会受到何种影响。 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. Image.SCALE_DEFAULT是用于调整大小的选定算法。 I don't know much about these algorithms, but this is the one most examples on the web use to do the scaling.我对这些算法了解不多,但这是 web 上用于进行缩放的最多示例之一。 If you are that curious, you should look into this and select (perhaps) an algorithm that is better suited for your particular case.如果你很好奇,你应该研究一下这个和 select(也许)一个更适合你的特定情况的算法。 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.无论如何,我认为在这篇文章上添加这个最终评论是谨慎的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM