使用 Swing 在图像上旋转矩形 [英] Rotating a Rectangle on an Image using Swing

查看:36
本文介绍了使用 Swing 在图像上旋转矩形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

好的,当用户在图像顶部绘制矩形时,我想提前找到所有图像旋转角度(90,180,270,360)的所有旋转矩形.

Ok when a user draws a Rectangle on top of an image, I want to find all the rotated Rectangles ahead of time for all image rotation angles (90,180,270,360).

根据 Java API,我可以继续调用 Graphics2D rotate() 方法.然后我可以使用这个 Graphics2D 转换器来获得旋转的矩形.

According to Java API I can just keep calling the Graphics2D rotate() method. I can then use this Graphics2D transformer to get the rotated Rectangle.

这适用于第一次旋转(1.5708)调用.我得到了正确的矩形点.使用 Transformer 后,所有其他调用都返回错误的矩形点.

This works for the very first rotation(1.5708) call. I get the correct Rectangle Point. All other calls after that return the wrong Rectangle Point after using Transformer.

我认为我的问题是 Graphics2D translate(x,y).我不明白如何使用它.

I think my problem is the Graphics2D translate(x,y). I don't understand how to use it.

有人知道如何修复我的代码,以便每次旋转后都能返回正确的矩形吗?

Anyone knows how to fix my code so that it will return the correct Rectangle after every rotation?

谢谢.

public void rotateRectangles(BufferedImage bim,int width,int height,Rectangle rect){
   BufferedImage bim = new BufferedImage(height, width,BufferedImage.TYPE_INT_RGB);
   Graphics2D g2d = (Graphics2D) (bufferedImage.createGraphics());
   g2d.translate(bufferedImage.getHeight(),0); 

   //Get Rectangle for 90 degree image rotation. This always good.
   g2d.rotate(1.5708);
   Shape shape = g2d.getTransform().createTransformedShape(rect); 
   Rectangle rotatedRect = shape.getBounds(); 
   System.out.println("rotated rectangle at 90 degrees.  Point x="+rotatedRect.x+"  y="+rotatedRect.y);


   //Get Rectangle for 180 degree image rotation. Getting wrong rotatedRect.
   g2d.rotate(1.5708);
   shape = g2d.getTransform().createTransformedShape(rect); 
   rotatedRect = shape.getBounds(); 
   System.out.println("rotated rectangle at 180 degrees. Point x="+rotatedRect.x+"  y="+rotatedRect.y); 


  //Get Rectangle for 270 degree image rotation. Getting wrong rotatedRect.
   g2d.rotate(1.5708);
   shape = g2d.getTransform().createTransformedShape(rect); 
   rotatedRect = shape.getBounds(); 
   System.out.println("rotated rectangle at 270 degrees. Point x="+rotatedRect.x+"  y="+rotatedRect.y);


  //Get Rectangle for 360 degree image rotation.Getting wrong rotatedRect.
   g2d.rotate(1.5708);
   shape = g2d.getTransform().createTransformedShape(rect); 
   rotatedRect = shape.getBounds(); 
   System.out.println("rotated rectangle at 360 degrees. Point x="+rotatedRect.x+"  y="+rotatedRect.y);         

}

谢谢.

推荐答案

与其通过 g2d.rotate() 旋转图形上下文的仿射变换,不如考虑使用 createTransformedShape()> 如本示例中所建议.

Instead of rotating the graphics context's affine transform via g2d.rotate(), consider using createTransformedShape() as suggested in this example.

附录:特别注意示例 Polygon 的基线最初以原点为中心.因此,初始变换是围绕原点旋转.在您的情况下,您可以使用 rotate() 方法,包含一个锚点,这将是矩形的中心.

Addendum: Note in particular that the baseline of the example Polygon is initially centered on the origin. As a result, the initial transformation is rotation around the origin. In your case, you can use the rotate() method that includes an anchor point, which would be the center of your rectangle.

这篇关于使用 Swing 在图像上旋转矩形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆