Graphics vs. Graphics2D

What’s the difference between Graphics and Graphics2D? As far as I’ve been able to tell, then the only way to get Graphics2D objects is to cast from Graphics objects. ^^ (I’ll read the docs closer later, but for now, what’s the real difference? :s )

Graphics is older. Graphics2D was introduced later on. Now every Graphics instance is actually a Graphics2D instance, but parameters are kept to Graphics for backwards compatibility :slight_smile:

Links to both are conveniently located in the 2D Graphics tutorial.
http://docs.oracle.com/javase/tutorial/2d/overview/index.html

JavaDoc:
http://docs.oracle.com/javase/7/docs/api/java/awt/Graphics.html
http://docs.oracle.com/javase/7/docs/api/java/awt/Graphics2D.html
(can see how Graphics2D extends Graphics, lots more info here)

r4king, that’s what I figured. ^^ So should I just stick with using Graphics2D instead of Graphics? Or does it at all matter?

philfrei, yeah, I know. I already looked at those. And as far as I could tell there weren’t any real difference between them.

Graphics2D has more advanced methods like setting the stroke, the compositor (for blending), the affine transform (rotate, translate, shear, etc…) and more. So yes, always use Graphics2D :slight_smile:

Since you’re actually passed a Graphics2D no matter what, the only time to choose “which one you use” would be whether you need to call methods defined on Graphics2D but not Graphics. Casting to Graphics2D just to call fillRect() and drawImage() will have absolutely no impact on performance.

Graphics… then Graphics2D… I wonder how would the “next version” be named :slight_smile: