Rotate a buffered image in Java

As always, the Internet to the rescue. So, this is some code which I hobbled together from other resources/post/blogs which will return a new image which is sized so it will contain the rotated image public BufferedImage rotateImageByDegrees(BufferedImage img, double angle) { double rads = Math.toRadians(angle); double sin = Math.abs(Math.sin(rads)), cos = Math.abs(Math.cos(rads)); int w … Read more

Rotate JLabel or ImageIcon on Java Swing

Instead of rotating the component itself, consider rotating the content of a component. This example draws a rotated image in a JPanel. Addendum: In the example RotatableImage.getImage() creates a BufferedImage directly in memory, but you can use ImageIO.read() to obtain an image from elsewhere. BufferedImage#createGraphics() is supported if you want to modify the image, but … Read more

Java 2d rotation in direction mouse point

Using the Graphics2D rotation method is indeed the easiest way. Here’s a simple implementation: int centerX = width / 2; int centerY = height / 2; double angle = Math.atan2(centerY – mouseY, centerX – mouseX) – Math.PI / 2; ((Graphics2D)g).rotate(angle, centerX, centerY); g.fillRect(…); // draw your rectangle If you want to remove the rotation when … Read more

Android: How to rotate a bitmap on a center point

I hope the following sequence of code will help you: Bitmap targetBitmap = Bitmap.createBitmap(targetWidth, targetHeight, config); Canvas canvas = new Canvas(targetBitmap); Matrix matrix = new Matrix(); matrix.setRotate(mRotation,source.getWidth()/2,source.getHeight()/2); canvas.drawBitmap(source, matrix, new Paint()); If you check the following method from ~frameworks\base\graphics\java\android\graphics\Bitmap.java public static Bitmap createBitmap(Bitmap source, int x, int y, int width, int height, Matrix m, boolean … Read more