I need to adjust the size of the image to the space provided by jframe
.
Error:
Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: Width (0) and height (0) must be non-zero at java.awt.image.ReplicateScaleFilter.<init>(ReplicateScaleFilter.java:102) at java.awt.image.AreaAveragingScaleFilter.<init>(AreaAveragingScaleFilter.java:77) at java.awt.Image.getScaledInstance(Image.java:172)
Code that I am using to resize the image (in the constructor, both this.getSize().width
and this.getSize().height
is 0):
try {
bgImg = ImageIO.read(getClass().getResource("Images/dummy-image.jpg"));
double scaleFactor = Math.min(1d, getScaleFactorToFit(new Dimension(bgImg.getWidth(), bgImg.getHeight()), new Dimension(this.getSize().width,this.getSize().height)));
int scaleWidth = (int) Math.round(bgImg.getWidth() * scaleFactor);
int scaleHeight = (int) Math.round(bgImg.getHeight() * scaleFactor);
Image scaled = bgImg.getScaledInstance(scaleWidth, scaleHeight, Image.SCALE_SMOOTH);
labelAux.setSize(this.getSize().width, this.getSize().height);
labelAux.setIcon(new ImageIcon(scaled));
slideContainer.setPref_H(this.getSize().height);
slideContainer.setPref_W(this.getSize().width);
slideContainer.insertComponentEffect(labelAux, "");
} catch (IOException ex) {
Logger.getLogger(ThePanelForImagesFitMaxImg.class.getName()).log(Level.SEVERE, null, ex);
}
As I'm using this library to create effects I can not define static measures ...
Exampleapp: