Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / GUI / December 2005

Tip: Looking for answers? Try searching our database.

2d zooming strategy / scaling but maintaining coordinate context

Thread view: 
Graham Street - 09 Dec 2005 03:05 GMT
Hey there,
I need to be able to dynamically resize a shape to fill up a fixed
drawing area... kind of an auto-zoom type behavior.   I've acheived the
zoom-in and zoom-out by getting the bounds of my polygon, comparing
that to the height/width of the drawing area, and calling g2d.scale
with the relative difference.      The problem is that once I scale it
up (zoom in) my pixel coordinates don't match any more....   so the
image isn't where it appears to be...   what is the sensible way to
approach this?

do I have to translate the coordinates (from mouse clicks, etc)  based
on the scaling factor?  hopefully not!  if so, is there an easy way to
make such a coordinate translation?

thanks!

Graham Street
Thomas Weidenfeller - 09 Dec 2005 12:37 GMT
> do I have to translate the coordinates (from mouse clicks, etc)  based
> on the scaling factor?

Yes. Mouse coordinates are always in device space coordinates. If you
want to relate them to user space coordinates, you need to do the
necessary coordinate space transformation.

> hopefully not!  if so, is there an easy way to
> make such a coordinate translation?

Are approx. five to ten lines of code to much?

* If you have complex scaling, rotation, translation, etc:

Use an explicit AffineTransform for the scaling, instead of just
manipulating the default one in Graphics2D. Keep that transform. In the
mouse listener use AffineTransform.inverseTransform() to transform the
device space coordinates into your user space coordinates.

* If you have a simple linear scaling:

Remember the zoom factor and just divide the coordinates in the mouse
listener by that factor.

/Thomas
Signature

The comp.lang.java.gui FAQ:
ftp://ftp.cs.uu.nl/pub/NEWS.ANSWERS/computer-lang/java/gui/faq
http://www.uni-giessen.de/faq/archiv/computer-lang.java.gui.faq/

Graham Street - 28 Dec 2005 18:16 GMT
I've realized that doing a zoom isn't going to work out, since that
will make the text appearing on my panel, etc get bigger... I just want
to increase the dimensions of my shape.   So, I've settled on iterating
through my coordinates and adjusting the lengths of each line by a
scaling factor.   This way the shape grows to fill the panel but the
other drawn items remain the same scale.    But, this is getting pretty
challenging with non-rectangles... It's getting messy.   I'm wondering
if there is a cleaner way.    Does anyone know of any tricks for doing
coordinate scaling transforms like this?  my problems seem to mostly be
related to trying to keep the position of the first point fixed.

I am dealing with the shape as "lines" consisting of two points each.
 I go through each line and change the second point (x2 or y2
depending on the plane) to reflect the new length (multiplied by
scaling factor, eg len * 1.5).   But once I get to the underside of the
shape,  things get out of whack.

> > do I have to translate the coordinates (from mouse clicks, etc)  based
> > on the scaling factor?
[quoted text clipped - 25 lines]
> ftp://ftp.cs.uu.nl/pub/NEWS.ANSWERS/computer-lang/java/gui/faq
> http://www.uni-giessen.de/faq/archiv/computer-lang.java.gui.faq/


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.