【问题标题】:Rendering section of a BufferedImage within a Rectangle矩形内 BufferedImage 的渲染部分
【发布时间】:2020-03-26 12:30:32
【问题描述】:

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

视觉描绘:

目前,这就是我所拥有的,它适用于低分辨率图像。但是如果我放大小地图,这会变得非常低效并导致滞后

private BufferedImage extractPixels() {
    int[] imagePixels = new int[scaledImage.getWidth() * scaledImage.getHeight()];
    scaledImage.getRGB(0, 0, scaledImage.getWidth(), scaledImage.getHeight(), imagePixels,
            0, 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);
}

有没有办法更有效地做到这一点,从而减少重新缩放对性能的影响?

【问题讨论】:

    标签: java rendering game-engine bufferedimage rectangles


    【解决方案1】:

    通过计算图像的子图像应该从哪里来修复,以及子图像的大小取决于主图像的位置。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-01-12
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多