【问题标题】:How do I resize these ImageIcons to my checkers?如何将图像图标调整为我的棋盘格?
【发布时间】:2014-10-31 18:20:30
【问题描述】:

我有这四个类变量:

private static final ImageIcon redKing = new ImageIcon("Images/redKingImage.png");
private static final ImageIcon blackKing = new ImageIcon("Images/blackKingImage.png");
private static final ImageIcon redChecker = new ImageIcon("Images/redCheckerImage.png");
private static final ImageIcon blackChecker = new ImageIcon("Images/blackCheckerImage.png");

我使用此方法将图标设置为我的棋盘格。

private void setImageOfChecker()
{   

    if(this.isKing == true){
        if(isRed){
            this.imageOfChecker = redKing;
        }
        else{
            this.imageOfChecker = blackKing;
        }
    }
    else{
        if(isRed){
            this.imageOfChecker = redChecker;
        }
        else{
            this.imageOfChecker = blackChecker;
        }
    }
}

我现在的问题是图标对于板上的方块来说太大了,我想知道将这些图像中的每一个重新缩放到特定大小(例如 16x16 像素)的最简单方法是什么图标很适合放在板上。

【问题讨论】:

标签: java swing jbutton imageicon


【解决方案1】:

您可以使用我基于此线程制作的简单方法: resizing a ImageIcon in a JButton

public ImageIcon resizeIcon(int width, int height, ImageIcon icon) {
   Image img = icon.getImage();
   Image newimg = img.getScaledInstance(width, height, java.awt.Image.SCALE_SMOOTH);
   icon = new ImageIcon(newimg);  
   return icon;
}

正如 MadProgrammer 所建议的,有一种更好的方法来确保高质量: link

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2022-11-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-10
  • 2017-08-20
  • 2022-01-17
  • 2014-11-10
相关资源
最近更新 更多