• 12
• 12
• 9
• 10
• 13

# [java] Java3D in a panel

This topic is 3963 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I've recently decided to pick up Java3D for one of my projects (it's already written in Java, and will eventually be 3D...so why not?). In an attempt to make things a bit more flexible, I'm going for the modular approach: the game renders to a JPanel that can be added to either a JFrame or JApplet. When I add Java3D code to the JPanel, then add the panel to a JFrame, it doesn't render (or at least the window doesn't show anything). Overriding the paint(Graphics) method doesn't help either. However, if I change the JPanel to a JFrame and have Java3D render to said frame using the exact same Java3D code, it works (so far, I've just gotten the background to render, but that's probably another problem). Does Java3D have issues when rendering to non-top-level components (Frames, Applets, etc.), or am I missing something?

##### Share on other sites
Are you using a Canvas3D component? You may want to glance at this tutorial.

##### Share on other sites

A snippet of the panel class's constructor (which is really all there is to it):

[source lang=java]  public GamePanel3D()  {    super();    GraphicsConfiguration gc = SimpleUniverse.getPreferredConfiguration();    Canvas3D c = new Canvas3D(gc);    add(c);    this.vu = new SimpleUniverse(c);    // Set up scene    BranchGroup sceneBG = new BranchGroup();    // ...    sceneBG.compile();    vu.addBranchGraph(sceneBG);  }

I've found that page before, but most of it seems concerned with J3D drawing on top of other components (a problem I don't see appearing in my design, as I don't plan to have components overlapping the rendering panel). I even copy/pasted the code from a working single-class example into my panel, and it wouldn't render. Once I changed to a JFrame, however, it worked (as previously described).

I don't know if it will help (could simply be another symptom of the problem), but when I run a Java3D app, I get an initial warning message stating that I have OpenGL v1.2, and require OpenGL v1.3, but that it will continue anyway (something about buggy ATI drivers). A "working" app (one that renders) will also give me a "JAVA 3D: OpenGL 1.2 detected; will run with reduced functionality" message sometime later, while a non-working app never does. Since the examples function properly anyway, I doubt the error is actually the cause (and I can't find any info on upgrading OpenGL on Linux).

If needed, I could probably stick with JFrames; I'm not sure if my project will be suitable for an applet or not anyway - I just like keeping the option on the table so I don't have to end up resorting to ugly hacks to make it work.

##### Share on other sites
I don't see any code in your snippet where you set the layout of the panel or set it's size before you add the canvas. You don't show the code where the panel is added to the frame, but it may simply be the canvas failing to size properly.

##### Share on other sites
It seems that solved the problem; once I passed a BorderLayout to the Panel's constructor, the canvas appeared just like it should (well, just like it did rendering directly to the frame - I still have another bug or two to stomp). Interestingly, none of the other LayoutManagers seem to work (though I certainly could be misusing them; I don't use them much).

In short, I changed this:
  public GamePanel3D()  {    super();    GraphicsConfiguration gc = SimpleUniverse.getPreferredConfiguration();    ...  }
to this:
  public GamePanel3D()  {    super(new BorderLayout());    GraphicsConfiguration gc = SimpleUniverse.getPreferredConfiguration();    ...  }

and the Canvas3D appears. Does this mean that I *need* a LayoutManager, or is it just the simplest solution to the sizing problem? Having one in place isn't a problem, I've just never run into a situation where one was required (even if they could make the situation easier).

EDIT: Nevermind. I did a bit more looking and found Canvas3D.setSize(). Since I had never called setSize() on it (and had no LayoutManager to do it for me), the canvas never had a size. Now I feel like a fool; for some reason, I assumed Canvas3D would simply take on the size of whatever container it was placed in.

Regardless, thanks for your time and help.

[Edited by - dragonknightx on May 15, 2007 1:54:05 PM]