Java自定义Paint实现性能问题

发布于 2024-08-22 02:17:44 字数 1704 浏览 7 评论 0原文

我正在使用 Java 创建游戏,并使用 TexturePaint 来对背景区域进行纹理处理。使用 java.awt.TexturePaint 的性能很好,但是,我想要具有固有旋转的区域,因此我尝试实现一个名为 OrientedTexturePaint 的自定义 Paint:

public class OrientableTexturePaint implements Paint {

    private TexturePaint texture;
    private float orientation;

    public OrientableTexturePaint(TexturePaint paint, float orientation)
    {
        texture = paint;
        this.orientation = HelperMethods.clampRadians((float)Math.toRadians(orientation));
    }

    public PaintContext createContext(ColorModel cm, Rectangle deviceBounds, Rectangle2D userBounds, AffineTransform xform, RenderingHints hints) 
    {
        AffineTransform newTransform = (AffineTransform)xform.clone();
        newTransform.rotate(orientation);
        return texture.createContext(cm, deviceBounds, userBounds, newTransform, hints);
    }

    public int getTransparency() 
    {
        return texture.getTransparency();
    }


}

唯一的问题是,性能受到巨大影响:帧速率从舒适的(上限)60fps 至约 3fps。此外,如果我将其实现为TexturePaint的纯包装器 - 无需创建新的转换,只需将参数传递给TexturePaint的方法并返回TexturePaint返回的内容,我会得到相同的结果。

即:

public class MyTexturePaint implements Paint {

    private TexturePaint texture;

    public OrientableTexturePaint(TexturePaint paint, float orientation)
    {
        texture = paint;
    }

    public PaintContext createContext(ColorModel cm, Rectangle deviceBounds, Rectangle2D userBounds, AffineTransform xform, RenderingHints hints) 
    {
        return texture.createContext(cm, deviceBounds, userBounds, xform, hints);
    }

    public int getTransparency() 
    {
        return texture.getTransparency();
    }
}

性能比TexturePaint差很多。怎么会这样,有什么办法可以解决这个问题吗?

I'm using Java to create a game, and I'm using TexturePaint to texture areas in the background. Performance is fine using java.awt.TexturePaint, however, I want to have areas having an inherent rotation, so I tried implementing a custom Paint called OrientedTexturePaint:

public class OrientableTexturePaint implements Paint {

    private TexturePaint texture;
    private float orientation;

    public OrientableTexturePaint(TexturePaint paint, float orientation)
    {
        texture = paint;
        this.orientation = HelperMethods.clampRadians((float)Math.toRadians(orientation));
    }

    public PaintContext createContext(ColorModel cm, Rectangle deviceBounds, Rectangle2D userBounds, AffineTransform xform, RenderingHints hints) 
    {
        AffineTransform newTransform = (AffineTransform)xform.clone();
        newTransform.rotate(orientation);
        return texture.createContext(cm, deviceBounds, userBounds, newTransform, hints);
    }

    public int getTransparency() 
    {
        return texture.getTransparency();
    }


}

Only problem is, there's a huge performance hit: the frame rate drops from a comfortable (capped) 60fps to about 3fps. Furthermore, if I implement this as a pure wrapper to TexturePaint - without creating the new transform, simply passing the arguments to TexturePaint's methods and returning what TexturePaint returns, I get the same result.

i.e.:

public class MyTexturePaint implements Paint {

    private TexturePaint texture;

    public OrientableTexturePaint(TexturePaint paint, float orientation)
    {
        texture = paint;
    }

    public PaintContext createContext(ColorModel cm, Rectangle deviceBounds, Rectangle2D userBounds, AffineTransform xform, RenderingHints hints) 
    {
        return texture.createContext(cm, deviceBounds, userBounds, xform, hints);
    }

    public int getTransparency() 
    {
        return texture.getTransparency();
    }
}

performs massively worse than TexturePaint does. How come, and is there any way to get around this?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

-残月青衣踏尘吟 2024-08-29 02:17:44

我会将旋转的部分绘制到 BufferedImage,然后将其绘制到背景。然后,当旋转部分发生变化时,您只需更新 BufferedImage 即可。

如果你不这样创建缓存,如果每次都从头开始绘制,你就必须考虑绘制模型的管道以及它的复杂程度。性能是为了减少渲染管道中的瓶颈,并且通过纹理绘制过程传递所有内容听起来像是一个巨大的瓶颈。纹理要创建一次,经常使用,而不是经常创建,使用一次。

I would paint the rotated portion to a BufferedImage and then paint that to the background. Then you only have to update the BufferedImage when there is a change to the rotated portions.

If you don't create a cache this way, if you do the painting from scratch each time, you have to consider the pipeline of the paint model and how complex that is. Performance is about reducing the bottlenecks in the rendering pipeline, and passing everything through a texturepaint process sounds like a massive bottleneck. Textures are to be created once, used often, not created often, used once.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文