Jump to content
  • Advertisement
Sign in to follow this  
dragonknightx

[java] Java3D in a panel

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
Advertisement
Thanks for the quick reply.

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 this post


Link to post
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 this post


Link to post
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]

Share this post


Link to post
Share on other sites
Well, the default LayoutManager is FlowLayout, which just pushes the components in one after another and let's each component have its preferred size. Since no preferred size was set and the canvas was empty, nothing would be visible. Border layout positions components and resizes them to fit the region specified.

You certainly don't have to use the BorderLayout, but you may have to experiment with setBounds(), setPreferredSize(), etc. to get things to size correctly. Alternately, you can setLayout(null) and specify the size and position of all components with setBounds(), but that can lead to headaches of it's own. The layout managers can be tricky to get used to, but they do provide a lot of functionality once you get them down.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!