I am working on a java application similar to the windows snipping tool. (Thats a tool for taking screenshot and annotating them)
I am using a JLabel to display the image (by setting the JLabels icon to the image). Now I have a “pen” tool that allows drawing on the image. I also have the possibility to zoom in and out on the image, which works by keeping track of the total zoom amount, scaling the original image accordingly and applying the scaled image to the JLabels icon.
My problem is that the exact position of the mouse cursor on the image is incorrectly calculated in some cases.
In some cases meaning this:
- Original image larger then window (regardless of zoom): correct
- Original image smaller then window and no zoom: correct
- Original image smaller then window and zoomed to larger or equal to window: correct
- Original image smaller then window and zoomed to smaller then window: incorrect
Window meaning the available space in the window in this case.
Incorrect here means that it draws some amount to the left of the cursor. The offset is biggest halfway between no zoom and zoomed to fit the window exactly. So zooming in on the image, the offset increases until that midpoint is reached, then zooming in further, it decreases again until it matches perfectly when the image is zoomed to fit the window exactly. Zooming in further works and the cursor position matches perfectly with the drawing position.
The reason why this only effects the X axis (left-right) and not the Y axis (top-bottom) is that when the screenshot is first taken the entire application window is sized to fit the image if it can, so the Y size of the image is basically guaranteed to be equal to or larger then the space available in the window unless zoomed out.
Here is the code I am using to calculate the x and y position on the image:
int x = (int)Math.round(mouseEvent.getPoint().getX());
int y = (int)Math.round(mouseEvent.getPoint().getY());
double zoomX = (double) screenCapture.getImage().getWidth() / editorPane.getIcon().getIconWidth();
double zoomY = (double) screenCapture.getImage().getHeight() / editorPane.getIcon().getIconHeight();
int offsetX = (editorPane.getWidth() - editorPane.getIcon().getIconWidth()) / 2; //TODO: Fix pen position in case of zoomed small image
int offsetY = (editorPane.getHeight() - editorPane.getIcon().getIconHeight()) / 2;
x = (int) Math.round(x * zoomX);
y = (int) Math.round(y * zoomY);
x -= offsetX;
y -= offsetY;
editorPane
is the JLabel, screenCapture.getImage()
returns the original image as a BufferedImage
and offsetX and offsetY are compensating for the original image being smaller then the available space in the window (without this, the same problem exist for images smaller then the available space even when not zoomed).
I dont know why an offset between the actual cursor location and the calculated position on the image exists, as I thought the offsetX and offsetY calculations Im doing should always compensate for that, but clearly it doesnt.
Testing shows that apparently the problem seems to be that the calculated offset overshoots and moves the drawing location to far left. Without the offset, the drawing location always ends up some distance to the right of the cursor.
It is unclear to me why this overshoots and I cant figure out how to fix it.
Does anyone have an idea how to fix this offset to ensure that the drawing point is always exactly where the mouse pointer is?