1

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);
                }
*/
            }
        });
NekoLLX
  • 219
  • 6
  • 19

2 Answers2

3

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.

Community
  • 1
  • 1
MadProgrammer
  • 343,457
  • 22
  • 230
  • 366
  • wow - i'm always impressed how fast and precise you're able to answer! i've added my solution only to show how to add a componentListener – Martin Frank Sep 22 '14 at 06:30
  • 1
    @MartinFrank I think this guy has a hook up at Stack Overflow that feeds him questions 30 minutes before everyone else gets them. Only explanation for this bizarre behavior :-D – Paul Samsotha Sep 22 '14 at 06:35
  • hahaha - i'm glad i'm not the only one who realised that ^^... but really - i'm very glad that here are such ambitious persons! – Martin Frank Sep 22 '14 at 06:41
  • @MadProgrammer How can you be bored when you're doing _what you love_! – Paul Samsotha Sep 22 '14 at 06:46
  • 3
    @peeskillet That's because I work for a company who has no qualms about ignoring 30+ years of user interface design principles and psychology, user experience guidelines and just about all major software development paradigms...kind of grinds on you after awhile... – MadProgrammer Sep 22 '14 at 06:47
2

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;
}
Community
  • 1
  • 1
Martin Frank
  • 3,445
  • 1
  • 27
  • 47
  • 2
    The only problem I would have with this solution is `componentReiszed` might be called multiple times in quick succession, resizing can't be quite labour intensify. I might consider having a "low quality" image which can be resized quickly and painted and have a Swing `Timer` setup (set to something like 125-250 milliseconds) that is restarted each time `componentResized`, this means that after (about) 125ms after the last call to `componentResized` I can perform the "quality" resize in one step and repaint the component - but that's just me ;) – MadProgrammer Sep 22 '14 at 06:45
  • Thanks Martin that got me on the right track, ultimatly i tinkered with it and got it 90% working save for one thing, i could find the size and set it to a image but when i tried to call the paint component inside the resize component it broke Eclise gives me a error on the cury braces meaning it doesnt reconize the end of the sub function meaning something has to be wrong in my function calls will update the OP with the code – NekoLLX Sep 25 '14 at 04:12
  • well i'm glad i could provide help, but maybe you *might* consider *sometimes* to have a look at those hints from @MadProgrammer - thats really worth considering ^^ but for me i'm happy i could help you out a litte and wish you good luck on your project!!! – Martin Frank Sep 25 '14 at 05:01
  • i did look at the hints, its not the code that's the problem as its a copy paste of my inital image draw, i'm pretty sure it's a syntax bug, a misplaced simicolor or parem but not sure which since the actual excecutable code i know works i just can't see the syntax error – NekoLLX Sep 25 '14 at 15:37