Robben_Ford_Fan_boy Robben_Ford_Fan_boy - 2 years ago 93
Java Question

Java SWT: Getting original X/Y Coordinates from Scaled Image

I have an image where

Height = 1300
Width = 1300

I scaled the image to:
ScaledHeight = 700
ScaledWidth = 700

I am using the following to get the original Coordinates:

public Coordinate GetScaledXYCoordinate(int oldX, int oldY, int width, int height, int scaledWidth, int scaledHeight)
int newX = (int)(oldX * width)/scaledWidth;
int newY = (int)(oldY * height)/scaledHeight;

Coordinate retXY = new Coordinate(newX, newY);
return retXY;

I have updated to include answer

Answer Source


int width = 1300;
int scaledWidth = 700;

the value of (width/scaledWidth) is 1 - you are doing integer arithmetic not floating point.


int newX = (oldX * width) /scaledWidth;
int newY = (oldY * height) /scaledHeight;

to avoid this issue.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download