在JScrollPane中相对于ImageIcon的鼠标坐标

3

我正在使用Java构建桌面应用程序。 我想获取相对于JSrollPane中的图像的鼠标单击的鼠标坐标。 JScrollPane(screenScroll)包含在具有BorderLayout的JPanel中。

    final JLabel screenLabel = new JLabel(new ImageIcon(image));
    JScrollPane screenScroll = new JScrollPane(screenLabel);
    screenScroll.getViewport().setBackground(Color.white);

    screenLabel.addMouseListener(new MouseAdapter() {

        @Override //I override only one method for presentation
        public void mousePressed(MouseEvent e) {
            System.out.println("Y'all clicked at: "+e.getX() + ", " + e.getY()+" in the image.");
        }
    });

问题是这样的:JPanel比图像大,而JScrollPane占据了JPanel的100%(看起来很好,我很高兴),但mousePressed事件给出的坐标是相对于JScrollPane / JPanel而不是图像,因此x坐标是偏移的(即使MouseListener被添加到包含ImageIcon的JLabel中)。

希望我已经清楚地解释了。如何修改上述代码以获取相对于图像的坐标?


2
请考虑提供一个可运行的示例,以演示您的问题。这将减少混淆并获得更好的响应。MCVE - MadProgrammer
谢谢,@MadProgrammer。我没想到这个问题需要这样做,但以后我会考虑的。 - Ishikawa
是的,就是这样。 - Ishikawa
嗯,我仍然坐在这里为你的问题苦恼,如果我能够复制你的问题,那将会让生活变得更简单(至少是我的生活)- 只是这么说... - MadProgrammer
啊,做不到啊……真的……图像的位置是未知的,实际上取决于安装的外观委托……话虽如此,您可以创建自己的图像面板,这将允许您保持所需的信息以翻译鼠标点击… - MadProgrammer
显示剩余2条评论
1个回答

6
基本上,使用JLabel实现这个功能是非常困难的,因为图像的实际位置是由JLabel的外观委托确定的。虽然您可以创建自己的委托,但最终您需要为每个支持的平台创建一个委托...我太懒了...

相反,您可以创建一个自定义组件并按照您想要的方式呈现图像。然后,您将能够更好地确定图像的位置并转换您需要的鼠标点值,例如...

enter image description here

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Graphics;
import java.awt.Point;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class Main {

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

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

                    BufferedImage img = ImageIO.read(new File("C:\\hold\\thumbnails\\MT015.jpg"));
                    final ImagePanel imgPane = new ImagePanel(img);
                    JScrollPane scrollPane = new JScrollPane(imgPane);
                    final JLabel report = new JLabel("...");

                    imgPane.addMouseListener(new MouseAdapter() {
                        @Override
                        public void mouseClicked(MouseEvent e) {
                            Point panelPoint = e.getPoint();
                            Point imgContext = imgPane.toImageContext(panelPoint);

                            report.setText("You clicked at " + panelPoint + " which is relative to the image " + imgContext);
                        }
                    });

                    JFrame frame = new JFrame("Testing");
                    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    frame.add(scrollPane);
                    frame.add(report, BorderLayout.SOUTH);
                    frame.pack();
                    frame.setLocationRelativeTo(null);
                    frame.setVisible(true);
                } catch (IOException ex) {
                    Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        });
    }

    public class ImagePanel extends JPanel {

        private BufferedImage img;

        public ImagePanel(BufferedImage img) {
            this.img = img;
        }

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

        protected Point getImageLocation() {

            Point p = null;
            if (img != null) {
                int x = (getWidth() - img.getWidth()) / 2;
                int y = (getHeight() - img.getHeight()) / 2;
                p = new Point(x, y);
            }
            return p;

        }

        public Point toImageContext(Point p) {
            Point imgLocation = getImageLocation();
            Point relative = new Point(p);
            relative.x -= imgLocation.x;
            relative.y -= imgLocation.y;
            return relative;
        }

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            if (img != null) {
                Point p = getImageLocation();
                g.drawImage(img, p.x, p.y, this);
            }
        }

    }

}

请查看执行自定义绘制2D图形了解更多详细信息。


这看起来像是解决方案。我仍在努力理解这段优美的代码。非常感谢您的时间,MadProgrammer。还有一个问题,如果图像高度大于框架高度并且用户必须向下滚动,这仍然有效吗? - Ishikawa
是的,但您会发现滚动窗格希望调整大小以适应整个图像。在这种情况下,您需要实现“Scrollable”接口,该接口提供了告诉“JScrollPane”所需视口大小的方法。 - MadProgrammer
感谢MadProgrammer提供的解决方案。我如何在鼠标单击事件中更新imagePanel? - Abhishek

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接