How can I rotate an image using Java / Swing and then set its start to 0.0?

I can rotate the image added to JLabel. The only problem is that if the height and width are not equal, the rotated image will no longer appear at the beginning of JLabel (0,0).

That's what I'm doing. I also tried to use AffineTransform and rotate the image myself, but with the same results.

Graphics2D g2d = (Graphics2D)g; g2d.rotate(Math.toRadians(90), image.getWidth()/2, image.getHeight()/2); super.paintComponent(g2d); 

If I have an image whose width is greater than its height, rotating this image using this method and then painting it will cause the image to be painted vertically above the point 0,0 and horizontally to the right of the point 0, 0.

+3
source share
4 answers

Use g2d.transform () to move the image to where it was needed. I just imagine what a calculation is, but I think something like:

 int diff = ( image.getWidth() - image.getHeight() ) / 2; g2.transform( -diff, diff ); 

By the way, you may have a problem with a shortcut that reports its preferred size - you may need to override getPreferredSize () and the account to replace the width and height of the images when rotating.

+4
source
 AffineTransform trans = new AffineTransform(0.0, 1.0, -1.0, 0.0, image.getHeight(), 0.0); g2d.transform(trans); g2d.drawImage(image, 0, 0, image.getWidth(), image.getHeight(), null); 
+3
source

The next function will rotate the buffered image, which comes undefined if it is a perfect square or not.

 public BufferedImage rotate(BufferedImage image) { /* * Affline transform only works with perfect squares. The following * code is used to take any rectangle image and rotate it correctly. * To do this it chooses a center point that is half the greater * length and tricks the library to think the image is a perfect * square, then it does the rotation and tells the library where * to find the correct top left point. The special cases in each * orientation happen when the extra image that doesn't exist is * either on the left or on top of the image being rotated. In * both cases the point is adjusted by the difference in the * longer side and the shorter side to get the point at the * correct top left corner of the image. NOTE: the x and y * axes also rotate with the image so where width > height * the adjustments always happen on the y axis and where * the height > width the adjustments happen on the x axis. * */ AffineTransform xform = new AffineTransform(); if (image.getWidth() > image.getHeight()) { xform.setToTranslation(0.5 * image.getWidth(), 0.5 * image.getWidth()); xform.rotate(_theta); int diff = image.getWidth() - image.getHeight(); switch (_thetaInDegrees) { case 90: xform.translate(-0.5 * image.getWidth(), -0.5 * image.getWidth() + diff); break; case 180: xform.translate(-0.5 * image.getWidth(), -0.5 * image.getWidth() + diff); break; default: xform.translate(-0.5 * image.getWidth(), -0.5 * image.getWidth()); break; } } else if (image.getHeight() > image.getWidth()) { xform.setToTranslation(0.5 * image.getHeight(), 0.5 * image.getHeight()); xform.rotate(_theta); int diff = image.getHeight() - image.getWidth(); switch (_thetaInDegrees) { case 180: xform.translate(-0.5 * image.getHeight() + diff, -0.5 * image.getHeight()); break; case 270: xform.translate(-0.5 * image.getHeight() + diff, -0.5 * image.getHeight()); break; default: xform.translate(-0.5 * image.getHeight(), -0.5 * image.getHeight()); break; } } else { xform.setToTranslation(0.5 * image.getWidth(), 0.5 * image.getHeight()); xform.rotate(_theta); xform.translate(-0.5 * image.getHeight(), -0.5 * image.getWidth()); } AffineTransformOp op = new AffineTransformOp(xform, AffineTransformOp.TYPE_BILINEAR); BufferedImage newImage =new BufferedImage(image.getHeight(), image.getWidth(), image.getType()); return op.filter(image, newImage); } 
+2
source

The Rotated Icon class may be what you are looking for.

+1
source

Source: https://habr.com/ru/post/1413525/


All Articles