Here’s what I do for Java2D
At the beginning of my main render method, the Graphics object is sent to my Screen class.
After this, the game is rendered, which also renders all of the entities and tiles and things. Here’s what a typical render method looks like for an entity:
public void render() {
Screen.draw(x, y, 0, false, false);
}
Where the parameters are x, y, sprite, xFlip and yFlip.
My Screen.draw method:
public static void draw(int x, int y, int sprite, boolean flipX, boolean flipY) {
BufferedImage img = sheet.sprites[sprite];
if (flipX) {
AffineTransform tx = AffineTransform.getScaleInstance(-1, 1);
tx.translate(-img.getWidth(), 0);
AffineTransformOp op = new AffineTransformOp(tx, AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
img = op.filter(img, null);
}
if (flipY) {
AffineTransform tx = AffineTransform.getScaleInstance(1, -1);
tx.translate(0, -img.getHeight());
AffineTransformOp op = new AffineTransformOp(tx, AffineTransformOp.TYPE_NEAREST_NEIGHBOR);
img = op.filter(img, null);
}
draw2(img, x, y, img.getWidth(), img.getHeight());
}
public static void draw2(BufferedImage img, int x, int y, int width, int height) {
g.drawImage(img, x, y, width, height, null);
}
At the end of the rendering cycle, my main render method gets the Graphics object back from the Screen class.
This prevents having to pass around the Graphics object to all of my classes that will be rendering, and it lets me condense my rendering logic.
Hope this helps somehow.
| Nathan