调整图像大小以完全适合jFrame

时间:2015-01-28 01:19:28

标签: java swing jframe

我想调整logo2.png的大小,使其完全适合jFrame。任何人都可以帮助我吗?

jLabel2 = new javax.swing.JLabel();

jLabel2.setIcon(new javax.swing.ImageIcon(getClass().getResource("/Banking/logo2.png"))); // NOI18N

JDesktopPane.add(jLabel2);
jLabel2.setBounds(0, 0, 500, 290);

3 个答案:

答案 0 :(得分:2)

JLabel没有重新缩放它的内容(即图像),为此,你将不得不让你的手有点脏......

有很多方法可以实现这一目标。就个人而言,我从一个自定义组件开始,该组件从JPanel扩展而来,它采用基本图像并且能够缩放和绘制它。

有关执行自定义绘画的详细信息,请查看Performing Custom PaintingPainting in AWT and Swing

缩放图像并不像看起来那么简单,而Java确实提供了一些API来缩放图像,通常,它们不会产生出色的结果。有关详细信息,请查看Quality of Image after resize very low -- JavaThe Perils of Image.getScaledInstance()

我通常建议您查看imgscalr库,因为它的结果通常非常好(比基本Java API提供的更好,或者我通过大多数其他方法看到的更好),但对于此示例,我已经列举了一个分歧的例子。

此示例提供了“缩放以填充”实现,这可确保生成的图像始终填充可用空间,同时保持其纵横比,有关此主题的更多讨论,请参阅Java: maintaining aspect ratio of JPanel background image

import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.HeadlessException;
import java.awt.RenderingHints;
import java.awt.Transparency;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class ScaledBackground {

    public static void main(String[] args) {
        new ScaledBackground();
    }

    public ScaledBackground() {
        EventQueue.invokeLater(new Runnable() {
            @Override
            public void run() {
                try {
                    UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
                } catch (ClassNotFoundException | InstantiationException | IllegalAccessException | UnsupportedLookAndFeelException ex) {
                    ex.printStackTrace();
                }

                try {
                    BufferedImage img = ImageIO.read(...);

                    JFrame frame = new JFrame("Testing");
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.add(new ImagePane(img));
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                } catch (IOException exp) {
                    exp.printStackTrace();
                }
            }
        });
    }

    public class ImagePane extends JPanel {

        private BufferedImage original;
        private BufferedImage scaled;

        public ImagePane(BufferedImage img) {
            original = img;
            scaled = original;
        }

        @Override
        public Dimension getPreferredSize() {
            return original == null ? new Dimension(200, 200) : new Dimension(original.getWidth(), original.getHeight());
        }

        @Override
        public void invalidate() {
            super.invalidate();
            generateScaledInstance();
        }

        protected void generateScaledInstance() {
            if (original != null) {

                scaled = getScaledInstanceToFill(original, getSize());

            }
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            Graphics2D g2d = (Graphics2D) g.create();
            g2d.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
            g2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
            g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
            if (scaled != null) {
                int x = (getWidth() - scaled.getWidth()) / 2;
                int y = (getHeight() - scaled.getHeight()) / 2;
                g2d.drawImage(scaled, x, y, this);
            }
            g2d.dispose();
        }

        public BufferedImage getScaledInstanceToFill(BufferedImage img, Dimension size) {
            float scaleFactor = getScaleFactorToFill(img, size);
            return getScaledInstance(img, scaleFactor);
        }

        public float getScaleFactorToFill(BufferedImage img, Dimension size) {
            float scale = 1f;
            if (img != null) {
                int imageWidth = img.getWidth();
                int imageHeight = img.getHeight();
                scale = getScaleFactorToFill(new Dimension(imageWidth, imageHeight), size);
            }
            return scale;
        }

        public float getScaleFactorToFill(Dimension original, Dimension toFit) {
            float scale = 1f;
            if (original != null && toFit != null) {
                float dScaleWidth = getScaleFactor(original.width, toFit.width);
                float dScaleHeight = getScaleFactor(original.height, toFit.height);
                scale = Math.max(dScaleHeight, dScaleWidth);
            }
            return scale;
        }

        public float getScaleFactor(int iMasterSize, int iTargetSize) {
            float scale = 1;
            if (iMasterSize > iTargetSize) {
                scale = (float) iTargetSize / (float) iMasterSize;
            } else {
                scale = (float) iTargetSize / (float) iMasterSize;
            }
            return scale;
        }

        public BufferedImage getScaledInstance(BufferedImage img, double dScaleFactor) {
            BufferedImage imgBuffer = null;
            imgBuffer = getScaledInstance(img, dScaleFactor, RenderingHints.VALUE_INTERPOLATION_BILINEAR, true);
            return imgBuffer;
        }

        protected BufferedImage getScaledInstance(BufferedImage img, double dScaleFactor, Object hint, boolean higherQuality) {

            BufferedImage scaled = img;
            if (dScaleFactor != 1.0) {
                if (dScaleFactor > 1.0) {
                    scaled = getScaledUpInstance(img, dScaleFactor, hint, higherQuality);
                } else if (dScaleFactor > 0.0) {
                    scaled = getScaledDownInstance(img, dScaleFactor, hint, higherQuality);
                }
            }

            return scaled;

        }

        protected BufferedImage getScaledDownInstance(BufferedImage img, double dScaleFactor, Object hint, boolean higherQuality) {

            int targetWidth = (int) Math.round(img.getWidth() * dScaleFactor);
            int targetHeight = (int) Math.round(img.getHeight() * dScaleFactor);

            int type = (img.getTransparency() == Transparency.OPAQUE)
                            ? BufferedImage.TYPE_INT_RGB : BufferedImage.TYPE_INT_ARGB;

            BufferedImage ret = (BufferedImage) img;

            if (targetHeight > 0 || targetWidth > 0) {
                int w, h;
                if (higherQuality) {
                    w = img.getWidth();
                    h = img.getHeight();
                } else {
                    w = targetWidth;
                    h = targetHeight;
                }

                do {
                    if (higherQuality && w > targetWidth) {
                        w /= 2;
                        if (w < targetWidth) {
                            w = targetWidth;
                        }
                    }

                    if (higherQuality && h > targetHeight) {
                        h /= 2;
                        if (h < targetHeight) {
                            h = targetHeight;
                        }
                    }

                    BufferedImage tmp = new BufferedImage(Math.max(w, 1), Math.max(h, 1), type);
                    Graphics2D g2 = tmp.createGraphics();
                    g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, hint);
                    g2.drawImage(ret, 0, 0, w, h, null);
                    g2.dispose();

                    ret = tmp;
                } while (w != targetWidth || h != targetHeight);
            } else {
                ret = new BufferedImage(1, 1, type);
            }
            return ret;
        }

        protected BufferedImage getScaledUpInstance(BufferedImage img,
                        double dScaleFactor,
                        Object hint,
                        boolean higherQuality) {

            int targetWidth = (int) Math.round(img.getWidth() * dScaleFactor);
            int targetHeight = (int) Math.round(img.getHeight() * dScaleFactor);

            int type = BufferedImage.TYPE_INT_ARGB;

            BufferedImage ret = (BufferedImage) img;
            int w, h;
            if (higherQuality) {

                w = img.getWidth();
                h = img.getHeight();

            } else {

                w = targetWidth;
                h = targetHeight;

            }

            do {

                if (higherQuality && w < targetWidth) {

                    w *= 2;
                    if (w > targetWidth) {

                        w = targetWidth;

                    }

                }

                if (higherQuality && h < targetHeight) {

                    h *= 2;
                    if (h > targetHeight) {

                        h = targetHeight;

                    }

                }

                BufferedImage tmp = new BufferedImage(w, h, type);
                Graphics2D g2 = tmp.createGraphics();
                g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, hint);
                g2.drawImage(ret, 0, 0, w, h, null);
                g2.dispose();

                ret = tmp;
                tmp = null;

            } while (w != targetWidth || h != targetHeight);

            return ret;

        }

    }

}

此示例将随着图像的变化而增长和缩小,作为面板可用空间量的变化

答案 1 :(得分:2)

您可以在JLabel上使用Stretch Icon

或者您可以使用Background Panel绘制图像。

在这两种情况下,随着帧的大小调整,图像将缩小或增长。

答案 2 :(得分:0)

首先获取Image,然后使用此方法:

yourImage.getScaledInstance(newWidth, newHeight, Image.SCALE_DEFAULT);

您可以从JFrame获取宽度和高度。然后,您可以将Image转换为ImageIcon并添加它。