Skip to content
Discussion options

You must be logged in to vote

You should be able to use the getWidthInTiles() and getHeightInTiles() methods to convert mouse coordinates to tile coordinates using a calculation similar to the following:

panel.addMouseListener(new MouseAdapter() {
    @Override
    public void mousePressed(final MouseEvent e) {
        super.mouseMoved(e);
        System.out.println("Mouse Pressed");
        System.out.println("\t(x, y): " + e.getX() + ", " + e.getY());

        final int tileX = e.getX() / (panel.getWidth() / panel.getWidthInTiles());
        final int tileY = e.getY() / (panel.getHeight() / panel.getHeightInTiles());
        System.out.println("\t(tiltX, tileY): " + tileX + ", " + tileY);
    }
});

Demo

Preview

Code

Replies: 2 comments 1 reply

Comment options

You must be logged in to vote
0 replies
Answer selected by akurutin
Comment options

You must be logged in to vote
1 reply
@Valkryst
Comment options

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants