难以调整存储在JLabel中的图像的大小 [英] Difficulty in resizing an image which is stored in a JLabel

查看:99
本文介绍了难以调整存储在JLabel中的图像的大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我设法增加了JLabel的图像(其中存储了imageIcon).当我按下增加大小按钮时,图像的原始大小会在面板上增加,这正是我想要的.但是,当我单击减小尺寸按钮(我认为将其除以缩放比例可能会解决该问题)时,标签会减小,但实际的图像外观(我猜想的尺寸)已更改.它不是在减小尺寸,就像我的增大按钮增大尺寸一样.我花了数小时试图找出原因,然后乘以它,就可以增加标签和标签中图像的大小(这意味着不仅标签在增加,实际图像也在增加),但是减少了(我在划分而不是相乘)它不起作用.这是我的增加和减少的收听者.

I managed to increase an image of JLabel(which has an imageIcon stored in). When I press the increase size button, the original size of the image is increased on the panel, which is exactly what I want. However, when I click on my decrease size button(I figured dividing it by the scale might fix it)the label decreases, but the actual image appearance(size I guess)is changed. It's not decreasing the size, the same way my increase button increases the size. I have spent hours trying to figure out why by multiplying it, I am able to increase the size of a the label and the image in it(which implies that not just the label is increasing, the actual image is too)but for decrease(I'm dividing instead of multiplying)it doesn't work. Here is both my increase and decrease listener.

    public class IncreaseSizeListener implements ActionListener {
    static JLabel increasedLabel;
    @Override
    public void actionPerformed(ActionEvent e) {        
        increasedLabel = CardLabelListener.selectedLabel;
        Icon icon = CardLabelListener.selectedLabel.getIcon();
        int scale =2;
        System.out.println("Increased size fired");
        //I can now resize images, based on my needs
        BufferedImage bi = new BufferedImage(
            scale*icon.getIconWidth(),
            scale*icon.getIconHeight(),
            BufferedImage.TYPE_INT_ARGB);
            Graphics2D g = bi.createGraphics();
            g.scale(scale,scale);
            icon.paintIcon(null,g,0,0);
            g.dispose();
            JLabel temp = new JLabel(new ImageIcon(bi));
            //to ensure proper size is kept for the enlarged image
            CardLabelListener.selectedLabel.setSize(icon.getIconWidth()*scale, icon.getIconHeight()*(scale));
            CardLabelListener.selectedLabel.setIcon(temp.getIcon());
            CardLabelListener.selectedLabel.updateUI(); 
    }


}


public class DecreaseSizeListener implements ActionListener {
    static JLabel increasedLabel;
    @Override
    public void actionPerformed(ActionEvent e) {        
        increasedLabel = CardLabelListener.selectedLabel;
      Icon icon = CardLabelListener.selectedLabel.getIcon();

        int scale =2;
        //I can now resize images, based on my needs
        BufferedImage bi = new BufferedImage(
            icon.getIconWidth()/scale,
            icon.getIconHeight()/scale,
            BufferedImage.TYPE_INT_ARGB);
            Graphics2D g = bi.createGraphics();
            g.scale(scale,scale);
            icon.paintIcon(null,g,0,0);
            g.dispose();
            JLabel temp = new JLabel(new ImageIcon(bi));
            //to ensure proper size is kept for the enlarged image
        CardLabelListener.selectedLabel.setSize( (icon.getIconWidth()/scale), (icon.getIconHeight()/(scale)));
            CardLabelListener.selectedLabel.setIcon(temp.getIcon());
            CardLabelListener.selectedLabel.updateUI();

    }


}

推荐答案

在您的减少动作侦听器中将g.scale(scale,scale);更改为g.scale(0.5d,0.5d);

Change g.scale(scale,scale); to g.scale(0.5d,0.5d); in your decrease action listener

或者您可以这样做...

Or you could do this...

int scale = 0.5;
//I can now resize images, based on my needs
BufferedImage bi = new BufferedImage(
    icon.getIconWidth() * scale,
    icon.getIconHeight() * scale,
    BufferedImage.TYPE_INT_ARGB);
Graphics2D g = bi.createGraphics();
g.scale(scale,scale);
icon.paintIcon(null,g,0,0);
g.dispose();
// This really isn't required...
//JLabel temp = new JLabel(new ImageIcon(bi));
//to ensure proper size is kept for the enlarged image
// There is a better way...
//CardLabelListener.selectedLabel.setSize( (icon.getIconWidth()/scale), (icon.getIconHeight()/(scale)));
// This isn't required
//CardLabelListener.selectedLabel.setIcon(temp.getIcon());
// This doesn't do what you think it does...
//CardLabelListener.selectedLabel.updateUI();

CardLabelListener.selectedLabel.setIcon(new ImageIcon(bi));
CardLabelListener.selectedLabel.setSize(CardLabelListener.selectedLabel.getPreferredSize());

现在,增加和减少算法几乎相同(除了因子),您应该可以使用一种方法;)

Now both the increase and decrease algorithm's are just about the same (except for the factor), you should be able to use a single method ;)

这几乎是我最终得到的代码...

This is pretty much the code I ended up with...

public class ScaleMyIcon {

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

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

                JFrame frame = new JFrame();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new ScaleMyIconPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    protected class ScaleMyIconPane extends JPanel {

        public ScaleMyIconPane() {

            setLayout(new BorderLayout());

            ImageIcon image = null;

            try {
                image = new ImageIcon(ImageIO.read(getClass().getResource("/stormtrooper-tie.jpg")));
            } catch (IOException ex) {
                ex.printStackTrace();
            }

            JLabel label = new JLabel(image);
            add(label);

            JPanel buttons = new JPanel();
            JButton increase = new JButton("+");
            JButton decrease = new JButton("-");

            buttons.add(increase);
            buttons.add(decrease);

            increase.addActionListener(new IncreaseSizeListener(label));
            decrease.addActionListener(new DecreaseSizeListener(label));

            add(buttons, BorderLayout.SOUTH);
        }
    }

    public class Scaler {

        public Icon getScaledInstance(Icon original, double scale) {
            BufferedImage bi = new BufferedImage(
                    (int)Math.round(scale * original.getIconWidth()),
                    (int)Math.round(scale * original.getIconHeight()),
                    BufferedImage.TYPE_INT_ARGB);
            Graphics2D g = bi.createGraphics();
            g.scale(scale, scale);
            original.paintIcon(null, g, 0, 0);
            g.dispose();

            return new ImageIcon(bi);
        }
    }

    public class IncreaseSizeListener extends Scaler implements ActionListener {

        private JLabel increasedLabel;

        private IncreaseSizeListener(JLabel label) {
            increasedLabel = label;
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            Icon icon = increasedLabel.getIcon();
            int scale = 2;
            increasedLabel.setIcon(getScaledInstance(icon, scale));
        }
    }

    public class DecreaseSizeListener extends Scaler implements ActionListener {

        private JLabel decreasedLabel;

        private DecreaseSizeListener(JLabel label) {
            decreasedLabel = label;
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            Icon icon = decreasedLabel.getIcon();
            decreasedLabel.setIcon(getScaledInstance(icon, 0.5d));
        }
    }
}

已用不同的方法更新

当我在想它的时候,我注意到了两个问题.上下缩放之间没有联盟,并且您从未使用原始图像进行缩放,而是始终缩放脏图像.尝试按比例缩小图像,然后再次备份.

While I was mucking around with it, I noticed two issues. There was no coalition between the up and down scales and you were never using the original image to scale against, you were always scaling the dirty image. Try scaling the image down and back up again.

这是我对如何克服这些问题的看法

This is my take on how to overcome those issues

public class ScaleMyIcon {

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

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

                JFrame frame = new JFrame();
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLayout(new BorderLayout());
                frame.add(new ScaleMyIconPane());
                frame.pack();
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    protected class ScaleMyIconPane extends JPanel {

        public ScaleMyIconPane() {

            setLayout(new BorderLayout());

            ImageIcon image = null;

            try {
                image = new ImageIcon(ImageIO.read(getClass().getResource("/stormtrooper-tie.jpg")));
            } catch (IOException ex) {
                ex.printStackTrace();
            }

            JLabel label = new JLabel(image);
            add(label);

            JPanel buttons = new JPanel();
            JButton increase = new JButton("+");
            JButton decrease = new JButton("-");

            buttons.add(increase);
            buttons.add(decrease);

            increase.addActionListener(new IncreaseSizeListener(label));
            decrease.addActionListener(new DecreaseSizeListener(label));

            add(buttons, BorderLayout.SOUTH);

        }
    }

    public static class Scalable {

        private JLabel label;
        private Icon original;
        private static double scale = 1;

        private Scalable(JLabel label) {
            this.label = label;
            original = label.getIcon();
        }

        public JLabel getLabel() {
            return label;
        }

        public double getScale() {
            return scale;
        }

        public void setScale(double scale) {
            this.scale = scale;
        }

        public void incrementScale(double factor) {

            setScale(getScale() + factor);

        }

        public Icon getScaledInstance() {

            BufferedImage bi = new BufferedImage(
                    (int) Math.round(scale * original.getIconWidth()),
                    (int) Math.round(scale * original.getIconHeight()),
                    BufferedImage.TYPE_INT_ARGB);
            Graphics2D g = bi.createGraphics();
            g.scale(scale, scale);
            original.paintIcon(null, g, 0, 0);
            g.dispose();

            return new ImageIcon(bi);

        }
    }

    public class IncreaseSizeListener extends Scalable implements ActionListener {

        public IncreaseSizeListener(JLabel label) {
            super(label);
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            incrementScale(0.05);
            getLabel().setIcon(getScaledInstance());
        }
    }

    public class DecreaseSizeListener extends Scalable implements ActionListener {

        private DecreaseSizeListener(JLabel label) {
            super(label);
        }

        @Override
        public void actionPerformed(ActionEvent e) {
            incrementScale(-0.05);
            getLabel().setIcon(getScaledInstance());
        }
    }
}

这篇关于难以调整存储在JLabel中的图像的大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆