Search code examples
javagraphicsaffinetransform

Java AffineTransform moving origin


I would like to move the origin from top left to bottom middle of the component? I have been playing with AffineTransform class could not get it to work?


Solution

  • You will need the height and width of the component that you are trying to draw. Assuming you are in the paint(Graphics g) method the simplest way is:

    paint(Graphics g){
    
    Graphics2D g2 =  (Graphics2D)g;
    
    g2.translate( component.getWidth()/2.0, component.getHeight()/2.0);
    
    //...
    
    }