简体   繁体   中英

Scaled “straight line” image jumps out of line

a have a problem with scaling images up. I have a png file that looks like this: raw png

The image is 8px * 8px and has some red straight lines on it. But when i draw this image with java and scale it up this happens: java image

And as you can barly see, the line is not exactly straight. It is always one pixel off and makes this kind of wavy shape. If the image gets drawn somewhere else on the frame the "waves" are somewhere else. The image is rotated 90° but I tested it without rotation and it was still there. Apart from this I do need rotated images.

Is there any way to fix this? I enabled text-antialiasing in the same Graphics2D object. Is there also some sort of anitaliasing for this?

Code

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;
}

When the program starts I load the image I rotate it in all 4 directions, so I don't have to do this over and over again while the program is running.

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

And then all I have to do is calling this get method and draw the image:

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

I actually found a way to avoid these weird pixels but this method makes the image a little bit blurry. Instead of using

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

you can simply use

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

which does basically the same thing but wehn I scale it up it uses this smooth making key.

I tried this and noticed that its not verry comfortable because it lags a lot.

So I just scale it up in the beginning when I also rotate the images.

As I said this method is a bit blurry but if there are no other ways avoiding this problem I have to get use of this. You almost can't see the blur, so this would be an option for me.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM