缩放 "straight line" 图像跳出线

Scaled "straight line" image jumps out of line

a 在放大图像时遇到问题。我有一个看起来像这样的 png 文件: raw png

图像是 8px * 8px,上面有一些红色的直线。 但是当我用 java 绘制此图像并将其放大时,会发生这种情况:java image

正如您几乎看不到的那样,这条线并不完全是直的。它总是偏离一个像素并形成这种波浪形。如果图像在框架的其他地方绘制,则 "waves" 在其他地方。图像旋转了 90°,但我在没有旋转的情况下对其进行了测试,它仍然存在。除此之外,我确实需要旋转图像。

有什么办法可以解决这个问题吗?我在同一个 Graphics2D 对象中启用了文本抗锯齿。是否也有某种抗锯齿处理?

代码

private void loadImage(String path, int field, int imageNumber) {
    BufferedImage image;
    image = new Resource().readImg(path);
    images[field][imageNumber][0] = image;
    images[field][imageNumber][1] = rotateClockwise90(image);
    images[field][imageNumber][2] = rotateClockwise90(rotateClockwise90(image));
    images[field][imageNumber][3] = rotateClockwise90(rotateClockwise90(rotateClockwise90(image)));
}

private BufferedImage rotateClockwise90(BufferedImage src) {
    int width = src.getWidth();
    int height = src.getHeight();

    BufferedImage dest = new BufferedImage(height, width, src.getType());

    Graphics2D graphics2D = dest.createGraphics();
    graphics2D.translate((height - width) / 2, (height - width) / 2);
    graphics2D.rotate(Math.PI / 2, height / 2, width / 2);
    graphics2D.drawRenderedImage(src, null);

    return dest;
}

当程序启动时,我加载图像并在所有 4 个方向上旋转它,因此我不必在程序运行时一遍又一遍地执行此操作 运行。

public BufferedImage getTile(int type, int part, int rotation) {
    return images[type][part][rotation];
}

然后我所要做的就是调用这个 get 方法并绘制图像:

g2d.drawImage(tiles.getShipTile(type, part, rotation), x, y, null);

我实际上找到了一种避免这些奇怪像素的方法,但这种方法会使图像有点模糊。 而不是使用

g2d.drawImage(img, x, y, width, height, null);

你可以简单地使用

g2d.drawImage(img.getScaledInstance(width, height, Image.SCALE_AREA_AVERAGING), x, y, null);

它做的基本上是一样的,但如果我放大它,它会使用这个 平滑制作 键。

我试过了,发现它不是很舒服,因为它滞后很多。

所以我只是在开始时将其放大,同时旋转图像。

正如我所说,这个方法有点模糊,但如果没有其他方法可以避免这个问题,我必须使用这个。你几乎看不到模糊,所以这对我来说是一个选择。