仅用不同颜色替换旋转图像的角

Replace only the corners of a rotated image with a different color

我目前正在制作一款需要旋转图像的游戏。为了旋转它,我使用了以下代码。

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(透明)。

唯一的问题是,我不能简单地遍历图像并替换 黑色像素,因为原始图像中还有其他黑色像素。所以我的问题是,如何只替换旋转产生的黑色像素。

(对不起,我不能提供图片的例子,我不知道如何用这台电脑截图。)

如果我没理解错的话,你有以下轮换:

绿色单元格是旋转后的原始图像,而白色单元格是要删除的区域。由旋转和给定的度数,可以知道红色单元格的坐标,从而删除满足条件的单元格:

(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 */

希望对您有所帮助!

The graphics object has no context to color these new pixels, so it simply colors them black.

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(...);