旋转图像,图像宽度和高度不均匀(Rotating image with uneven image width and height)

编程入门 行业动态 更新时间:2024-10-18 03:25:49
旋转图像,图像宽度和高度不均匀(Rotating image with uneven image width and height)

我试图旋转宽度和高度不均匀的图像说:890W 180H ...适用于(512X512)或(72x72)

下面的代码适用于高度和宽度相同的图像。 如果我在paintComponent()上旋转相同的图像将旋转正常,但它会导致图像平移问题。 有人可以为我提供一种旋转图像的替代方法,而无需在paintComponent()上使用'g.rotate()',或至少帮助我理解如何在宽度和高度不相等的图像上执行旋转

import java.awt.Dimension; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.awt.image.BufferedImage; import javax.swing.JPanel; public class ImageHolder extends JPanel implements MouseListener { BufferedImage image; int imageX = 0, imageY = 0; int parentWidth, parentHeight; public ImageHolder() {} public ImageHolder(BufferedImage image) { this.image = image; setSize(new Dimension(800, 600)); setPreferredSize(new Dimension(800, 600)); parentWidth = this.getWidth(); parentHeight = this.getHeight(); imageX = (this.getWidth() - image.getWidth())/2; imageY = (this.getHeight() - image.getHeight())/2; addMouseListener(this); } @Override protected void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2d = (Graphics2D) g; g2d.drawImage(image, imageX, imageY, (int) image.getWidth(), (int) image.getHeight(), null); } @Override public void mouseClicked(MouseEvent e) { rotate(Math.PI / 2); repaint(); } private void rotate(double d) { BufferedImage clonedImage =new BufferedImage((int) this.image.getWidth(), (int) this.image.getHeight(),this.image.getType()); Graphics2D g2d = clonedImage.createGraphics(); g2d.rotate(d, clonedImage.getWidth() / 2, clonedImage.getHeight() / 2); g2d.drawImage(this.image, null, 0, 0); g2d.dispose(); this.image = clonedImage; } @Override public void mousePressed(MouseEvent e) {} @Override public void mouseReleased(MouseEvent e) {} @Override public void mouseEntered(MouseEvent e) {} @Override public void mouseExited(MouseEvent e) {} }

I am trying to rotate an image having uneven width and height say : 890W 180H ... works fine for (512X512) or (72x72)

The Below Code works perfectly for an image with equal height and width. If i rotate the same on the paintComponent() the image would rotates fine, but it causes issue with image panning. Can someone provide me with an alternative way to rotate the image without using 'g.rotate()' on paintComponent(), or at-least help me to understand how rotation is performed on an image with unequal width and height

import java.awt.Dimension; import java.awt.Graphics; import java.awt.Graphics2D; import java.awt.event.MouseEvent; import java.awt.event.MouseListener; import java.awt.image.BufferedImage; import javax.swing.JPanel; public class ImageHolder extends JPanel implements MouseListener { BufferedImage image; int imageX = 0, imageY = 0; int parentWidth, parentHeight; public ImageHolder() {} public ImageHolder(BufferedImage image) { this.image = image; setSize(new Dimension(800, 600)); setPreferredSize(new Dimension(800, 600)); parentWidth = this.getWidth(); parentHeight = this.getHeight(); imageX = (this.getWidth() - image.getWidth())/2; imageY = (this.getHeight() - image.getHeight())/2; addMouseListener(this); } @Override protected void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2d = (Graphics2D) g; g2d.drawImage(image, imageX, imageY, (int) image.getWidth(), (int) image.getHeight(), null); } @Override public void mouseClicked(MouseEvent e) { rotate(Math.PI / 2); repaint(); } private void rotate(double d) { BufferedImage clonedImage =new BufferedImage((int) this.image.getWidth(), (int) this.image.getHeight(),this.image.getType()); Graphics2D g2d = clonedImage.createGraphics(); g2d.rotate(d, clonedImage.getWidth() / 2, clonedImage.getHeight() / 2); g2d.drawImage(this.image, null, 0, 0); g2d.dispose(); this.image = clonedImage; } @Override public void mousePressed(MouseEvent e) {} @Override public void mouseReleased(MouseEvent e) {} @Override public void mouseEntered(MouseEvent e) {} @Override public void mouseExited(MouseEvent e) {} }

最满意答案

您应该使用以下一系列命令来获得有效的转换:

g.translate(newCenterX, newCenterY); g.rotate(d); g.translate(-centerX, -centerY);

其中centerX/Y是原始中心的坐标, newCenterX/Y是新中心的坐标。 请注意,旋转90°会切换图像的宽度/高度,从而也会切换它们的中心X / Y坐标。 这意味着以下内容:

Rotation X Y 0° X Y 90° Y X 180° X Y 270° Y X 360° X Y

所以newCenterX/Y取决于d的值

You should use the following series of commands to get a working transform:

g.translate(newCenterX, newCenterY); g.rotate(d); g.translate(-centerX, -centerY);

where centerX/Y are the coordinates of the original center and newCenterX/Y are the coordinates of the new center. Note that rotating by 90° swaps the images width/height and thus also their centerX/Y coordinates. That means the following:

Rotation X Y 0° X Y 90° Y X 180° X Y 270° Y X 360° X Y

So the newCenterX/Y depend on the value of d

更多推荐

本文发布于:2023-07-26 00:23:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1268390.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:图像   宽度   不均匀   高度   Rotating

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!