【问题标题】:Mouse coordinates relative to ImageIcon within a JScrollPaneJScrollPane 中相对于 ImageIcon 的鼠标坐标
【发布时间】:2014-08-12 02:31:46
【问题描述】:

我正在用 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)。

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

【问题讨论】:

  • 考虑提供一个runnable example 来证明您的问题。这将导致更少的混乱和更好的响应
  • 谢谢你,@MadProgrammer。这个问题我觉得没必要,但是以后会考虑的。
  • 所以,如果我理解正确,您正在尝试确定鼠标单击相对于实际图像 (image) 的位置,该图像显示在 JLabel?
  • 是的,就是这样。
  • 好吧,我仍然坐在这里为你的问题挠头,如果我能复制你的问题,它会让生活(至少我的)更简单 - 只是说......

标签: java swing jscrollpane mouselistener imageicon


【解决方案1】:

基本上,使用JLabel 很难实现这一点,因为图像的实际位置由JLabel 的外观代表决定。虽然您可以创建自己的委托,但最终需要为每个受支持的平台创建一个委托,而且……我太懒了……

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

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

    }

}

查看Performing Custom Painting2D Graphics 了解更多详情

【讨论】:

  • 这看起来像是解决方案。仍然试图将我的头包裹在代码上,这很漂亮。非常感谢您的宝贵时间,MadProgrammer。一个问题,当图像高度大于框架高度并且用户必须向下滚动时,这仍然有效吗?
  • 是的,但是您会发现滚动窗格需要调整大小以适应整个图像。在这种情况下,您需要实现Scrollable 接口,它提供了将JScrollPane 告知prefferedViewportSize 的方法
  • 感谢 MadProgrammer 的解决方案。如何在 mouseClicked 事件上更新 imagePanel?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-10-20
  • 1970-01-01
  • 1970-01-01
  • 2014-04-08
  • 1970-01-01
  • 1970-01-01
  • 2018-11-16
相关资源
最近更新 更多