Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Canvas3D overlaps JMenu

Reply
Thread Tools

Canvas3D overlaps JMenu

 
 
Angus Parvis
Guest
Posts: n/a
 
      06-13-2005
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.PickRotateBeha vior;
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(Key Event.VK_N,
ActionEvent.CTRL_MASK));
menuItem = new JMenuItem("Exit Game", KeyEvent.VK_X);
menu.add(menuItem);
menuItem.setAccelerator(KeyStroke.getKeyStroke(Key Event.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().setNominalViewingTran sform();

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_TRANS FORM_WRITE);
objRotate.setCapability(TransformGroup.ALLOW_TRANS FORM_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);
}
}
 
Reply With Quote
 
 
 
 
Bill Tschumy
Guest
Posts: n/a
 
      06-13-2005
On Mon, 13 Jun 2005 12:43:55 -0500, Angus Parvis wrote
(in article <6d38$42adc5da$3eb22065$(E-Mail Removed)>):

> 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
does GC eat my JMenu? Martijn Mulder Java 3 12-17-2005 03:12 AM
JMenu with images Chameleon Java 5 10-26-2005 11:24 PM
JMenu/JPopupMenu disappear on mouseEntered? Sam Brightman Java 18 12-10-2003 02:26 AM
Canvas3D inside JSplitPane x2 Java 2 11-30-2003 01:07 AM
JToolBar/JMenu interaction Ike Java 4 08-16-2003 08:31 AM



Advertisments