我目前正在制作一款需要旋转图像的游戏。为了旋转它,我使用以下代码。
public ManipulableImage rotate(double degrees){
BufferedImage rotatedImage = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_RGB);
Graphics2D g = rotatedImage.createGraphics();
g.rotate(Math.toRadians(degrees), image.getWidth()/2, image.getHeight()/2);
/*
ManipulableImage is a custom class that makes it easier to manipulate
an image code wise.
*/
g.drawImage(image, 0, 0, null);
return new ManipulableImage(rotatedImage, true).replace(0, -1);
}
代码会旋转图像,但会使角落变黑,应该是透明的。我的渲染器将rgb值-1识别为透明值,并且在该值存在时不会更改像素。所以,我想将角的rgb值从0(黑色)更改为-1(透明)。
唯一的问题是,我不能简单地遍历图像并替换 黑色像素,因为原始图像中还有其他像素为黑色。所以我的问题是,如何仅替换旋转创建的黑色像素。
(抱歉,我无法提供图片示例,我不确定如何使用此计算机进行屏幕截图。)
答案 0 :(得分:1)
图形对象没有为这些新像素着色的上下文,因此它只是将它们涂成黑色。
BufferedImage rotatedImage = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_RGB);
您应该使用以下内容,以便BufferedImage
支持透明度:
BufferedImage.TYPE_INT_ARGB
然后在绘画代码中你可以使用:
g.setColor( new Color(0, 0, 0, 0) );
g.fillRect(0, 0, image.getWidth(), image.getHeight());
g.rotate(...);
g.drawImage(...);
答案 1 :(得分:0)
如果我理解正确,您可以进行以下轮换:
绿色单元格是旋转的原始图像,而白色单元格是要删除的区域。从旋转和给定的度数,您可以知道红细胞的坐标,从而删除符合条件的单元格:
(x_coord <= x1 and y_coord > x_coord * y1 / x1) /* Top Left */ or
(x_coord >= x2 and y_coord > x_coord * y2 / x2) /* Top Right */ or
(x_coord >= x3 and y_coord < x_coord * y3 / x3) /* Bottom Right */ or
(x_coord <= x4 and y_coord < x_coord * y4 / x4) /* Bottom Left */
希望这有帮助!