How to get "Graphics2D" object from a component?

 As you know, the "getGraphics()" method of a component
returns a "Graphics" object, but in many cases we need to
pass a "Graphics2D" object for "UI" processes.What is the
solution to this?
  I would be very gratefull if you send me a reply.
Thanks in Advance.
MohamadiAsked:
Who is Participating?
 
ragaConnect With a Mentor Commented:
get the graphics objects(say graphics).
cast it to graphics2D objects, i.e.,
Graphics2D graphics2d = (Graphics2D) graphics;
0
 
venkat2000120699Commented:
Just use type casting;

Graphics g = comp.getGraphics();
Graphics2D g2d = (Graphics2D) g;

This will work...
0
 
venkat2000120699Commented:
Hi raga,

How come your answer is different than that of mine?
Please look at the posted comments, before proposing an answer.... :-)  Hope u will not repeat it again....
0
 
MohamadiAuthor Commented:
Tank you for your kindness.
When I cast the object the "getFontRenderContex()" method
returns "null" value.
Is it because of casting?
Thanks in advance.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.