Martijn said:
You typically construct a Behavior node in your app and let it wake up on a
mouse event.
Send your code for us to shoot at.
I know my code could be better-looking, but atm I just try to make it
run. All I have atm are 4*4 color cubes that should be replaces by some
single-colored boxes you can click at to shoot at them.
... here it is - and thx for your help
:
package battleships.gui3d;
import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GraphicsConfiguration;
import java.awt.event.ActionEvent;
import java.awt.event.KeyEvent;
import javax.media.j3d.BranchGroup;
import javax.media.j3d.Canvas3D;
import javax.media.j3d.Shape3D;
import javax.media.j3d.Transform3D;
import javax.media.j3d.TransformGroup;
import javax.swing.JFrame;
import javax.swing.JLabel;
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.universe.SimpleUniverse;
public class Simple3DGame extends JFrame {
JLabel _statusBarTextLabel = new JLabel("Game status ...");
public Simple3DGame() {
super("Battleships 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(600, 600);
setJMenuBar(menuBar);
setLayout(new BorderLayout());
add(BorderLayout.CENTER, create3DPanel());
add(BorderLayout.PAGE_END, createStatusBar());
}
public JPanel createStatusBar() {
JPanel panel = new JPanel();
panel.setLayout(new FlowLayout());
panel.add(_statusBarTextLabel);
return panel;
}
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);
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();
int MAX_CUBES_X = 4;
int MAX_CUBES_Y = 4;
for (int x = 0; x < MAX_CUBES_X; x++) {
for (int y = 0; y < MAX_CUBES_Y; y++) {
BranchGroup objGroup = new BranchGroup();
objRoot.addChild(objGroup);
Transform3D transform = new Transform3D();
float newX = (MAX_CUBES_X / -2) * 0.3f + 0.15f + (0.3f * x);
float newY = (MAX_CUBES_Y / -2) * 0.3f + 0.15f + (0.3f * y);
System.out.println(newX + " " + newY);
transform.setTranslation(new Vector3f(newX, newY, -3.0f));
TransformGroup objRotate = new TransformGroup(transform);
objRotate.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
objRotate.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
objRotate.setCapability(TransformGroup.ENABLE_PICK_REPORTING);
objGroup.addChild(objRotate);
Shape3D shape = new ColorCube(0.1);
objRotate.addChild(shape);
// XXX add behavior here?
// PickRotateBehavior pickRotate = new PickRotateBehavior(
// objGroup, canvas, shape.getBounds());
// objRotate.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);
}
}