When & Why parenthesis are used in declaring a variable.For example :
Graphics2D g2 = (Graphics2D)g;
Here why Graphics2D are put in Parenthesis and what is the use of these ().
When & Why parenthesis are used in declaring a variable.For example :
Graphics2D g2 = (Graphics2D)g;
Here why Graphics2D are put in Parenthesis and what is the use of these ().
This is a cast. You are casting the g object (of type graphics) to a more specific type of Graphics2D
As per the document:
This Graphics2D class extends the Graphics class to provide more sophisticated control over geometry, coordinate transformations, color management, and text layout. This is the fundamental class for rendering 2-dimensional shapes, text and images on the Java(tm) platform.
Basically you are telling your code to take the g object and treat it like a graphics2d object which has more features. You can do this because Graphics2D is a "subclass" of graphics.
It's used to cast from one type to another one.
Ex:
float floatingNumber = 5.2;
int integerNumber = (int) floatingNumber;
In this case, the (int) takes just the integer part from the floating point number stored on "floatingNumber". So integerNumber will now store the value 5, which is the integer part of floatingNumber.