Win a copy of Kotlin in Action this week in the Kotlin forum!
programming forums Java Java JSRs Mobile Certification Databases Caching Books Engineering Languages Frameworks Products This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
Sheriffs:
Saloon Keepers:
Bartenders:

# Zoom and centralize object

Vinicius Moreira
Greenhorn
Posts: 27
Hello everybody, I am developing an application which will be necessary to implement the zoom on a particular map.
The Zoom I could implement using the AffineTransform class, but I would like the location where the user clicked the mouse to perform the zoom is centered on the screen.
How could I perform the calculation of the translation of the site click so it can be centered on the screen.
Thank you.

VinÃ­cius

Jayesh A Lalwani
Rancher
Posts: 2762
32
To zoom into a particular point, you need to first move the point clicked on to the center and then zoom into the center. Moving a particular point to the center is called translation and zooming is achieved using a transformation called scaling. So, what you need to do is first translate, then scale. AffineTransform contains methods that apply both the transformations. SOmething like this

The distance that you want to translate is calculated from the center, so x and y above are the distance of the clicked point from the center.

Vinicius Moreira
Greenhorn
Posts: 27
Jayesh A Lalwani wrote:To zoom into a particular point, you need to first move the point clicked on to the center and then zoom into the center. Moving a particular point to the center is called translation and zooming is achieved using a transformation called scaling. So, what you need to do is first translate, then scale. AffineTransform contains methods that apply both the transformations. SOmething like this

The distance that you want to translate is calculated from the center, so x and y above are the distance of the clicked point from the center.

Thanks for responding.
For example, if I want to zoom into a point at coordinate x = 30 and y = 50 and my screen is 800 px wide and 600 high should I calculate the distance should I move this way: xCenter = 30 - 400 and yCenter = 50 - 300?

Vinicius Moreira
Greenhorn
Posts: 27
I implemented here but failed.
I'll post the code below.
Figure yet is not correctly centered. What could be wrong?