I’ve modified the code and I don’ t have the exception anymore. But, when i press the button several times :
com.sun.j3d.utils.geometry.Sphere@1df354
action ajout
javax.media.j3d.BranchGroup@5225a7
com.sun.j3d.utils.geometry.Sphere@2da3d
action ajout
javax.media.j3d.BranchGroup@45f743
Which seems to be normal.
look at the code hereafter (this is the complete class). The sphere is still not seen, but i’ve put some attributes to it.
import java.awt.;
import java.awt.GraphicsConfiguration;
import java.awt.event.;
import com.sun.j3d.utils.geometry.;
import com.sun.j3d.utils.universe.;
import com.sun.j3d.utils.behaviors.mouse.;
import com.sun.j3d.utils.behaviors.keyboard.;
import javax.media.j3d.;
import javax.vecmath.;
import javax.swing.*;
public class Scene3Db extends JFrame implements ActionListener, MouseListener {
/**
*
*/
private static final long serialVersionUID = 1L;
private SimpleUniverse su;
private BranchGroup sceneBG, mainBG, shapeBG, objRoot;
private TransformGroup mainTG, shapeTG, cameraTG;
private BoundingSphere bounds;
private Material material2;
private static int mycount = 1;
//declaration des elements du gui
private JLabel etiq1;
private JCheckBox couleur;
//constructeur
public Scene3Db() {
Container contentPane = getContentPane();
//BorderLayout bl = new BorderLayout();
//setLayout(bl);
GraphicsConfiguration config = SimpleUniverse.getPreferredConfiguration();
Canvas3D c = new Canvas3D(config);
contentPane.add("Center", c);
JPanel p1 = new JPanel();
p1.add(presentationPanel());
contentPane.add("North", p1);
JPanel p2 = new JPanel();
JButton ajout = new JButton("Ajout");
addObject addObjBG = new addObject();
ajout.addActionListener(addObjBG);
//objRoot.addChild(addObjBG); //pb !
p2.add(ajout);
contentPane.add("South", p2);
SimpleUniverse su = new SimpleUniverse(c);
BranchGroup scene = createSceneGraph(su);
//scene.setCapability( BranchGroup.ALLOW_BOUNDS_READ );
su.getViewingPlatform().setNominalViewingTransform();
su.addBranchGraph(scene);
addWindowListener( new WindowAdapter() {
public void windowClosing(WindowEvent e) {System.exit(0);}});
setSize(800,600); //Taille Fenetre
show();
repaint();
}
//création du graphe de scène
public BranchGroup createSceneGraph(SimpleUniverse su) {
//création d'un BG racine de l'arbre 3D
BranchGroup objRoot = new BranchGroup();
objRoot.setCapability( BranchGroup.ALLOW_PICKABLE_READ );
objRoot.setCapability( BranchGroup.ALLOW_PICKABLE_WRITE );
objRoot.setCapability(BranchGroup.ALLOW_CHILDREN_EXTEND);
objRoot.setCapability(BranchGroup.ALLOW_DETACH);
objRoot.setCapability(Group.ALLOW_CHILDREN_READ);
objRoot.setCapability(Group.ALLOW_CHILDREN_WRITE);
objRoot.setCapability(Group.ALLOW_CHILDREN_EXTEND);
//création d'un bounds pour le fond et la lumière
BoundingSphere bounds =
new BoundingSphere(new Point3d(0.0,0.0,0.0), 100.0);
Color3f bgColor = new Color3f(0.05f, 0.05f, 0.2f);
Background bg = new Background(bgColor);
bg.setApplicationBounds(bounds);
objRoot.addChild(bg);
Color3f light1Color = new Color3f(1.0f, 1.0f, 0.9f);
Vector3f light1Direction = new Vector3f(4.0f, -7.0f, -12.0f);
DirectionalLight light1
= new DirectionalLight(light1Color, light1Direction);
light1.setInfluencingBounds(bounds);
objRoot.addChild(light1);
BranchGroup sceneObj = new BranchGroup();
//création d'un TG pour la gestion d'échelle
TransformGroup objScale = new TransformGroup();
Transform3D t3d = new Transform3D();
t3d.setScale(0.4);
objScale.setTransform(t3d);
sceneObj.addChild(objScale);
//création d'un TG pour permettre la modification des objets visuels
TransformGroup objTrans = new TransformGroup();
objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
objScale.addChild(objTrans);
MouseRotate myMouseRotate = new MouseRotate();
myMouseRotate.setTransformGroup(objTrans);
myMouseRotate.setSchedulingBounds(new BoundingSphere());
sceneObj.addChild(myMouseRotate);
MouseTranslate myMouseTranslate = new MouseTranslate();
myMouseTranslate.setTransformGroup(objTrans);
myMouseTranslate.setSchedulingBounds(new BoundingSphere());
sceneObj.addChild(myMouseTranslate);
MouseZoom myMouseZoom = new MouseZoom();
myMouseZoom.setTransformGroup(objTrans);
myMouseZoom.setSchedulingBounds(new BoundingSphere());
sceneObj.addChild(myMouseZoom);
Vector3f translate = new Vector3f();
Transform3D T3D = new Transform3D();
TransformGroup TG = null;
TransformGroup vpTrans = null;
SharedGroup share = new SharedGroup();
float[][] position = {{ 0.0f, 0.0f, -3.0f},
{ 6.0f, 0.0f, 0.0f},
{ 6.0f, 0.0f, 6.0f},
{ 3.0f, 0.0f, -10.0f},
{ 13.0f, 0.0f, -30.0f},
{-13.0f, 0.0f, 30.0f},
{-13.0f, 0.0f, 23.0f},
{ 13.0f, 0.0f, 3.0f}};
for (int i = 0; i < position.length; i++){
translate.set(position[i]);
T3D.setTranslation(translate);
TG = new TransformGroup(T3D);
TG.addChild(new Link(share));
objRoot.addChild(TG);
}
vpTrans = su.getViewingPlatform().getViewPlatformTransform();
translate.set( 0.0f, 0.3f, 0.0f);
T3D.setTranslation(translate);
vpTrans.setTransform(T3D);
KeyNavigatorBehavior keyNavBeh = new KeyNavigatorBehavior(vpTrans);
keyNavBeh.setSchedulingBounds(new BoundingSphere(new Point3d(),1000.0));
sceneObj.addChild(keyNavBeh);
//Canvas3D c = new Canvas3D(null);
//PickHighlightBehavior pickBeh = new
//PickHighlightBehavior(c, objRoot, bounds);
BranchGroup b2 = new addObject();
objRoot.addChild(b2);
//System.out.println("br "+b2);
objRoot.addChild(sceneObj);
//objRoot.addChild(b2);
objRoot.compile();
return objRoot;
}
public JPanel presentationPanel() {
JPanel panel1 = new JPanel();
etiq1 = new JLabel
("Modelisation d'un environnement naturel en 3D / © 2005 cnam ubo");
panel1.add(etiq1);
return panel1;
}
//fonction ajout d'objet
private class addObject extends BranchGroup implements ActionListener {
private addObject () {
this.setCapability(BranchGroup.ALLOW_CHILDREN_EXTEND);
}
public void actionPerformed(ActionEvent event){
BranchGroup obj = new BranchGroup ();
// Création d'une sphère
Sphere sphere = new Sphere (0.3f, Sphere.GENERATE_NORMALS, 20);
System.out.println(sphere);
Transform3D sphereRandromTransform = new Transform3D ();
sphereRandromTransform.setTranslation(new Vector3d(randomCoord(),randomCoord(),randomCoord()));
TransformGroup sphereTG = new TransformGroup (sphereRandromTransform);
sphereTG.addChild(sphere);
obj.addChild(sphereTG);
this.addChild(obj);
System.out.println("action ajout");
System.out.println(obj);
}
}
private double randomCoord () {
double coord = Math.random();
return coord>=0.5?-coord*5:coord*5;
}
public void mouseClicked( MouseEvent e ) {
}
public void mouseEntered( MouseEvent e ) {
}
public void mouseExited( MouseEvent e ) {
}
public void mousePressed( MouseEvent e ) {
// if (e.getSource()==reset) {
//setNominalPositionAndOrientation();
//}
}
public void mouseReleased( MouseEvent e ) {
//mode = STILL;
}
public void actionPerformed(ActionEvent e) {
Object target = e.getSource();
if (target == couleur) {
if (couleur.isSelected()){
//scene.pickBeh;
}
}
}
public static void main(String[] args) {
try {
//Définition du type d'affichage boite de dialogue
//UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
UIManager.setLookAndFeel(UIManager.getCrossPlatformLookAndFeelClassName());
} catch (Exception exc) {
System.err.println("Erreur de chargement L&F: " + exc);
}
new Scene3Db();
}
}
In fact I have this
java.lang.NullPointerException :
at applet3d.Scene3Db.<init>(Scene3Db.java:66)
at applet3d.Scene3Db.main(Scene3Db.java:253)
when I want to attach the new object to the main branchgroup
objRoot.addChild(addObjBG);
any idea ?