Width and height of a JPanel are 0 (Specific Situation)

前端 未结 2 1071
庸人自扰
庸人自扰 2020-12-21 20:23

Please pardon me if this is hard to follow, but I have a specific problem that I need help solving. I have done a ton of research, and I have tried numerous solutions but no

相关标签:
2条回答
  • 2020-12-21 20:50

    I would like the image to change size with the size of the JFrame

    Instead of doing custom painting in your own component you can use Darryl's Stretch Icon. The Icon will automatically be resized based on the space available.

    which is why I dont just use 'setPreferedSize();'.

    If you do use custom painting then you should NOT use setPreferredSize(). You SHOULD be overriding the getPreferredSize() method to return the size of the image. Remember the preferred size is just a suggestion to the layout manager. The layout manager can use or ignore the size.

    If you want to scale the image automatically in your paintComponent() method then the code should be:

    Dimension d = getSize();
    g.drawImage(paint, 0, 0, d.width, d.height, this);
    

    So the real challenge in your code (no matter which solution you choose) is to make sure you are using a layout manger that will give all available space to your component so the image can be scaled automatically.

    0 讨论(0)
  • 2020-12-21 20:54

    There are at least two ways this might be achieved, the first would be to allow the paintComponent to check the state of the paint and rescale the image appropriatly when it is null

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
    
        if (i != null && paint == null){
            generateScaledInstance();
        }
        if (paint != null) {
            g.drawImage(paint, 0, 0, this);
        }
    }
    

    This will work because paintComponent should never be called unless the component has a size greater than 0 and is attached to a native peer (on the screen).

    This is not a great idea as scaling can take time and you don't want to slow down the paint process if you can avoid it.

    You could use a ComponentListener attached to the ImagePanel and monitor the componentResized event

    addComponentListener(new ComponentAdapter() {
        @Override
        public void componentResized(ComponentEvent e) {
            if (i != null) {
                generateScaledInstance();
            }
        }
    });
    

    This might be called a number of times in quick succession, so be careful.

    In this case, what I tend to do is use a javax.swing.Timer set to small delay to coalse the updates down to as few a calls as possible, for example...

    private Timer resizeTimer;
    //...
    // Probably in you classes constructor
    resizeTimer = new Timer(250, new ActionListener() {
        public void actionPerformed(ActionEvent evt) {
            // Actually perform the resizing of the image...
            generateScaledInstance();
        }
    });
    // Don't want a repeating event...
    resizeTimer.setRepeats(false);
    
    //...
    public void componentResized(ComponentEvent evt) {
        resizeTimer.restart();
    }
    

    This allows componentResized to be called a number of times in quick succession, but if the time between exceeds 250 milliseconds, the generateScaledInstance can be called, as an example...

    You should also provide a preferredSize value of a non 0 size by default (remember, the default preferred size of a panel is 0x0). Depending on the layout manager, this could be ignored, but is generally used as a basis for most layout managers...

    0 讨论(0)
提交回复
热议问题