Skip to main content

Help Mixing Custom Heavyweight with Swing

4 replies [Last post]
swpalmer
Offline
Joined: 2003-06-10

Hi there. �I'm hoping that there are a few folks around that have worked with heavyweights that use native rendering and Swing.. (maybe people familiar with how Java3D is implemented). So if I'm lucky they might be able to help me here.
�
I have created a custom component that uses native code to paint... actually, I don't even care so much about the 'paint()' method, as I have a native thread doing active rendering to the component. �The component is a live video 'window' which is being pushed frames from custom video capture hardware. �All of this is running on Windows XP or Win2k.
�
I have implemented the drawing code and my NativeVideoPreview component seems to be working fine from the point of view of the native drawing code. �I keep a global reference to my object that extends Canvas in my native code and use standard JAWT calls to get the drawing surface etc.. then ultimately call the Win32 function StretchDIBits to render to the HDC that I get from the JAWT_DrawingSurfaceInfo.
�
Questions:
I'm currently only using the hdc member of jawt_Win32DrawingSurfaceInfo, but how do I know which interpretation of the union of { HWND, HBITMAP, void*pbits } is valid?
It seems that since that union holds either a native window, DDB, or DIB that I can somehow use the JAWT stuff on something that isn't a heavyweight. �It seems that I could be asked to draw on some sort of backbuffer is that possible?
�
As I said, my drawing code is working, so I'm only interested in the above if it allows me to avoid the heavyweight and all the problems that it seems to be causing.
�
The Main Problem:
I have read the article at http://java.sun.com/products/jfc/tsc/articles/mixing/ on mixing heavy and light components. �I have set my popups to not be lightweight so menus and such work okay. �My problem is that although my heavyweight component never needs to be on top of anything, it does need to be clipped or even resized to a rectangular area that fits within my GUI where I want it. �That doesn't seem to be working properly.
�
I have the component in the top half of a JSplitPane, yet it insists on drawing past the splitter bar and over top of the lightweights that are in the bottom half.
�
I tried putting it in an AWT ScrollPane - something I would probably do eventually anyway, but it still does not render properly, the ScrollPane (or it's viewport) is much too small so the scroll bars are shown when they shouldn't be needed. �The top half of my JSplitPane is a JPanel with a GridBagLayout that contains only my heavyweight or the ScrollPane wrapping it. �The GridBagLayout constraints are set to let that component fill the available space (but it doesn't).
�
But that isn't the worst of my problems... it seems that ScrollPane or not, the heavyweight will grow vertically to go into the bottom half of the JSplitPane. �I can make it grow by SHRINKing the top pane. �As I drag the splitter bar up to the point where I start to squeeze the heavyweight (and would hope that the ScrollPane gets smaller, OR it is clipped to a smaller rectangle) it actually starts to get bigger vertically the smaller the vertical space I give it. �In fact even when I start to increase the size of the top pane if it still has less vertical space than the (now taller) heavyweight the heavyweight will grow more every time the splitter bar is moved.... �The end result is a heavyweight that goes from the top off the top pane all the way to the bottom of my apps window.. tromping on top of everything. �(I can still see my live video drawing at the top of this component.)
�
If I continue to play with the splitter bar the heavyweight eventually disappears completely and never comes back.
�
I'm stumped.
�
Oh, yeah, I forgot to say that the top half of the split pane has the JPanel that I put the heavyweight in, and an entirely different GUI on another card of a CardLayout.. So it is JSplitPane->JPanel(CardLayout)->JPanel(GridBagLayout)->Heavyweight (direct or in AWT ScrollPane).
�
Any ideas?
�
If I can get away for the heavyweight entirely I would be willing, as I expect the sizing problems will vanish, although being heavyweight I could maybe use DirectX or OpenGL to render my YUV data without needing to software convert to RGB as I am doing now.
�
Totally unrelated comment:
Couldn't Z-order with heavyweights be emulated to a degree �by setting the clipping of the heavyweight to a complex region? �You can make top level windows odd shapes this way, so I would think it would be possible to "cut out" the areas that are supposed to have lightweights on top so they simply show through.

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
mthornton
Offline
Joined: 2003-06-10

I think JSplitPane and certainly JScrollPane don't try to change the size of the components placed in them but just attempt to manipulate the transform and clip rectangle of the Graphics object passed to them when drawing ... except this doesn't work for heavyweights (which create their own) and certainly not if you are drawing to the panel from a separate (native) thread.
Try tracing the size and location of the Panel to see if it corresponds with the visual effect.

You might try extending JViewport (in particular the setExtentSize and setViewPosition methods) and use that as a container for your Panel. setExtensSize should change the size of your panel and setViewPosition will need to pass on that information to your native drawing code.

swpalmer
Offline
Joined: 2003-06-10

Thanks for the response...

Is there a reason that the HDC available via JAWT wouldn't have the appropriate clipping applied before it was returned to the native code?

I'm using the heavyweight AWT ScrollPane (not JScrollPane), but I do have the JSplitPane.. The thing is the size of the heavyweight IS changing (in the wrong direction!) as I adjust the JScrollPane and it is in the situation where it would need to clip.

I don't think I can safely use a JViewport to contain a heavyweight, can I?

You've given me some ideas... at the least I can gather more information to find out how the size is getting messed up.

**After some experiments***

It is clear that setBounds is being called on the ScrollPane with height values that increase each time the splitter bar is adjusted as long as the space available in the top half of the split pane is smaller than its current height. That seems quite odd, possibly a bug in awt/swing?

Message was edited by: swpalmer

swpalmer
Offline
Joined: 2003-06-10

I have worked around the odd resizing problem by using a null layout manager for the JPanel containing the Scrollpane with my heavyweight.. then overriding setBounds on the JPanel to make appropriate adjustments via setBounds on the ScrollPane. In other words I'm basically substituting my own mini layout manager to place and constrain the ScrollPane to reasonable values. The ScrollPane handles the clipping of my heavyweight appropriately.. so it seems "good enough" for now.

mthornton
Offline
Joined: 2003-06-10

I'm glad you got it working.