Canvas3D overlaps JMenu

Discussion in 'Java' started by Angus Parvis, Jun 13, 2005.

  1. Angus Parvis

    Angus Parvis Guest

    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);
    }
    }
    Angus Parvis, Jun 13, 2005
    #1
    1. Advertising

  2. Angus Parvis

    Bill Tschumy Guest

    On Mon, 13 Jun 2005 12:43:55 -0500, Angus Parvis wrote
    (in article <6d38$42adc5da$3eb22065$>):

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

    --
    Bill Tschumy
    Otherwise -- Austin, TX
    http://www.otherwise.com
    Bill Tschumy, Jun 13, 2005
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ike
    Replies:
    4
    Views:
    946
  2. x2
    Replies:
    2
    Views:
    582
  3. Sam Brightman
    Replies:
    18
    Views:
    3,848
    Andrew Thompson
    Dec 10, 2003
  4. Chameleon

    JMenu with images

    Chameleon, Oct 25, 2005, in forum: Java
    Replies:
    5
    Views:
    1,189
    Thomas Fritsch
    Oct 27, 2005
  5. Martijn Mulder

    does GC eat my JMenu?

    Martijn Mulder, Dec 16, 2005, in forum: Java
    Replies:
    3
    Views:
    401
    Roedy Green
    Dec 17, 2005
Loading...

Share This Page