Context for Graphics

Is there a way I can get the reference for graphics for a component. The only function where we get the graphics object is paint()/paintoverchildren(). Are there any function which will return this graphic object.

No, because it only exists during those calls. Perhaps if you explain why you want it, we could help.

Most of the time, what you’d probably want to do is give your Component an Image; you can create a Graphics context for that yourself at any time, and then you can draw the contents of the Image to the Component when it comes round to painting it.


Heres what I want to do.

I have a component within a parent component. Now on this child component, I want to draw a circle of say 3 pixel radius at the four corners, corners being the center for the circles. What happens is only 1/4th of the circle is visible. The remaining 3/4th is not, as it goes beyond the bounds of this child component.

What I was thinking as a workaround was, get the screen coordinates and use the parent component to draw these circles so that they are fully visible wherever they are dragged within the child component(even on the edges and corners). Note that, these points are draggable. So, its not that they will always be at corners.

Your thinking seems a little muddled… You can’t just go around randomly drawing all over the place, I’m afraid!

The way to approach something like this is not to think about what shapes you want to draw, but think in terms of objects and their function. You need to create custom components for your circles (they’re draggable handles of some kind, I guess?), and position them where they’re needed, as siblings of the component they’re attached to.

Don’t be afraid to break your UI down into lots of simpler components, it’s a common beginner’s mistake to have one big component class, and try to make it do everything.