Painting Custom Component

I am writing a custom Component inheriting from JComponent class. I am doing custom painting of this component as the component I want has to have a gradient background. I am able to draw the graphics properly. But I am having a problem with the positioning of the control. I read in the tutorial for the custom painting of JComponent that we should paint the component taking the base as 0. If I place my component any where on the panel other than (0,0) the component either gets displayed at 0,0 or is not getting displayed properly.Kindly provide me with a solution.

[577 byte] By [dan_aravinda] at [2007-10-3 4:15:26]
# 1

Painting of the component and positioning of the component are two different things.

Yes painting is done relative to the top left of the component which is (0, 0);

Positioning of the component in a container is done by the Layout Manager. So you need to provide a getPreferredSize(...) implementation so the LayoutManager knows how much space to reserve for the component.

Maybe the Swing tutorial on [url http://java.sun.com/docs/books/tutorial/uiswing/14painting/index.html]Custom Painting[/url] will help you out. There is also a section on "Using Layout Manager".

If you need further help then you need to create a [url http://homepage1.nifty.com/algafield/sscce.html]Short, Self Contained, Compilable and Executable, Example Program[/url] that demonstrates the incorrect behaviour, because I can't guess exactly what you are doing based on the information provided.

And don't forget to use the [url http://forum.java.sun.com/help.jspa?sec=formatting]Code Formatting Tags[/url] so the code retains its original formatting.

camickra at 2007-7-14 22:16:55 > top of Java-index,Desktop,Core GUI APIs...