Java get mouse location on JLabel icon

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?

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật