Question about using Graphics2D

I’ve been writing my program using this.getBufferStrategy().getGraphics() but I was looking at all the cool stuff Graphics2D adds and want to use that now. How do I set it up to use Graphics2D instead? (there’s no getBufferStrategy().getGraphics2D() :’( )

IIRC you can always cast a Graphics to a Graphics2D. I have no idea why it’s done like that, but I’ve never seen it fail and every sample bit of code everywhere does it too.

correct, it’s always a graphics 2D:

public void paint(Graphics g) {
  // always works ...
  Graphics2D g2 = (Graphics2D)g;

}

IIRC, Java2D creates a Graphics2D object internally but casts it to Graphics for backwards compatibility.

In retrospect, there is really no reason why BufferStrategy even specified to return Graphics. When
the api was introduced there were no problems of backward compatibility, so it should have
returned Graphics2D.

Dmitri
Java2D Team