我试图旋转宽度和高度不均匀的图像说: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 YSo the newCenterX/Y depend on the value of d
更多推荐
发布评论