How to know the size of Jframe in the constructor?

Asked

Viewed 369 times

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

Library

Example of the use of the library: inserir a descrição da imagem aqui

Example of the app: inserir a descrição da imagem aqui

inserir a descrição da imagem aqui

  • There’s no way, the window hasn’t even been built yet, how to know its size?

  • Perhaps by inserting a [mcve] it is possible to analyze the problem better. Unfortunately not everyone can see the images for reasons of blocking.

  • @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?

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

  • Or try this solution (which I find very bad, although valid): https://stackoverflow.com/a/1065136/5524514

1 answer

1

It will only be possible to discover the size of a window after it is set, either through the methods setSize(Dimension d) or setSize(int width, int height) which define a fixed size, either by means of relative size methods, which is the case of setPreferredSize(Dimension preferredSize), setMaximumSize(Dimension maximumSize) or setMinimumSize(Dimension minimumSize).

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

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

Browser other questions tagged

You are not signed in. Login or sign up in order to post.