• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Graphics.translate(x,y) ?

 
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Maybe one of you can answer this.
The docs for translate() state:
Translates the origin of the graphics context to the point (x, y) in the
current coordinate system. Modifies this graphics context so that its new
origin corresponds to the point (x, y) in this graphics context's original
coordinate system. All coordinates used in subsequent rendering operations
on this graphics context will be relative to this new origin.

If I translate the origin from the top-left of a page, to a point say 10
pixels right and 50 pixels down,
translate(10,50);
When I draw something at (0,0) it's going to be drawn inset by 10 pixels
from the left, and 50 pixels from the top of the page.
My question is, if I now do another translate:
translate(10, 500);
is that additive to the previous call, so in reality, my new origin is now
at 20, 550 from the original top-left of the page? Or does it always use the
'original' origin to base the translation on?
Does anyone know?
Thanks.
 
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Rob,
I'm gonna take a guess at this one and say yes. Are you casting to a Graphics2D? If so you may be able to figure it out after the second translate by a call to getTransform() which returns an AffineTransform for the current context. I may run a little test just out of curiosity.
Michael Morris
SCJP
 
Rob Ross
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If it's additive, is there a way to resore the origin to it's original state? I want to be able to focus on little pieces of the Graphics at one time, and translate and clip to a sub-area, and pass the Graphics to a method that then just draws in it's little sub-area, oblivious to the rest of the Graphics. I also want the method to think the top-left starting area of it's clipping Rect is 0,0.
But I don't want to have to keep track of the current translate operation between calls; I want to be able to translate to a new origin, based on the *original* Graphics origin point.
 
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Unless I'm misunderstanding your question...
It's "additive."
 
Michael Morris
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Rob,
Now I'm not sure that it is additive. Your best bet (assuming you're using Java 2) is to cast your context to a Graphics2D and use AffineTransorms for your translations and forget about the translate() method. The Graphics2D class has [get,set]Transform() methods which accept/return AffineTransforms which are 3 x 3 matrices which define the translation/rotation/scale/shear. Just pull up the javadoc on AffineTransform and pull out the old analytic geometry book and that should get you goin'. Of course with all AffineTransforms there is an invert which will restore the context back to the original coordinates, rotation, etc.
Hope this helps
Michael Morris
SCJP
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ah, now I see what you're getting at. Java obviously keeps track of this information, just consider a call to repaint().
I only know how to get at it somewhat indirectly:
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It is additive. Just try it out (and read the documentation).
From the documentation: "All coordinates used in subsequent rendering operations on this graphics context will be relative to this new origin."
 
Rob Ross
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, it's pretty clear now that it's additive.
On the subject of AffineTransforms, well, I'm just starting to read up on the java 2D API, but that's way overkill for what I need right now.
I just want some way of being able to translate the origin, but always from a known starting point,ie, the *original* origin of the Graphics, which I can't seem to find any methods for.
So what it looks like i'll have to do is after I make a call to transform(10,50), I have to "undo" it by making a call to transform(-10,-50), which I really wish I didn't have to do. a restoreOrigin() method would be very useful here.
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Ok, I'm curious...
Why don't you like my idea of getting the Rectangle representation of the clip bounds and using its top-left corner (r.x, r.y) as your new origin?
 
Rob Ross
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
because I am already setting the clip rect to something else, so I would loose the value. Like I mentioned, I am translating, AND clipping, so that each method that I pass the Graphics to can just draw into its own private little "sandbox" and not worry about clobbering anything else on the page. Your way would work if I wasn't already setting the clip.
 
reply
    Bookmark Topic Watch Topic
  • New Topic