Skip to main content

adding a branchgroup to a live scene with swing buttons

7 replies [Last post]
spytox4
Offline
Joined: 2006-02-14

Hi, please have a look on the following code.
I want to add "objects" in a live scene.
I've planned to do this by attaching a new branchgroup
to an existing alive structure.
The code of the new branchgroup is in the action of a swing button.

// method "addObject" called by the button

private class addObject extends BranchGroup implements ActionListener {
public void actionPerformed(ActionEvent event){
BranchGroup obj = new BranchGroup ();
// Creation of a sphere
Sphere sphere = new Sphere (0.3f);
System.out.println(sphere);
obj.addChild (sphere);
obj.addChild (this);
System.out.println("add action");
System.out.println(obj);

// definition of the button

private JButton ajout = new JButton("Ajout");

...
JPanel p2 = new JPanel();
ajout.addActionListener(new addObject());
p2.add(ajout);
...

// code to attach the branchgroup

.....
BranchGroup b2 = new addObject();
System.out.println("br "+b2);
objRoot.addChild(b2);
objRoot.compile();
return objRoot;

But, the Sphere is not seen on the scene
even if the action on the button seems to work.
And if i do twice, i get this error :

Exception occurred during event dispatching:

javax.media.j3d.MultipleParentException: Group.addChild: child already has a parent

at javax.media.j3d.GroupRetained.checkValidChild(GroupRetained.java:442)

at javax.media.j3d.GroupRetained.addChild(GroupRetained.java:451)

at javax.media.j3d.Group.addChild(Group.java:266)

at applet3d.Scene3D$addObject.actionPerformed(Scene3D.java:588)

at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:1450)

at javax.swing.AbstractButton$ForwardActionEvents.actionPerformed(AbstractButton.java:1504)

at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:378)

at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:250)

at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:216)

at java.awt.Component.processMouseEvent(Component.java:3715)

at java.awt.Component.processEvent(Component.java:3544)

at java.awt.Container.processEvent(Container.java:1164)

at java.awt.Component.dispatchEventImpl(Component.java:2593)

at java.awt.Container.dispatchEventImpl(Container.java:1213)

at java.awt.Component.dispatchEvent(Component.java:2497)

at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:2451)

at java.awt.LightweightDispatcher.processMouseEvent(Container.java:2216)

at java.awt.LightweightDispatcher.dispatchEvent(Container.java:2125)

at java.awt.Container.dispatchEventImpl(Container.java:1200)

at java.awt.Window.dispatchEventImpl(Window.java:914)

at java.awt.Component.dispatchEvent(Component.java:2497)

at java.awt.EventQueue.dispatchEvent(EventQueue.java:339)

at java.awt.EventDispatchThread.pumpOneEventForHierarchy(EventDispatchThread.java:131)

at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:98)

at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)

at java.awt.EventDispatchThread.run(EventDispatchThread.java:85)

Anyone can help ?

Thanks

Reply viewing options

Select your preferred way to display the comments and click "Save settings" to activate your changes.
spytox4
Offline
Joined: 2006-02-14

Thanks for your help.

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

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.(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 ?

Anonymous

in this method :
[code]
//création du graphe de scène
public BranchGroup createSceneGraph (SimpleUniverse su) {

//création d'un BG racine de l'arbre 3D
// the line below creates a new BranchGroup ref. as objRoot.
//The private ref. from class to this BG is lost.
//BranchGroup objRoot = new BranchGroup ();
//change it to:
objRoot = new BranchGroup ();
objRoot.setCapability (BranchGroup.ALLOW_PICKABLE_READ);
objRoot.setCapability (BranchGroup.ALLOW_PICKABLE_WRITE);
[/code]

Then uncomment this line...
[code]//objRoot.addChild(b2);[/code]

Below this part :
[code]vpTrans = su.getViewingPlatform().getViewPlatformTransform();
translate.set( 0.0f, 0.3f, 0.0f);[/code]
you change the pos. of the camera. The camera should always look in the direction -Z.

You should also read some about inner classes. The code is not as it should be.
hope helps.

spytox4
Offline
Joined: 2006-02-14

Hi, I've remove the code for arrow keys management (vptrans) because i don't need it and change the line as you mentionned but it is still the same : no view :

package applet3d;

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 = 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);

//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();
}
}

Anonymous

Hi,
your camera is [b]still very close[/b] to origin.

su.getViewingPlatform().setNominalViewingTransform();
add this code after this line :
[code]
cameraTG = su.getViewingPlatform().getViewPlatformTransform();
Transform3D cameraTransform = new Transform3D();
cameraTransform.setTranslation(new Vector3d(0.0,0.0,20.0));
cameraTG.setTransform(cameraTransform);
[/code]
so you move the camera to 0,0,20.

Then change action performed to:
[code]
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);
objRoot.addChild(obj);

System.out.println ("action ajout");
System.out.println (obj);
}
[/code]
Actually you dont need to create an inner class here.

spytox4
Offline
Joined: 2006-02-14

Thanks so much BrainC
That is working !

spytox4

spytox4
Offline
Joined: 2006-02-14

Thanks for your help.

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

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.(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 ?

Anonymous

Hi,
exception tells that you already have attached the sphere to a Group. (with the first click of the button). you dont see nothing because you actually dont attach the BG to the scene. I didnt want to medify your code too much. try this one below...
[code]
public class TestPlatform extends JApplet {

private SimpleUniverse su;
private BranchGroup sceneBG, mainBG, shapeBG;
private TransformGroup mainTG, shapeTG, cameraTG;
private BoundingSphere bounds;
private Material material2;
private static int mycount = 1;

public void init () {

setLayout (new BorderLayout ());
GraphicsConfiguration config = SimpleUniverse.getPreferredConfiguration ();
Canvas3D canvas = new Canvas3D (config);
add ("Center", canvas);

JButton ajout=new JButton("Attach");
JPanel p2 = new JPanel();

add ("East", p2);
su = new SimpleUniverse (canvas);
// su.getViewingPlatform ().setNominalViewingTransform ();
Transform3D cameraTransform = new Transform3D ();
cameraTransform.setTranslation (new Vector3d (0.0, 0.0, 15.0));
cameraTG = su.getViewingPlatform ().getViewPlatformTransform ();
cameraTG.setTransform (cameraTransform);
sceneBG = createSceneBG ();

addObject addObjBG = new addObject();
ajout.addActionListener(addObjBG);
mainBG.addChild(addObjBG);
p2.add(ajout);

OrbitBehavior orbit = new OrbitBehavior (canvas, OrbitBehavior.REVERSE_ALL);
orbit.setSchedulingBounds (bounds);
su.getViewingPlatform ().setViewPlatformBehavior (orbit);
su.addBranchGraph (sceneBG);
this.setSize (600, 400);
}

private BranchGroup createSceneBG () {

mainBG = new BranchGroup ();
mainBG.setCapability (BranchGroup.ALLOW_DETACH);
mainBG.setCapability (Group.ALLOW_CHILDREN_EXTEND);
mainBG.setCapability (Group.ALLOW_CHILDREN_READ);
mainBG.setCapability (Group.ALLOW_CHILDREN_WRITE);

bounds = new BoundingSphere (new Point3d (), 1000);
Background back = new Background (new Color3f (0.5f, 0.5f, 1f));
back.setApplicationBounds (bounds);
mainBG.addChild (back);
DirectionalLight light1 = new DirectionalLight (new Color3f (1f, 1f, 1f), new Vector3f (1f, 1f, 0.5f));
light1.setInfluencingBounds (bounds);
mainBG.addChild (light1);
mainBG.compile ();
return mainBG;
}

private class addObject extends BranchGroup implements ActionListener {

private addObject () {
this.setCapability(BranchGroup.ALLOW_CHILDREN_EXTEND);
}
public void actionPerformed(ActionEvent event){
BranchGroup obj = new BranchGroup ();

// Creation of a sphere
Sphere sphere = new Sphere (0.3f, Sphere.GENERATE_NORMALS, 20);

System.out.println("add action");

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

//mainBG.addChild(obj);
}
}

private double randomCoord () {
double coord = Math.random();
return coord>=0.5?-coord*5:coord*5;
}

}

[/code]
--------------------------------------------------------------------------
Some time ago ive written a test code for testing memory leaks by BG.detach... try this one also.

[code]
public class TestPlatformMem extends JApplet implements ActionListener {

private SimpleUniverse su;
private BranchGroup sceneBG, mainBG, shapeBG;
private TransformGroup mainTG, shapeTG, cameraTG;
private BoundingSphere bounds;
private Material material2;
private static int mycount = 1;

public void init () {

setLayout (new BorderLayout ());
GraphicsConfiguration config = SimpleUniverse.getPreferredConfiguration ();
Canvas3D canvas = new Canvas3D (config);
add ("Center", canvas);
JPanel panel = new JPanel (new FlowLayout ());
JButton button = new JButton ("Attach");
panel.add (button);
button.addActionListener (this);
JButton button2 = new JButton ("Detach");
panel.add (button2);
button2.addActionListener (this);
add ("East", panel);
su = new SimpleUniverse (canvas);
// su.getViewingPlatform ().setNominalViewingTransform ();
Transform3D cameraTransform = new Transform3D ();
cameraTransform.setTranslation (new Vector3d (0.0, 0.0, 15.0));
cameraTG = su.getViewingPlatform ().getViewPlatformTransform ();
cameraTG.setTransform (cameraTransform);
sceneBG = createSceneBG ();
OrbitBehavior orbit = new OrbitBehavior (canvas, OrbitBehavior.REVERSE_ALL);
orbit.setSchedulingBounds (bounds);
su.getViewingPlatform ().setViewPlatformBehavior (orbit);
su.addBranchGraph (sceneBG);
this.setSize (600, 400);
}

private BranchGroup createSceneBG () {

mainBG = new BranchGroup ();
mainBG.setCapability (BranchGroup.ALLOW_DETACH);
mainBG.setCapability (Group.ALLOW_CHILDREN_EXTEND);
mainBG.setCapability (Group.ALLOW_CHILDREN_READ);
mainBG.setCapability (Group.ALLOW_CHILDREN_WRITE);
shapeBG = new BranchGroup ();
shapeBG.setCapability (BranchGroup.ALLOW_DETACH);
mainTG = new TransformGroup ();
mainTG.addChild (shapeBG);
mainBG.addChild (mainTG);
shapeTG = new TransformGroup ();
shapeBG.addChild (shapeTG);
bounds = new BoundingSphere (new Point3d (), 1000);
Background back = new Background (new Color3f (0.5f, 0.5f, 1f));
back.setApplicationBounds (bounds);
mainBG.addChild (back);
DirectionalLight light1 = new DirectionalLight (new Color3f (1f, 1f, 1f), new Vector3f (1f, 1f, 0.5f));
light1.setInfluencingBounds (bounds);
mainBG.addChild (light1);
mainBG.compile ();
return mainBG;
}

private BranchGroup createRandomShapes (int count) {

BranchGroup shapeBG = new BranchGroup ();
shapeBG.setCapability (BranchGroup.ALLOW_DETACH);
for (int i = 0; i < count; i++) {
Transform3D randomShapeTransform = new Transform3D ();
randomShapeTransform.setTranslation (new Vector3d (createRandomSign () * Math.random () * 3f, createRandomSign () * Math.random () * 3f, createRandomSign () * Math.random () * 3f));
TransformGroup randomShapeTransformGroup = new TransformGroup (randomShapeTransform);
Shape3D shape3D = new Shape3D ();
shape3D.setGeometry (new Sphere ((float) Math.random () * 0.3f, Sphere.GENERATE_NORMALS | Sphere.GEOMETRY_NOT_SHARED, 24, null).getShape (Sphere.BODY).getGeometry ());
shape3D.setAppearance (buildAppearance (true));
randomShapeTransformGroup.addChild (shape3D);
shapeBG.addChild (randomShapeTransformGroup);
}
shapeBG.setUserData (new String ("ShapeBG" + mycount));
mycount++;
return shapeBG;
}

private Appearance buildAppearance (boolean wireFrame) {

Appearance appear = new Appearance ();
Color3f matAmbient = new Color3f (0.2f, 0.2f, 0.2f), matEmissive = new Color3f ((float) Math.random (), (float) Math.random (), (float) Math.random ()), matDiffuse = new Color3f (1f, 1f, 1f), matSpecular = new Color3f (
1f, 1f, 1f);
float matShininess = 81f;
material2 = new Material (matAmbient, matEmissive, matDiffuse, matSpecular, matShininess);
PolygonAttributes pa = new PolygonAttributes ();
if (!wireFrame)
pa.setPolygonMode (PolygonAttributes.POLYGON_FILL);
else
pa.setPolygonMode (PolygonAttributes.POLYGON_LINE);
pa.setCullFace (PolygonAttributes.CULL_NONE);
appear.setMaterial (material2);
appear.setPolygonAttributes (pa);
return appear;
}

private int createRandomSign () {

return Math.random () >= 0.5 ? 1 : -1;
}

public void actionPerformed (ActionEvent e) {

if (e.getActionCommand ().equals ("Attach")) {
System.out.println ("Attaching random shapes...");
mainBG.addChild (createRandomShapes (40));
} else if (e.getActionCommand ().equals ("Detach")) {
System.out.print ("Removing random shapes...");
Enumeration enume = mainBG.getAllChildren ();
while (enume.hasMoreElements ()) {
Object o = enume.nextElement ();
if (o instanceof BranchGroup) {
System.out.println (((BranchGroup) o).getUserData ());
// release BGs and free mem ?!?!?!?!?!
((BranchGroup) o).detach ();
su.getLocale ().addBranchGraph ((BranchGroup) o);
su.getLocale ().removeBranchGraph ((BranchGroup) o);
BranchGroup bg = ((BranchGroup)o);
bg=null;
}
}
mycount = 1;
}
}
}[/code]