1

我正在尝试为 2D RPG 制作地图编辑器,而我目前正在尝试做的(在地面上放置瓷砖)是有一个带有 JLabel 的 JScrollPane (其中有一个图像)和一个鼠标监听器到 JScrollPane 以确定图像的 X 和 Y 位置。我遇到的问题是它没有得到图像 X 和 Y 位置,而是 JScrollPanes X 和 Y 位置。

所以我有一个 JScrollPane 附加到一个 512x4928 的图像上,我附加了一个鼠标监听器。当我尝试获取 Y 位置时,问题仍然存在,因为 JScrollPane 是一个单独的对象,它获取 JScrollPane 大小的 X 和 Y JScrollPanes 大小是 512x600 无论图像在哪里,它都不会返回大于600。

有什么办法可以使这项工作?

这是代码

public void loadMapTileImage(){
    try {
        image = ImageIO.read(getClass().getResource("data/misc/tiledata.png"));
        image = image.getSubimage(0, 0, 512, 4928);
    } catch (IOException e) {
        e.printStackTrace();
    }
    ImageIcon i = new ImageIcon(image);
    MapEditorGlobalObjects.mapTileScroll = new JScrollPane(new JLabel(i));
}

mapTileScroller.addMouseListener(new MouseListener() {

        @Override
        public void mouseReleased(MouseEvent arg0) {
            MapEditorGlobalObjects.checkIfDebugging("Mouse Released Location X = "+arg0.getX());
            MapEditorGlobalObjects.checkIfDebugging("Mouse Released Location Y = "+arg0.getY());

        }
        @Override
        public void mousePressed(MouseEvent arg0) {
            MapEditorGlobalObjects.checkIfDebugging("Mouse Clicked Location X = "+arg0.getX());
            MapEditorGlobalObjects.checkIfDebugging("Mouse Clicked Location Y = "+arg0.getY());

        }
4

2 回答 2

3

MouseListener尝试将其添加到滚动窗格的视图组件(即滚动窗格显示的内容)中,而不是将其添加到滚动窗格中

此示例中的图像为 2560x1600

在此处输入图像描述

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.FontMetrics;
import java.awt.Graphics;
import java.awt.Graphics2D;
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 javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;

public class TestScrollPanePos {

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

    public TestScrollPanePos() {
        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 JScrollPane(new TestPane()));
                frame.setSize(400, 400);
                frame.setLocationRelativeTo(null);
                frame.setVisible(true);
            }
        });
    }

    public class TestPane extends JPanel {

        private BufferedImage bg;
        private Point point;

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

            addMouseMotionListener(new MouseAdapter() {

                @Override
                public void mouseMoved(MouseEvent e) {
                    System.out.println(e.getPoint());
                    point = e.getPoint();
                    repaint();
                }

            });
        }

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

        @Override
        protected void paintComponent(Graphics g) {
            super.paintComponent(g);
            if (bg != null) {
                Graphics2D g2d = (Graphics2D) g.create();
                int x = (getWidth() - bg.getWidth()) / 2;
                int y = (getHeight()- bg.getHeight()) / 2;
                g2d.drawImage(bg, x, y, this);
                if (point != null) {
                    FontMetrics fm = g2d.getFontMetrics();
                    g2d.setColor(Color.BLACK);
                    g2d.drawString(point.x + "x" + point.y, point.x, point.y - fm.getHeight() + fm.getAscent());
                }
                g2d.dispose();
            }
        }
    }

}
于 2013-09-24T23:29:08.367 回答
0

如果为此使用 JScrollPane,则获取垂直滚动条的值,并将其添加到鼠标侦听器的 Y 值中,您将获得正确的值

于 2013-09-24T23:26:19.893 回答