Boolean
Boolean

Reputation: 14664

Java2d: Translate the axes

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.

Upvotes: 2

Views: 4746

Answers (5)

malat
malat

Reputation: 12510

Just for later reference, I had to swap the order of the calls to scale and translate in my code. Maybe this will help someone in the future:

@Test
public void bottomLeftOriginTest() throws IOException {
    int width = 256;
    int height = 512;

    BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_BGR);    
    Graphics2D ig = bi.createGraphics();
    // save the "old" transform
    AffineTransform old = ig.getTransform();

    // origin is top left:
    // update graphics object with the inverted y-transform
    if (true) { /* order ok */
        ig.scale(1.0, -1.0);
        ig.translate(0, -bi.getHeight());
    } else {
        ig.translate(0, -bi.getHeight());
        ig.scale(1.0, -1.0);
    }

    int xPoints[] = new int[] { 0, width, width };
    int yPoints[] = new int[] { 0, height, 0 };
    int nPoints = xPoints.length;
    ig.setColor(Color.BLUE);
    ig.fillRect(0, 0, bi.getWidth(), bi.getHeight());

    ig.setColor(Color.RED);
    ig.fillPolygon(xPoints, yPoints, nPoints);

    // restore the old transform
    ig.setTransform(old);

    // Export the result to a file
    ImageIO.write(bi, "PNG", new File("origin.png"));
}

bottom left origin

Upvotes: 0

pradeekrathnayaka
pradeekrathnayaka

Reputation: 315

We can use the following way to resolve easily the problem,

public void paintComponent(Graphics g)
{
Graphics2D g2d = (Graphics2D) g;
// Flip the sign of the coordinate system
g2d.translate(0.0, getHeight());
g2d.scale(1.0, -1.0);
......
}

Upvotes: 2

Jeff
Jeff

Reputation: 3707

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.

Upvotes: 0

luke
luke

Reputation: 14788

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.

Upvotes: 5

Chuk Lee
Chuk Lee

Reputation: 3608

Have you tried Graphics2D.translate()?

Upvotes: 0

Related Questions