EDIT: this is an edited version of the original post since that was probably a bit too much at once... i'll try to keep it a bit simpler now to hopefully get an answer... dear javaranch community,
i have found this place a valueable source of information then and when and finally signed up to discuss an issue i'm not really sure about.
i have the OpenJDK Runtime Environment (build 1.6.0_0-b11) installed, my OS is Ubuntu-Linux.
the scenario is as follows:
i want to display a number of lines and some filled rectangles on a panel. all of these objects can possibly overlap and the z-order (or stacking order) is important.
i found that when i draw onto a normal JPanel, its not entirely reliable that the last painted object is also displayed on top so i found JLayeredPane might be useful for what i am trying to achieve. since i wan't full control over where my lines and rectangles appear, i use a null-layout (no layout-manager).
it took some time until i found out, that my custom JComponents won't get painted unless i call setSize() with values > 0 before.
further, when i set my custom JComponent to lets say a size of 50x50, it seems i can only use draw commands within the area of (0,0) and (50,50), everything outside this area won't get displayed.
so unless line 29 on SimpleLine is commented in, no lines are visible and since there is no output on the console, paintComponent() is probably never called.
:?: can anybody please tell me whats happening here or confirm this behaviour?
here is a code example, the controlling class: and my JComponent: note that both classes are in a package called 'custJComponentDrawingSimple', so to run the code, you'd need to put the two files into a folder named like the package and call 'javac custJComponentDrawingSimple/*.java' to compile and 'java custJComponentDrawingSimple.CJCD' to run the compiled code.
oh right, i could of course set the size of the component to the visible area of the parent container, but i noticed that this seriously impacts the drawing performance of my application. i guess that is because the whole area is considered as dirty region and redrawn on the next update no matter if necessary or not. and i guess its not quite the way this was meant to be used...
well, i finally found the reason why these issues were happening the way i described scattered over some threads on sun-forums:
it has to do with the null-layout manager. with a layout manager, one would use setPreferredSize and set the components size. the layout manager then would determine the display-size according to its rules and call setSize as well as the location and call setLocation on that component.
when java2d finally renders the component it relies on the values set through these methods.
if there is no layout-manager (and this is what null-layout means), the size and location is not set, so the default values of (0,0) for components and (10,10) for JPanels are used!
in this case, calling setPreferredSize() would of course have no effect because there is nobody who takes care of the preferred values.
oh right, note that setBounds( ... ) does the same as calling setSize and setLocation, just within one single call.
one more think if ever someone may stumble over this thread trying to do the same:
this was the entirely wrong approach for what i wanted to do and its rendering is far too slow!
i luckily came across a wonderful book that covers plenty java-graphics issues in-depth,
the title sounds not like it would be very helpful but in fact it turned out as a great resource.
my solution in short: don't misuse custom jcomponents as graphical objects put into a jlayeredpanel,
use bufferedImages and draw them to a bufferedimage instead, but make sure to get ito java2d and the like.
(one more time: this book helped me a lot with this!)
greez - alex
I'm THIS CLOSE to ruling the world! Right after reading this tiny ad: