Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: Rotating Images

Tags:

I need to be able to rotate images individually(in java). The only thing I have found so far is g2d.drawImage(image, affinetransform, ImageObserver ). Unfortunately, I need to draw the image at a specific point, and there is no method with an argument that 1.rotates the image separately and 2. allows me to set the x and y. any help is appreciated

like image 859
Jimmt Avatar asked Dec 26 '11 22:12

Jimmt


2 Answers

This is how you can do it. This code assumes the existance of a buffered image called 'image' (like your comment says)

// The required drawing location int drawLocationX = 300; int drawLocationY = 300;  // Rotation information  double rotationRequired = Math.toRadians (45); double locationX = image.getWidth() / 2; double locationY = image.getHeight() / 2; AffineTransform tx = AffineTransform.getRotateInstance(rotationRequired, locationX, locationY); AffineTransformOp op = new AffineTransformOp(tx, AffineTransformOp.TYPE_BILINEAR);  // Drawing the rotated image at the required drawing locations g2d.drawImage(op.filter(image, null), drawLocationX, drawLocationY, null); 
like image 162
AlanFoster Avatar answered Sep 21 '22 12:09

AlanFoster


AffineTransform instances can be concatenated (added together). Therefore you can have a transform that combines 'shift to origin', 'rotate' and 'shift back to desired position'.

like image 42
Andrew Thompson Avatar answered Sep 19 '22 12:09

Andrew Thompson