Search code examples
javagraphicsjava-2d

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.


Solution

  • 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.