Using Graphics2D better than using just Graphics?

Hi!

I was wondering if using a Graphics2D object instead of a Graphics object gives you better performance? If I for example casted the Graphics object received from my buffer strategy would drawing with it give me better results? If I used methods inherited from the Graphics class? If I use the new methods in Graphics2D? If I use the drawImage(BufferedImage) in Graphics2D with a BufferedImage or if I use drawImage(Image) in Graphics with the same image?

Best Regards,

Johan Tibell

all Graphics objects returned from awt related objects are infact Graphics2D objects.

Using a method defined in Graphics will behave identically whether it is done through a Graphics reference or a Graphics2D reference.

This is fundamental to the concepts of polymorphism and inheritance.

Isn’t the choice on which class to use for resolving the method call based on the type of the object, not by the type of the reference? This is what I was taught if I remember correctly… :-/

How about the two different versions of drawImage? Is one of the faster?

Best Regards,

Johan Tibell

Theres no hard and fast rules when choosing between the two, but in general i think you’ll find that the Grahics2D methods are more general purpose and give greater flexibility (addition of AffineTransform for example), and almost certainly this comes at a cost to speed.

Then again, if you need the functionality, odds are you’re not going to be able to code a faster version yourself.

Also, your example of drawImage(Image) vs. drawImage(BufferedImage) doesn’t make any sense - if you’re using a buffered image in the first place it’ll be identical, if you’re using a volative image then its not a direct replacement.

In short, use what you need and benchmark it if you’re not sure.