Java issue

I use Java to create a game, and I use TexturePaint for texture areas in the background. Performance works fine using java.awt.TexturePaint, however I want to have areas with integral rotation, so I tried to implement 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();
    }


}

The only problem is that there is huge success: the frame rate drops from a comfortable (limited) 60 frames per second to 3 frames per second. Also, if I implement this as a clean wrapper for TexturePaint β€” without creating a new transformation, just passing arguments to the TexturePaint 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();
    }
}

performed much worse than TexturePaint. Why, and is there a way around this?

+3
1

BufferedImage, . BufferedImage, .

, , . , texturepaint . , , , .

0

Source: https://habr.com/ru/post/1733213/


All Articles