How to know the size of JFrame in the constructor of the same?

0

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 ...

Library

Library usage example:

Exampleapp:

    
asked by anonymous 28.08.2017 / 13:05

1 answer

1

You can only find out the size of a window after it has been defined, either through setSize(Dimension d) " or setSize(int width, int height) " that define a fixed size, sometimes through the relative size methods, which is the case of setPreferredSize(Dimension preferredSize) , setMaximumSize(Dimension maximumSize) or setMinimumSize(Dimension minimumSize) .

Each of these methods has a specific getter so that you can retrieve the value set to them, but in case of relative size methods like setPreferredSize() , when no value is set, get will only return some value that is consistent with the size of the window after the pack() method is invoked, as this is who will validate the window and force the layout manager to best match the components within it.

For setSize , if you set a size for it before the invocation of pack() , these values will be replaced with a new size value that will be defined by the layouts manager after the screen construction.

    
27.10.2017 / 13:26