1

I have these four class variables:

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");

and I use this method to set the icons to my checkerboard appropriately.

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;
        }
    }
}

My issue now is that the icons are too big for the squares on the board and I'd like to know what would be the easiest way to rescale EACH of these images to a specific size (for example 16x16pixels) so that all the icons fit nicely on the board.

aidandeno
  • 307
  • 1
  • 2
  • 16
  • Take a look at [this example](http://stackoverflow.com/questions/11959758/java-maintaining-aspect-ratio-of-jpanel-background-image/11959928#11959928), [this example](http://stackoverflow.com/questions/18396302/how-to-scale-image-using-getscaledinstance/18396317#18396317), [this example](http://stackoverflow.com/questions/14115950/quality-of-image-after-resize-very-low-java/14116752#14116752) – MadProgrammer Sep 07 '14 at 09:56

1 Answers1

1

You can use a simple method that I made based on this thread: 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;
}

As MadProgrammer suggested, there is a better approach to ensure a good quality: link

Community
  • 1
  • 1
ondrejba
  • 261
  • 6
  • 16
  • 1
    That looks helpful, I'll try it out now. But how can it be a void method if you're returning an icon? – aidandeno Sep 07 '14 at 09:52
  • 2
    You might like to take a look at [The Perils of Image.getScaledInstance()](https://today.java.net/pub/a/today/2007/04/03/perils-of-image-getscaledinstance.html) – MadProgrammer Sep 07 '14 at 09:57
  • 1
    You might also like to take a look at [this example](http://stackoverflow.com/questions/14115950/quality-of-image-after-resize-very-low-java/14116752#14116752) – MadProgrammer Sep 07 '14 at 10:00