在将组添加到 Locale 或 SimpleUniverse 后,将对象添加到 BranchGroup
Add a Object to a BranchGroup after the group is added to a Locale or a SimpleUniverse
考虑一个简单的代码:
import java.awt.*;
import javax.media.j3d.*;
import javax.vecmath.Color3f;
import javax.vecmath.*;
import com.sun.j3d.utils.universe.*;
import com.sun.j3d.utils.geometry.*;
public class Main {
public static void main(final String[] args) {
final SimpleUniverse universe = new SimpleUniverse();
final BranchGroup group = new BranchGroup();
TriangleArray triangle = new TriangleArray(3, TriangleArray.COORDINATES);
triangle.setCoordinates(0, new Point3f[]{
new Point3f(-0.3f, 0, -1),
new Point3f( 0, -0.3f, -1),
new Point3f( 0, 0, -1),
});
group.addChild(new Shape3D(triangle));
universe.addBranchGraph(group);
TriangleArray triangle1 = new TriangleArray(3, TriangleArray.COORDINATES);
triangle1.setCoordinates(0, new Point3f[]{
new Point3f(0.3f, 0, -1),
new Point3f( 0, 0.3f, -1),
new Point3f( 0, 0, -1),
});
group.addChild(new Shape3D(triangle1));
}
}
在将 group
添加到 universe
之前,我添加了一个三角形 triangle
,它工作正常。
将组添加到宇宙后,我想再添加一个三角形triangle1
。但是,我收到错误
Exception in thread "main" javax.media.j3d.RestrictedAccessException: Group: only a BranchGroup node may be added
at javax.media.j3d.Group.addChild(Group.java:284)
at Main.<init>(Main.java:34)
at Main.main(Main.java:11)
所以我设置了 BranchGroup
的 ALLOW_DETACH
功能,将其从语言环境中删除,添加对象,然后将组重新添加到宇宙中:
// On initialization
group.setCapability(BranchGroup.ALLOW_DETACH);
// ...
// Adding another triangle
group.detach();
group.addChild(new Shape3D(triangle1));
universe.addBranchGraph(group);
但是,这似乎不必要地复杂。还有其他更好的方法来添加对象吗?
只需尽早调用 BranchGroup.setCapability(Group.ALLOW_CHILDREN_EXTEND),即使在添加 BranchGroup 之后,您也应该被允许添加新的子项。
考虑一个简单的代码:
import java.awt.*;
import javax.media.j3d.*;
import javax.vecmath.Color3f;
import javax.vecmath.*;
import com.sun.j3d.utils.universe.*;
import com.sun.j3d.utils.geometry.*;
public class Main {
public static void main(final String[] args) {
final SimpleUniverse universe = new SimpleUniverse();
final BranchGroup group = new BranchGroup();
TriangleArray triangle = new TriangleArray(3, TriangleArray.COORDINATES);
triangle.setCoordinates(0, new Point3f[]{
new Point3f(-0.3f, 0, -1),
new Point3f( 0, -0.3f, -1),
new Point3f( 0, 0, -1),
});
group.addChild(new Shape3D(triangle));
universe.addBranchGraph(group);
TriangleArray triangle1 = new TriangleArray(3, TriangleArray.COORDINATES);
triangle1.setCoordinates(0, new Point3f[]{
new Point3f(0.3f, 0, -1),
new Point3f( 0, 0.3f, -1),
new Point3f( 0, 0, -1),
});
group.addChild(new Shape3D(triangle1));
}
}
在将 group
添加到 universe
之前,我添加了一个三角形 triangle
,它工作正常。
将组添加到宇宙后,我想再添加一个三角形triangle1
。但是,我收到错误
Exception in thread "main" javax.media.j3d.RestrictedAccessException: Group: only a BranchGroup node may be added
at javax.media.j3d.Group.addChild(Group.java:284)
at Main.<init>(Main.java:34)
at Main.main(Main.java:11)
所以我设置了 BranchGroup
的 ALLOW_DETACH
功能,将其从语言环境中删除,添加对象,然后将组重新添加到宇宙中:
// On initialization
group.setCapability(BranchGroup.ALLOW_DETACH);
// ...
// Adding another triangle
group.detach();
group.addChild(new Shape3D(triangle1));
universe.addBranchGraph(group);
但是,这似乎不必要地复杂。还有其他更好的方法来添加对象吗?
只需尽早调用 BranchGroup.setCapability(Group.ALLOW_CHILDREN_EXTEND),即使在添加 BranchGroup 之后,您也应该被允许添加新的子项。