Skip to main content

[JAVA2D] Retrieving 'untransformed' co-ordinates of a transformed point

2 replies [Last post]
Anonymous

Dear All,

Is there a way to retrieve the untransformed, 'absolute' co-ordinates of a
point from a transformed Graphics2D context?

For example, if I set up a BufferedImage 250x250, grab its Graphics2D
object, perform a bunch of AffineTransforms on it (translate, rotate, etc.)
and then plot a pixel at co-ordinates (0,0) that pixel will obviously not
appear, in the grand scheme of things, at 'absolute' (0,0) within the
BufferedImage as a whole. In pseudo-code...

Image image = new BufferedImage( 250, 250 );
Graphics2D g = image.getGraphics();
g.translate(100,100);
g.setPixel( 0, 0, Color.black );

...this would generate a pixel that, 'in the grand scheme of things' is
actually at point (100,100) within the BufferedImage, even though I plotted
it at point (0,0).

Is there any way I can determine what point (100,100) 'means', in
untransformed terms? I can only assume the Graphics2D object must be doing
something along these lines 'behind the scenes', but is there anyway I can
get at the equivalent 'untransformed' co-ordinate?

Many thanks in advance,

Richard.

===========================================================================
To unsubscribe, send email to listserv@java.sun.com and include in the body
of the message "signoff JAVA2D-INTEREST". For general help, send email to
listserv@java.sun.com and include in the body of the message "help".

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
Ted Hill

Check out these methods, they may give you what you want.

Graphics2D.getTransform();

AffineTransform.inverseTransform(...)

Ted Hill

> -----Original Message-----
> From: Discussion list for Java 2D API [mailto:JAVA2D-
> INTEREST@JAVA.SUN.COM] On Behalf Of Richard Kennard
> Sent: Thursday, May 13, 2004 6:29 AM
> To: JAVA2D-INTEREST@JAVA.SUN.COM
> Subject: [JAVA2D] Retrieving 'untransformed' co-ordinates of a
transformed
> point
>
> Dear All,
>
> Is there a way to retrieve the untransformed, 'absolute' co-ordinates
of a
> point from a transformed Graphics2D context?
>
> For example, if I set up a BufferedImage 250x250, grab its Graphics2D
> object, perform a bunch of AffineTransforms on it (translate, rotate,
> etc.)
> and then plot a pixel at co-ordinates (0,0) that pixel will obviously
not
> appear, in the grand scheme of things, at 'absolute' (0,0) within the
> BufferedImage as a whole. In pseudo-code...
>
> Image image = new BufferedImage( 250, 250 );
> Graphics2D g = image.getGraphics();
> g.translate(100,100);
> g.setPixel( 0, 0, Color.black );
>
> ...this would generate a pixel that, 'in the grand scheme of things'
is
> actually at point (100,100) within the BufferedImage, even though I
> plotted
> it at point (0,0).
>
> Is there any way I can determine what point (100,100) 'means', in
> untransformed terms? I can only assume the Graphics2D object must be
doing
> something along these lines 'behind the scenes', but is there anyway I
can
> get at the equivalent 'untransformed' co-ordinate?
>
> Many thanks in advance,
>
> Richard.
>
>
========================================================================
==
> =
> To unsubscribe, send email to listserv@java.sun.com and include in the
> body
> of the message "signoff JAVA2D-INTEREST". For general help, send
email to
> listserv@java.sun.com and include in the body of the message "help".

===========================================================================
To unsubscribe, send email to listserv@java.sun.com and include in the body
of the message "signoff JAVA2D-INTEREST". For general help, send email to
listserv@java.sun.com and include in the body of the message "help".

mgrev
Offline
Joined: 2003-08-12

The transforming of coordinates is done by an AffineTransform object contained in the Graphics2D object. It holds all transform, rotate, sheer and scale information in a matrix.

Use
[code]g2.getTransform().createInverse()
[/code]
to get a transform object that translates points "backwards".

You can also, maybe easier, do:
[code]
g2.getTransform().inverseTransform(Point2D src, Point2D dst)
[/code]
to transform a point "backwards".

It's all in the JavaDocs for Graphics2D.

Cheers,
Mikael