我正在使用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更糟糕。怎么来,有什么方法可以解决这个问题吗?
答案 0 :(得分:0)
我会将旋转的部分绘制到BufferedImage,然后将其绘制到背景中。然后,只需在旋转部分发生更改时更新BufferedImage。
如果不以这种方式创建缓存,如果每次都从头开始绘制,则必须考虑绘制模型的管道以及它的复杂程度。性能是关于减少渲染管道中的瓶颈,并通过纹理绘制过程传递所有内容听起来像一个巨大的瓶颈。纹理将被创建一次,经常使用,不经常创建,使用一次。