矩形内BufferedImage的渲染部分

我目前正在尝试仅绘制BufferedImage的一部分,该部分在飞行中处于Rectangle的范围内。图像被移动,因此矩形中图像的大小发生了变化。

视觉描绘:

矩形内BufferedImage的渲染部分

目前,这就是我所拥有的,并且在低分辨率图像下效果很好。但是如果我放大小地图,这将变得效率很低并且会导致滞后

private BufferedImage extractPixels() {
    int[] imagePixels = new int[scaledImage.getWidth() * scaledImage.getHeight()];
    scaledImage.getRGB(0,scaledImage.getWidth(),scaledImage.getHeight(),imagePixels,scaledImage.getWidth());
    int maxX = 0,maxY = 0;
    boolean first = false;
    for (int y = 0; y < scaledImage.getHeight(); y++) {
        for (int x = 0; x < scaledImage.getWidth(); x++) {
            int px = (int)(this.x + x);
            int py = (int)(this.y + y);
            if (viewingArea.contains(px,py)) {
                if (x > maxX) maxX = x;
                if (y > maxY) maxY = y;
                if (!first) {
                    imageX = x;
                    imageY = y;
                    first = true;
                }
            }
        }
    }
    int xCount = maxX - imageX;
    int yCount = maxY - imageY;
    if (imageX < 0 || imageX > scaledImage.getWidth() || imageX + xCount > scaledImage.getWidth()) return null;
    if (imageY < 0 || imageY > scaledImage.getHeight() || imageY + yCount > scaledImage.getHeight()) return null;
    return scaledImage.getSubimage(imageX,imageY,xCount,yCount);
}

在渲染循环中:

public void Render(PixelRenderer renderer) {
    BufferedImage image = extractPixels();
    if (image != null) renderer.renderImage(image,x + imageX,y + imageY);
}

是否有一种方法可以更有效地执行此操作,以便重新缩放对性能的影响较小?

andy_69 回答:矩形内BufferedImage的渲染部分

通过计算图像应从何处生成子图像,以及子图像的大小取决于主图像的位置来固定。

private BufferedImage extractPixels() {
    double xp = viewingArea.x - this.x;
    double yp = viewingArea.y - this.y;
    double iw = viewingArea.width;
    double ih = viewingArea.height;

    int rightBound = scaledImage.getWidth() - viewingArea.width;
    int bottomBound = scaledImage.getHeight() - viewingArea.height;

    if (xp < 0) {
        xp = 0;
        iw = viewingArea.width - (this.x - viewingArea.x);
        imageX = viewingArea.x + (viewingArea.width - (int)iw);
    } else if (xp >= 0 && xp < rightBound) {
        xp -= dx;
        iw -= dx;
        imageX = viewingArea.x;
    }
    if (xp >= rightBound) {
        iw = viewingArea.width - ((int)xp - (scaledImage.getWidth() - viewingArea.width));
        imageX = viewingArea.x;
    }

    if (yp < 0) {
        yp = 0;
        ih = viewingArea.height - (this.y - viewingArea.y);
        imageY = viewingArea.x + (viewingArea.height - (int)ih);
    } else if (yp >= 0 && yp < bottomBound) {
        yp -= dy;
        ih -= dy;
        imageY = viewingArea.y;
    }
    if (yp >= bottomBound) {
        ih = viewingArea.height - ((int)yp - (scaledImage.getHeight() - viewingArea.height));
        imageY = viewingArea.y;
    }

    if (iw < 0) iw = Math.abs(iw);
    if (ih < 0) ih = Math.abs(ih);
    if (xp < 0) xp = Math.abs(xp);
    if (yp < 0) yp = Math.abs(yp);

    BufferedImage result = new BufferedImage((int)iw,(int)ih,BufferedImage.TYPE_INT_RGB);

    try {
        result = scaledImage.getSubimage((int)xp,(int)yp,(int)iw,(int)ih);
    } catch (Exception e) {
        //TODO: Log to game engine something bad has happened
        e.printStackTrace();
    }

    return result;
}
本文链接:https://www.f2er.com/2575891.html

大家都在问