【发布时间】:2020-01-30 19:49:18
【问题描述】:
我一直在尝试使用 for 循环来旋转图像。我的代码确实有效,但这种方法似乎没有必要,并且图像在旋转时会丢失像素。有没有更简单的方法来做到这一点?
//rotates image around center a degrees
public void drawRotatedImage(Graphics g, BufferedImage image, double a, int x, int y, int pixelSize) {
//origin point for rotation
int rX = image.getWidth() / 2;
int rY = image.getHeight() / 2;
for(int x1 = 0; x1 < image.getWidth(); x1++) {
for(int y1 = 0; y1 < image.getHeight(); y1++) {
int c = image.getRGB(x1, y1);
//rotating point
int nX = (int) (((x1-rX) * Math.cos(a)) - ((y1-rY) * Math.sin(a)));
int nY = (int) (((x1-rX) * Math.sin(a)) + ((y1-rY) * Math.cos(a)));
g.setColor(new Color(c));
//drawing each pixel
g.fillRect((nX*pixelSize) + x, (nY*pixelSize) + y, pixelSize, pixelSize);
}
}
}
【问题讨论】:
-
可能重复 Rotate a buffered image in Java。这不使用像素操作,而是使用
AffineTransform- 这个例子和其他答案之间的区别?它将调整图像的大小以使内容适合,这似乎是您所缺少/正在寻找的
标签: java for-loop methods graphics bufferedimage