0
I need to adjust the image size 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 I’m using to resize the image (in the constructor, both the this.getSize().width
like the 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 am using this library to create effects I cannot define static measures...
There’s no way, the window hasn’t even been built yet, how to know its size?
– user28595
Perhaps by inserting a [mcve] it is possible to analyze the problem better. Unfortunately not everyone can see the images for reasons of blocking.
– user28595
@Is the code too long to put it all together, if the constructor helps? I’ve also thought about it, if the window isn’t built yet it can’t have an assigned size... However there is no way to launch an event when the window is ready?
– jsantos1991
The suggestion is that you elaborate a [mcve] only with the image case, not the whole class, only the necessary to reproduce the problem. Without running it is difficult to suggest something that can solve and not generate you another problem.
– user28595
Or try this solution (which I find very bad, although valid): https://stackoverflow.com/a/1065136/5524514
– user28595