0

我设法增加了 JLabel 的图像(其中存储了一个 imageIcon)。当我按下增加尺寸按钮时,面板上的图像原始尺寸增加了,这正是我想要的。但是,当我单击减小尺寸按钮时(我认为将其除以比例可能会修复它)标签减小,但实际图像外观(我猜的尺寸)发生了变化。它不是减小尺寸,就像我的增加按钮增加尺寸一样。我花了几个小时试图弄清楚为什么通过乘以它,我能够增加标签的大小和其中的图像(这意味着不仅标签在增加,实际图像也在增加)但减少(我是除法而不是乘法)它不起作用。这是我的增加和减少听众。

    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();

    }


}
4

1 回答 1

0

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

或者你可以这样做...

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());

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

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

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

用不同的方法更新

当我在处理它时,我注意到了两个问题。上下比例之间没有联合,并且您从未使用原始图像进行缩放,您总是在缩放脏图像。尝试缩小图像并再次备份。

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

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());
        }
    }
}
于 2012-10-31T03:28:22.123 回答