tags:

views:

98

answers:

0

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?