You are casting Graphics2D
to the Graphics
context g
. Read more about casting here in Inheritance in the Casting section.
What this ultimately does is allot you use the available methods of Graphics2D
with the Graphics
context of the passed to the paintComponent
method. Whitout the casting, you'd only be limited to the methods of the Graphics
class
Graphics2D
is a subclass of Graphics
so by using Graphics2D
you gain all the methods of Graphics
while taking advantage of the methods in the Graphics2D
class.
Side Notes
You shouldn't be override
paint
. Also if you are, you shouldn't be painting on top level containers likeJApplet
.Instead paint on a
JPanel
orJComponent
and overridepaintComponent
instead ofpaint
and callsuper.paintComponent
. Then just add theJPanel
to the parent container.public DrawPanel extends JPanel { @Override protected void paintComponent(Graphics g) { super.paintComponent(g); Graphics2D g2 = (Graphics2D)g; } }
See more at Custom Painting and Graphics2D