Canvas3D overlaps JMenu

A

Angus Parvis

Hi,

I'm trying to write "Battleships 3D" and I'm new to Java 3D. The whole
frame of the app is used to display the game. Additionaly there's a menu
to start a new game or exit it. Now obviously the Java 3D Canvas3D
overlaps the JMenu-items .. does anyone know how to fix that?

Thx for your help,

Angus


Here's the code:

import java.awt.BorderLayout;
import java.awt.GraphicsConfiguration;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;

import javax.media.j3d.BoundingSphere;
import javax.media.j3d.BranchGroup;
import javax.media.j3d.Canvas3D;
import javax.media.j3d.Transform3D;
import javax.media.j3d.TransformGroup;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JPanel;
import javax.swing.KeyStroke;
import javax.vecmath.Vector3f;

import com.sun.j3d.utils.geometry.ColorCube;
import com.sun.j3d.utils.picking.behaviors.PickRotateBehavior;
import com.sun.j3d.utils.universe.SimpleUniverse;

public class Simple3DGame extends JFrame {

public Simple3DGame() {
super("Schiffe versenken 3D");

JMenuBar menuBar = new JMenuBar();

JMenu menu = new JMenu("Game");
menu.setMnemonic(KeyEvent.VK_G);
menuBar.add(menu);

JMenuItem menuItem = new JMenuItem("New Game", KeyEvent.VK_N);
menu.add(menuItem);
menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_N,
ActionEvent.CTRL_MASK));
menuItem = new JMenuItem("Exit Game", KeyEvent.VK_X);
menu.add(menuItem);
menuItem.setAccelerator(KeyStroke.getKeyStroke(KeyEvent.VK_X,
ActionEvent.CTRL_MASK));

setSize(800, 600);

setJMenuBar(menuBar);
setLayout(new BorderLayout());
add(BorderLayout.CENTER, create3DPanel());
}

public JPanel create3DPanel() {
JPanel panel = new JPanel();
panel.setLayout(new BorderLayout());
GraphicsConfiguration config = SimpleUniverse
.getPreferredConfiguration();
Canvas3D canvas3D = new Canvas3D(config);
panel.add(BorderLayout.CENTER, canvas3D);

BranchGroup scene = createSceneGraph(canvas3D);
scene.compile();

// SimpleUniverse is a Convenience Utility class
SimpleUniverse simpleU = new SimpleUniverse(canvas3D);

// This moves the ViewPlatform back a bit so the
// objects in the scene can be viewed.
simpleU.getViewingPlatform().setNominalViewingTransform();

simpleU.addBranchGraph(scene);

return panel;
} // end of HelloJava3Da (constructor) public BranchGroup

public BranchGroup createSceneGraph(Canvas3D canvas) {
// Create the root of the branch graph
BranchGroup objRoot = new BranchGroup();

// create ColorCube and PickRotateBehavior objects
Transform3D transform = new Transform3D();
transform.setTranslation(new Vector3f(-0.6f, 0.0f, -0.6f));
TransformGroup objRotate = new TransformGroup(transform);
objRotate.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
objRotate.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
objRotate.setCapability(TransformGroup.ENABLE_PICK_REPORTING);

objRoot.addChild(objRotate);
objRotate.addChild(new ColorCube(0.4));

BoundingSphere behaveBounds = new BoundingSphere();
PickRotateBehavior pickRotate = new PickRotateBehavior(objRoot, canvas,
behaveBounds);
objRoot.addChild(pickRotate);

// Let Java 3D perform optimizations on this scene graph.
objRoot.compile();

return objRoot;
}

public static void main(String args[]) {
JFrame gameFrame = new Simple3DGame();
gameFrame.setVisible(true);
}
}
 
B

Bill Tschumy

Hi,

I'm trying to write "Battleships 3D" and I'm new to Java 3D. The whole
frame of the app is used to display the game. Additionaly there's a menu
to start a new game or exit it. Now obviously the Java 3D Canvas3D
overlaps the JMenu-items .. does anyone know how to fix that?

Thx for your help,

Angus

I'm surprised your code even runs. You seem to be adding the 3D panel
directly to the JFrame rather than to the Frame's content panel. At runtime
this should be throwing an exception.

Try changing the code to add it to the content panel (and setting the layout
manager on the content panel also).

Part of he problem is also related to the fact that Canvas3D is a
'heavyweight" component while JMenu is "lightweight". One recommended
solution it to add the Canvas3D to a JPane, which you are doing. You can
read more about the interaction of Canvas3D and Swing components at:
<http://java3d.j3d.org/tutorials/quick_fix/swing.html>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,053
Latest member
BrodieSola

Latest Threads

Top