java - 绘制图块的更有效方法?

时间:2017-11-25 13:47:11

标签: java

我的程序中有一个特定的tile对象。它由两个也可以翻转的图像组成。第一列瓦片(无论是从左侧还是右侧,它取决于'翻转'布尔值)必须是'bricks0'图像,而所有其他瓦片应该是'bricks1'。所以,这就像我想要的那样工作,我只是解释这一点,以便清楚地了解代码。

我的程序中有两个这样的对象(其中一个是翻转的,另一个不是),这个。因此,问题是当需要绘制更多图像时,它会破坏游戏的性能。例如,如果我降低这些对象的高度,或只渲染一个对象,则没有滞后。

那么,我如何更有效地完成这项工作呢?

@Override
public void render(Graphics g) {

    if(destroyed)
        return;

    //Initialises the height and width of a tile
    int tW = 0, tH = 0;

    BufferedImage img = bricks0;

    if(!flip) {
        for(int i = 0; i < w; i += tW) {

            //Sets the image to 'bricks1' if it's not the first column, else, uses the initialised type of the image 'bricks0'
            if(i != 0)
                img = bricks1;

            for(int j = 0; j < h; j += tH) {

                //Calculates the remaining width and height yet to be drawn
                int W = Math.abs(w - i), H = Math.abs(h - j);

                /*Sets the width and height of the image;
                 * The width of 'bricks0' is 53, while for 'bricks1' it's 64;
                 * Height is 64
                 */
                tW = img.getWidth();
                tH = img.getHeight();

                /* Calculates the width and height of the subimage that is cropped from the original;
                 * This is done to prevent an image getting drawn out of the object's bounds
                 */
                if(W > tW)
                    W = tW;
                if(H > tH)
                    H = tH;

                //Crops and draws the image
                img = img.getSubimage(0, 0, W, H);
                g.drawImage(img, this.x + i, this.y + j, null);

            }
        }
    }
    else {
        for(int i = w; i > 0; i -= tW) {

            //Sets the image to 'bricks1' if it's not the first column, else, uses the initialised type of the image 'bricks0'
            if(i != w)
                img = bricks1;

            for(int j = 0; j < h; j += tH) {

                //Calculates the remaining width and height yet to be drawn
                int W = i, H = Math.abs(h - j);

                /*Sets the width and height of the image;
                 * The width of 'bricks0' is 53, while for 'bricks1' it's 64;
                 * Height is 64
                 */
                tW = img.getWidth();
                tH = img.getHeight();

                /* Calculates the width and height of the subimage that is cropped from the original;
                 * This is done to prevent an image getting drawn out of the object's bounds
                 */
                int x = 0, y = 0;

                if(W > tW) {
                    W = tW;
                }
                else {
                    x = tW - W;
                }
                if(H > tH) {
                    H = tH;
                }

                //Crops and draws the image
                img = img.getSubimage(x, y, W, H);
                g.drawImage(img, this.x + i - tW, this.y + j, null);

            }
        }
    }
}

我目前正在为这两个对象绘制26张图片。 1列是'bricks0'图像,而第二列是每个列的'bricks1'。

编辑:好的,我已经减少了由getSubimage()裁剪图块的概率,但是在它必须剪裁它的情况下它仍然有点滞后..

0 个答案:

没有答案