How do I get the X and Y location of an image in a JScrollPane

偶尔善良 提交于 2019-11-30 20:37:48

问题


I am trying to make a Map Editor for a 2D RPG, and what I currently am trying to do (to place tiles on the ground) is to have a JScrollPane with a JLabel (that has an image in it) and a Mouse Listener attached to the JScrollPane to determine the X and Y location of the image. The problem I run into is that it doesn't get the Images X and Y location but the JScrollPanes X and Y location.

So I have a JScrollPane attached to an Image that is 512x4928, I attached a mouse listener to it. the problem resides when I try to get the Y location, since JScrollPane is a separate object it gets the X and Y of the size of the JScrollPane JScrollPanes size is 512x600 no matter where the image is at, it will never return a value greater than 600.

Any way I can make this work?

Heres the the Code

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

        }

回答1:


Instead of adding the MouseListener to the scrollpane, try adding it to the scroll pane's view component (ie, the thing that the scroll pane is displaying)

The image in this example is 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();
            }
        }
    }

}



回答2:


If you use JScrollPane for this, get the value of the vertical scrollbar, and add it to the Y value from the mouse listener and you will have the correct value



来源:https://stackoverflow.com/questions/18993455/how-do-i-get-the-x-and-y-location-of-an-image-in-a-jscrollpane

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!