• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 828
  • Last Modified:

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.
  • 2
1 Solution
Just use type casting;

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

This will work...
get the graphics objects(say graphics).
cast it to graphics2D objects, i.e.,
Graphics2D graphics2d = (Graphics2D) graphics;
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....
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.
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.

Join & Write a Comment

Featured Post

Get your problem seen by more experts

Be seen. Boost your question’s priority for more expert views and faster solutions

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now