views:

47

answers:

3

Hi,

I am developing an application using Java2d. The weird thing I noticed is, the origin is at the top left corner and positive x goes right and positive y increases down.

Is there a way to move the origin bottom left?

Thank you.

A: 

Have you tried Graphics2D.translate()?

Chuk Lee
Translate itself will not do the trick.
Roman Zenka
+2  A: 

You are going to need to do a Scale and a translate.

in your paintComponent method you could do this:

public void paintComponent(Graphics g)
{
    Graphics2D g2d = (Graphics2D) g;
    g2d.translate(0, -height);
    g2d.scale(1.0, -1.0);
    //draw your component with the new coordinates

    //you may want to reset the transforms at the end to prevent
    //other controls from making incorrect assumptions
    g2d.scale(1.0, -1.0);
    g2d.translate(0, height);
}

my Swing is a little rusty but this should accomplish the task.

luke
I do not like how you set the scale/translate back. It *should* be okay in this particular special case, but in general you would accumulate floating point errors as you stack the transforms. Better to call getTransform and then setTransform to restore the original.
Roman Zenka
A: 

You're going to want to just get used to it. Like luke mentioned, you technically CAN apply a transform to the graphics instance, but that will end up affecting performance negatively.

Just doing a translate could move the position of 0,0 to the bottom left, but movement along the positive axes will still be right in the x direction and down in the y direction, so the only thing you would accomplish is drawing everything offscreen. You'd need to do a rotate to accomplish what you're asking, which would add the overhead of radian calculations to the transform matrix of the graphics instance. That is not a good tradeoff.

Jeff