简体   繁体   English

在窗口上调整ImageIcon的大小

[英]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 所以我用图像图标设置了面板的背景,但是当我调整窗口大小时,它使BG保持相同的大小,并且我在裸露的边缘得到了这堵巨大的白墙,我想将bg拉伸为窗口变化

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);
                }
*/
            }
        });

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). 您仅告诉Graphics上下文它应该在组件的左上角(0x0)绘制图像。

You should have a look at Graphics#drawImage(Image, int, int, int, int, ImageObserver) and Graphics2D#drawImage(Image, AffineTransform, ImageObserver) 您应该看看Graphics#drawImage(Image, int, int, int, int, ImageObserver)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. 在执行任何自定义绘制之前,您应该调用super.paintComponent

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. 当出于自定义绘画的目的而覆盖诸如JPanelJComponent类的容器时,您还应该考虑覆盖getPreferredSize方法,这将为布局管理器提供一种将组件调整为适当的默认大小的方法,而不是使用通常为默认值的0x0尺寸。

you can add a listener to your panel... 您可以将侦听器添加到面板中...

How can I force an ImageIcon to be a certain size? 如何强制将ImageIcon设置为特定大小?

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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM