NekoLLX
NekoLLX

Reputation: 219

Resize ImageIcon on window Resize

this has been bugging me for a while but i just can't seem to figure out what i'm doing wrong. So i'm setting the background of a pannel with image Icon but when i resize the window it leave the BG at the same size and i get this huge white wall around the exposed edge, i'd like to stretch the bg as the window changes

here's my relevant code

    protected JPanel createRightPane() {
        final ImageIcon BGiconSM = ScaledImageIcon("parchmentTall.jpg", "BG Plate", initalWidth/2, initalHeight);
        final ImageIcon iconSM = ScaledImageIcon("titlebar.png", "Title Bar BG", (initalWidth/3), 40);
        //TODO Parchment image resize
        final JPanel content = new JPanel(new GridBagLayout());
        content.setOpaque(false);

        final JPanel panel = new JPanel(new BorderLayout())                           {
            protected void paintComponent(Graphics g)
            {
                //  Dispaly image at full size
                Image BGicon = BGiconSM.getImage();                 
                g.drawImage(BGicon, 0, 0, null);
                super.paintComponent(g);
            }
        };
        panel.addComponentListener(new ComponentAdapter() {
            @Override
            public void componentResized(ComponentEvent e){
                Rectangle r = frame.getBounds();
                int h = r.height;
                int w = r.width;
                if (h >= initalHeight){h = initalHeight-30;}                    
                //System.out.println(h);
                //System.out.println(w);
/*                  protected paintComponent(Graphics g)
                {
                    //  Dispaly image at full size
                    Image BGicon = BGiconSM.getImage();                 
                    g.drawImage(BGicon, 0, 0, null);
                    super.paintComponent(g);
                }
*/
            }
        });

Upvotes: 1

Views: 1069

Answers (2)

Martin Frank
Martin Frank

Reputation: 3454

you can add a listener to your panel...

How can I force an ImageIcon to be a certain size?

there is a solution on an button but you can very easy excahnge those components...

(copy/paste)

so how to measure a buttons size?

final JPanel panel = new JPanel(); //create it on your custom way...
panel.addComponentListener(new ComponentAdapter() {

    @Override
    public void componentResized(ComponentEvent e){
        int w = b.getWidth();
        int h = b.getHeight();            
        setIconSize(w,h); //TODO by yourself (sorry, if more help required please say so)
    }
});

you can simply scale a image with this code:

/**
 * Copyright (c) 1995, 2008, Oracle and/or its affiliates. All rights reserved.
 * Resizes an image using a Graphics2D object backed by a BufferedImage.
 * @param srcImg - source image to scale
 * @param w - desired width
 * @param h - desired height
 * @return - the new resized image
 */
private Image getScaledImage(Image srcImg, int w, int h){
    BufferedImage resizedImg = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
    Graphics2D g2 = resizedImg.createGraphics();
    g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
    g2.drawImage(srcImg, 0, 0, w, h, null);
    g2.dispose();        
    return resizedImg;
}

Upvotes: 2

MadProgrammer
MadProgrammer

Reputation: 347184

The question is, why should it (resize)? You've only told the Graphics context that it should draw the image at the top left corner of the component (0x0).

You should have a look at Graphics#drawImage(Image, int, int, int, int, ImageObserver) and Graphics2D#drawImage(Image, AffineTransform, ImageObserver)

You should also have a look at:

For discussions over different approaches to scaling images

You're also breaking the paint chain, which is causing the "wall of white". You should be calling super.paintComponent before performing any custom painting.

When overriding a container like JPanel or JComponent for the purpose of custom painting, you should also consider overriding the getPreferredSize method, this will provide the means for layout managers to size the component to an appropriate default size instead of using 0x0 which is normally the default size.

Upvotes: 3

Related Questions