Swing + Java3D = giant black rectangle

I’m having trouble with a large black rectangle when I introduce my java3d viewer for the first time. The rest of the game (I use the term loosely- it is an educational thing) uses normal Swing stuff and when I switch the view from a normal JPanel to a Canvas3D a large area of my screen fills with a black rectangle. This goes outside the limits of my application and does not clear up until I move or resize the application, forcing a repaint. Calling repaint does nothing, for some reason.

Anyone know what is causing this? I get a similar effect when I open two java3d applications but I am being very careful to only use one Canvas3d for everything I do here. Which isn’t to say I’m not creating two, but I don’t think I am.

So I got my debugger working and stepped through and it comes out on this line:

public Canvas3D View3D= new Canvas3D(SimpleUniverse.getPreferredConfiguration());

Which I’m sure I’ve used before without any ill-effects. Is it because I’m using more other stuff at the same time? Any good ways to work out what is wrong about the graphics configuration or tools I can use to see what is going on in more detail?

Try keeping the JPanel, and adding the Canvas3D to the JPanel instead of replacing it. Then the Canvas3D becomes a managed component and shouldn’t go outside the application bounds, and should resize properly, etc.

Also, the Canvas3D is a heavyweight component, so it will show up over any lightweight Swing components, including menubars, toolbars, etc. if they are in the same container. So ideally your Canvas3D should be the only component in the JPanel. Your other Swing components should be in a different JPanel. Then you can put both of the JPanels into another JPanel to organize them however you want, using LayoutManagers.

If you have popup menus and you want them to show up over the Canvas3D, then you can use:

JPopupMenu.setDefaultLightWeightPopupEnabled(false);

This is the odd thing, because it is actually being drawn on a JPanel anyway - and the Canvas3D is drawing correctly and in the right place, it is just that when it is initialized it draws this big old black rectangle over half my screen, a bit like this:


.AAAAAAA…
.AABBBBBBBBBBB…
.AABBBBBBBBBBB…
.AABBBBBBBBBBB…
…BBBBBBBBBBB…
…BBBBBBBBBBB…

Where A is my application, B is the black square and . is the background/other windows/etc.

I can force a repaint to get

.AAAAAAA…
.AAAAAAABBBBBB…
.AAAAAAABBBBBB…
.AAAAAAABBBBBB…
…BBBBBBBBBBB…
…BBBBBBBBBBB…


but the rest of the black area will stay until I redraw the underlying windows.

It looks like it is some kind of openGL initialization or something, but it seems to happen whether I use the OpenGL or D3D versions of J3D so that is probably not the case.

Oh, my mistake- it only happens when I’m using the OpenGL version of J3D. Looks like it’s some kind of bug or driver clash. Great.

OK, so this is now starting to really annoy me - it does still happen using the DX version, but only when a second Canvas3D is created. I’m fairly sure that the reason is still to do with my rather lame Graphics card drivers (rage 128) but it is a fairly standard piece of kit and I’m sure it should be able to handle this.

Basically, assuming this is the case, I need to have a way to use a single Canvas3D throughout my application without recreating it at any point. Is there a way to do this? I think all I need to do is remove everything from it, but it isn’t clear how to do this.

Hmmm… I am using 2 Canvas3Ds at the same time with no problem, using both OpenGL and DX versions, on an ATI Mobility card, on Linux and Windows, with no issues.

Try adding the Canvas3D to the JPanel before making the JPanel visible, if you are not already.

I will try and post my code that does similiar stuff, if I get a chance.

In the end I just kept the one canvas and removed everything from it when I’m done with it for now and then added new stuff, so I didn’t have to reallocate it at all. That way even if I do see the rectangle of doom, it only happens once, at the beginning.