Re: Flipping Y axis in a Graphics 2D??
From: Alex Hunsley (alex.hunsley_at_blueyonder.munge.co.uk)
Date: 12/21/03
- Previous message: Josh D.King: "Re: Flipping Y axis in a Graphics 2D??"
- In reply to: Chris Grant: "Flipping Y axis in a Graphics 2D??"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Sat, 20 Dec 2003 23:04:30 +0000
Chris Grant wrote:
> Hello:
>
> I am trying to do something that is supposed to be simple (?). In the
> attached code, I have drawn a red dot at top right and blue dot at
> lower left. I am tryin to flip the Y axis so I expect the red dot at
> the lower right and the blue at the top left using:
>
> Graphics2D g2d = (Graphics2D)getGraphics();
> g2d.scale(1, -1);
>
> The -1 is supposed to flip the y axis.
And it does!
> Nothing draws at all.
This is what I'd expect...
> If you
> comment out the g2d.scale(1, -1); line, then everything works fine.
> What am I doing wrong. Can you please show how I would correct it to
> do what I want?
The reason you don't see anything is: by scaling all y coordinates by
-1, you're causing all positive coords to be plotted at negative
locations (and vice versa).. and the windows' visible space shows Y
coords from 0 up to 800. So you end up plotting stuff off the top of
your window, where you can't see it!
It sounds like you're wanting to mirror the Y coordinates about the
central horizontal line of the window.
So y=800 maps to y=0, and y=0 maps to y=800, with the same idea applying
to everything inbetween.
The equation to this mirroring is: y' = 800 - y.
IF you want to do this transformation in the Graphics2D object, as you
appear to want to do, you do it by first scaling the Y coords by -1
(like you're doing now), and then by adding 800 to all the Y coords (in
other words, a 'translation' of (0, 800)).
This will get you the result you want.
Look at Graphics2D's translate(dx,dy) command for more help with how to
call translation...
alex
- Previous message: Josh D.King: "Re: Flipping Y axis in a Graphics 2D??"
- In reply to: Chris Grant: "Flipping Y axis in a Graphics 2D??"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|