1

我正在尝试使用鼠标运动侦听器缩放图像,但它不起作用。所以现在我正在使用 2 手动执行此操作JTextfields。我从中获取值Jtextfields,然后将该值传递给getScaledInstance()方法。但它不工作。

我的代码:

        final JTextField jj = new JTextField();
        jj.setColumns(5);
        buttonPane.add(jj);
        JButton btn  = new JButton("Resize");
        final JTextField jj1 = new JTextField();
        jj1.setColumns(5);
        buttonPane.add(jj1);
        buttonPane.add(btn);
        btn.addActionListener(new ActionListener() {


            @Override
            public void actionPerformed(ActionEvent arg0) 
            {
                int x= Integer.parseInt(jj.getText());
                int y=Integer.parseInt(jj1.getText());

                BufferedImage b = a;
                b.getScaledInstance(x, y,BufferedImage.TYPE_INT_ARGB);
                label.setIcon(new ImageIcon(b));
            }
        });

a是从堆栈中弹出的 BufferedImage 实例。并且labelJLabel

4

1 回答 1

3

您需要将返回引用分配给某些东西......

Image scaled = b.getScaledInstance(x, y,BufferedImage.TYPE_INT_ARGB);
label.setIcon(new ImageIcon(scaled));

您可能还想通读Image.getScaledInstance() 的危险

使用鼠标滚轮缩放更新

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.event.MouseWheelEvent;
import java.awt.event.MouseWheelListener;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.text.NumberFormat;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.Timer;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class MouseScaleTest {

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

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

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

    public class TestPane extends JPanel {

        private BufferedImage img;

        private float scale = 1f;
        private float scaleDelta = 0.05f;

        public TestPane() {

            try {
                img = ImageIO.read(new File("/path/to/your/image"));
            } catch (IOException ex) {
                ex.printStackTrace();
            }

            addMouseWheelListener(new MouseWheelListener() {

                @Override
                public void mouseWheelMoved(MouseWheelEvent e) {
                    int rotation = e.getWheelRotation();
                    if (rotation < 0) {
                        scale -= scaleDelta;
                    } else {
                        scale += scaleDelta;
                    }
                    if (scale < 0) {
                        scale = 0;
                    } else if (scale > 1) {
                        scale = 1;
                    }
                    repaint();
                }
            });
        }

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

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            if (img != null) {
                Graphics2D g2d = (Graphics2D) g.create();

                int x = (int)((getWidth() - (img.getWidth() * scale)) / 2);
                int y = (int)(getHeight() - (img.getHeight() * scale)) / 2;

                AffineTransform at = new AffineTransform();
                at.translate(x, y);
                at.scale(scale, scale);

                g2d.setTransform(at);
                g2d.drawImage(img, 0, 0, this);

                g2d.dispose();
            }
        }
    }
}

花时间阅读如何编写鼠标侦听器如何编写鼠标滚轮侦听器

于 2013-08-23T06:33:47.773 回答