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