Reputation: 761
Graphics2D g2 = (Graphics2D) g;
Honestly, the entire g2 and g concept is confusing. Can anyone explain it? Thanks.
Additionally, an explanation of the difference between g and g2 would be nice.
Upvotes: 0
Views: 70
Reputation: 5235
You cast a Graphics
object called g
in Graphics2d
.
It is an example of polymorphism.
Basically the graphics 2d extends the graphics class in awt. Graphics itself is an abstract class, so it can't be created, it only defines some interface and some functionality.
Maybe the oracle API could help you:
http://docs.oracle.com/javase/7/docs/api/java/awt/Graphics.html http://docs.oracle.com/javase/7/docs/api/java/awt/Graphics2D.html
Upvotes: 0
Reputation: 347184
Presumably, g
is a Graphics
instance, Graphics2D g2 = (Graphics2D) g;
is casting g
to an instance of Graphics2D
.
This is an example of Polymorphism in action...
Around Java 1.3/1.4 the new Graphics2D
API was introduced into the Java API, but because most of the paint methods required Graphics
, they weren't updated (for backwards compatibility). The developers however basically guaranteed that any system generated Graphics
passed to these paint methods would get an instance of a Graphics2D
context
Basically, Graphics2D
is an extension to the Graphics
API
Have a look at Painting in AWT and Swing and Performing Custom Painting for more details
Upvotes: 4