多个容器加载 Java3D
Multiple Containers Loading Java3D
我正在尝试使用 Java3D 为多容器加载问题制作 3D 界面。
我想在单独的框架中可视化每个容器和已经装在其中的物品。 (例如,如果我有 3 个容器,就会有 3 个不同的框架)
在容器的 class 中,我添加了一个 BranchGroup 变量:
private static BranchGroup scene ;
并且在构造函数中,我创建了 BranchGroup 场景和一个框架(我将 post 上面的界面 class)
public ContainerW(final int iD) {
super();
cRef = iD;
this.emptyBreadth = 0;
this.length = 0.8;
this.height = 0.6;
this.breadth = 0.5 - emptyBreadth;
this.usedVolume = 0;
volume = length * breadth * height;
this.packedItems = new ArrayList<ItemsUnit>();
scene = new BranchGroup();
Frame frame = new MainFrame(new Interface(scene),800, 800);
}
打包新项目时,我添加这个(我将新节点添加到 BranchGroup 场景):
scene.addChild(Interface.addItem(item[p].getLength(), item[p].getBreadth(), item[p].getBreadth(), x, y, z));
(x
、y
和z
是翻译参数)
在 Interface
class 中,我写了一个方法来创建框架并在其中绘制容器,另一个方法是添加项目的 3D Node
。
package Interface3D;
import java.applet.Applet;
import java.awt.BorderLayout;
import javax.media.j3d.Appearance;
import javax.media.j3d.BranchGroup;
import javax.media.j3d.Canvas3D;
import javax.media.j3d.PolygonAttributes;
import javax.media.j3d.Transform3D;
import javax.media.j3d.TransformGroup;
import javax.media.j3d.TransparencyAttributes;
import javax.vecmath.AxisAngle4d;
import javax.vecmath.Vector3f;
import com.sun.j3d.utils.geometry.Box;
import com.sun.j3d.utils.universe.SimpleUniverse;
import Classes.ContainerW;
public class Interface extends Applet{
public Interface(BranchGroup scene) {
this.setLayout(new BorderLayout());
Canvas3D canvas3D = new Canvas3D(SimpleUniverse.getPreferredConfiguration());
this.add(canvas3D, BorderLayout.CENTER);
SimpleUniverse simpleU = new SimpleUniverse(canvas3D);
simpleU.getViewingPlatform().setNominalViewingTransform();
//create an appearance for the container
Appearance app = new Appearance();
//Create the polygon attributes
PolygonAttributes polyAttr = new PolygonAttributes();
//Set them so that the draw mode is polygon line
polyAttr.setPolygonMode(PolygonAttributes.POLYGON_LINE);
polyAttr.setCullFace(PolygonAttributes.CULL_NONE);
//Use these in the appearance
app.setPolygonAttributes(polyAttr);
//rotate the view angle
Transform3D rotateCube = new Transform3D();
rotateCube.set(new AxisAngle4d(1.0, 1.0, 0.0, Math.PI /4.0));
//create a transform Group for the container
TransformGroup tg1 = new TransformGroup(rotateCube);
tg1.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
//create a box: container
Box container = new Box ( (float) ContainerW.getLength() , (float) ContainerW.getBreadth(), (float) ContainerW.getHeight(), app);
//add the container to the first transform group
tg1.addChild(container);
//add the first transform group to the branch group
scene.addChild(tg1);
scene.compile();
simpleU.addBranchGraph(scene);
}
/**
* adding an item box into the container in the position (x, y, z)
* @param x
* @param y
* @param z
*/
public static TransformGroup addItem(double l, double b, double h, double x, double y, double z) {
TransformGroup tg2 = new TransformGroup ();
//set the appearance of the product boxes (transparent)
Appearance app = new Appearance();
app.setTransparencyAttributes(new TransparencyAttributes(TransparencyAttributes.BLENDED,0.2f));
//create a box having length = l, breadth = b and height = h
Box box = new Box( (float )l, (float) b, (float) h, app);
// vector to translate the item box to the position (x, y, z)
Vector3f position = new Vector3f((float) x, (float) y, (float) z);
//creation of a 3D transform group
Transform3D transform = new Transform3D();
transform.setTranslation(position);
tg2.setTransform(transform);
//add new item box to the transform group
tg2.addChild(box);
return tg2;
}
}
当 运行 程序时,我得到多个框架,其中绘制了容器,但没有项目
我知道我的问题出在 BranchGroup scene
因为我得到这个错误:
javax.media.j3d.RestrictedAccessException: Group: only a BranchGroup node may be added
因为这一行:
scene.addChild(Interface.addItem(item[p].getLength(), item[p].getBreadth(), item[p].getBreadth(), x, y, z));
我尝试用 scene.detach()
解决问题,但没有任何改变(也许
我把分离方法放在错误的地方,我不确定)
有谁知道如何创建多个场景并在每个场景中绘制容器和包装好的物品?
希望你能理解我的问题。
我不能 post 完整代码和所有 class 在这里,因为它太复杂了。
谢谢。
所以,我做了如下工作:
我制作了 BranchGroup scene1 、 canvas 和 universe 全局变量:
private static SimpleUniverse simpleU ;
private static Canvas3D canvas3D;
private static BranchGroup scene1;
我在容器的 class 构造函数中创建了 3D 界面:
public ContainerW(final int iD) {
super();
cRef = iD;
this.emptyBreadth = 0;
this.length = 0.8;
this.height = 0.6;
this.breadth = 0.5 - emptyBreadth;
this.usedVolume = 0;
volume = length * breadth * height;
this.packedItems = new ArrayList<ItemsUnit>();
//----------------------INTERFACE----------------------------
this.setLayout(new BorderLayout());
//create a canvas3D
canvas3D = new Canvas3D(SimpleUniverse.getPreferredConfiguration());
this.add(canvas3D, BorderLayout.CENTER);
//create a universe
simpleU = new SimpleUniverse(canvas3D);
simpleU.getViewingPlatform().setNominalViewingTransform();
//create a branch group scene
scene1 = new BranchGroup();
//create an appearance for the container
Appearance app = new Appearance();
//Create the polygon attributes
PolygonAttributes polyAttr = new PolygonAttributes();
//Set them so that the draw mode is polygon line
polyAttr.setPolygonMode(PolygonAttributes.POLYGON_LINE);
polyAttr.setCullFace(PolygonAttributes.CULL_NONE);
//Use these in the appearance
app.setPolygonAttributes(polyAttr);
//rotate the view angle
Transform3D rotateCube = new Transform3D();
rotateCube.set(new AxisAngle4d(1.0, 1.0, 0.0, Math.PI /4.0));
//create a transform Group for the container
TransformGroup tg1 = new TransformGroup(rotateCube);
tg1.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
//create a box: container
Box container = new Box ( (float) length , (float) breadth, (float)height, app);
//add the container to the first transform group
tg1.addChild(container);
//add the first transform group to the branch group
scene1.addChild(tg1);
scene1.setCapability(BranchGroup.ALLOW_CHILDREN_EXTEND);
scene1.compile();
simpleU.addBranchGraph(scene1);
Frame frame = new MainFrame(this,800, 800);
}
每添加一个新项目,我都会创建另一个 BranchGroup scene
并将其添加到全局 BranchGroup scene1
,如下所示:
TransformGroup tg2 = new TransformGroup ();
//set the appearance of the product boxes (transparent)
Appearance app = new Appearance();
app.setTransparencyAttributes(new TransparencyAttributes(TransparencyAttributes.BLENDED,0.2f));
//create a box having length = l, breadth = b and height = h
Box box = new Box( (float )item[p].getLength(), (float) item[p].getBreadth(), (float) item[p].getHeight(), app);
// vector to translate the item box to the position (x, y, z)
Vector3f position = new Vector3f((float) x, (float) y, (float) z);
//creation of a 3D transform group
Transform3D transform = new Transform3D();
transform.setTranslation(position);
tg2.setTransform(transform);
//add new item box to the transform group
tg2.addChild(box);
tg2.setCapability(Group.ALLOW_CHILDREN_EXTEND);
BranchGroup scene = new BranchGroup();
scene.addChild(tg2);
scene1.addChild(scene);
现在,我在框架中同时获得了容器和包装好的物品。
我正在尝试使用 Java3D 为多容器加载问题制作 3D 界面。
我想在单独的框架中可视化每个容器和已经装在其中的物品。 (例如,如果我有 3 个容器,就会有 3 个不同的框架)
在容器的 class 中,我添加了一个 BranchGroup 变量:
private static BranchGroup scene ;
并且在构造函数中,我创建了 BranchGroup 场景和一个框架(我将 post 上面的界面 class)
public ContainerW(final int iD) {
super();
cRef = iD;
this.emptyBreadth = 0;
this.length = 0.8;
this.height = 0.6;
this.breadth = 0.5 - emptyBreadth;
this.usedVolume = 0;
volume = length * breadth * height;
this.packedItems = new ArrayList<ItemsUnit>();
scene = new BranchGroup();
Frame frame = new MainFrame(new Interface(scene),800, 800);
}
打包新项目时,我添加这个(我将新节点添加到 BranchGroup 场景):
scene.addChild(Interface.addItem(item[p].getLength(), item[p].getBreadth(), item[p].getBreadth(), x, y, z));
(x
、y
和z
是翻译参数)
在 Interface
class 中,我写了一个方法来创建框架并在其中绘制容器,另一个方法是添加项目的 3D Node
。
package Interface3D;
import java.applet.Applet;
import java.awt.BorderLayout;
import javax.media.j3d.Appearance;
import javax.media.j3d.BranchGroup;
import javax.media.j3d.Canvas3D;
import javax.media.j3d.PolygonAttributes;
import javax.media.j3d.Transform3D;
import javax.media.j3d.TransformGroup;
import javax.media.j3d.TransparencyAttributes;
import javax.vecmath.AxisAngle4d;
import javax.vecmath.Vector3f;
import com.sun.j3d.utils.geometry.Box;
import com.sun.j3d.utils.universe.SimpleUniverse;
import Classes.ContainerW;
public class Interface extends Applet{
public Interface(BranchGroup scene) {
this.setLayout(new BorderLayout());
Canvas3D canvas3D = new Canvas3D(SimpleUniverse.getPreferredConfiguration());
this.add(canvas3D, BorderLayout.CENTER);
SimpleUniverse simpleU = new SimpleUniverse(canvas3D);
simpleU.getViewingPlatform().setNominalViewingTransform();
//create an appearance for the container
Appearance app = new Appearance();
//Create the polygon attributes
PolygonAttributes polyAttr = new PolygonAttributes();
//Set them so that the draw mode is polygon line
polyAttr.setPolygonMode(PolygonAttributes.POLYGON_LINE);
polyAttr.setCullFace(PolygonAttributes.CULL_NONE);
//Use these in the appearance
app.setPolygonAttributes(polyAttr);
//rotate the view angle
Transform3D rotateCube = new Transform3D();
rotateCube.set(new AxisAngle4d(1.0, 1.0, 0.0, Math.PI /4.0));
//create a transform Group for the container
TransformGroup tg1 = new TransformGroup(rotateCube);
tg1.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
//create a box: container
Box container = new Box ( (float) ContainerW.getLength() , (float) ContainerW.getBreadth(), (float) ContainerW.getHeight(), app);
//add the container to the first transform group
tg1.addChild(container);
//add the first transform group to the branch group
scene.addChild(tg1);
scene.compile();
simpleU.addBranchGraph(scene);
}
/**
* adding an item box into the container in the position (x, y, z)
* @param x
* @param y
* @param z
*/
public static TransformGroup addItem(double l, double b, double h, double x, double y, double z) {
TransformGroup tg2 = new TransformGroup ();
//set the appearance of the product boxes (transparent)
Appearance app = new Appearance();
app.setTransparencyAttributes(new TransparencyAttributes(TransparencyAttributes.BLENDED,0.2f));
//create a box having length = l, breadth = b and height = h
Box box = new Box( (float )l, (float) b, (float) h, app);
// vector to translate the item box to the position (x, y, z)
Vector3f position = new Vector3f((float) x, (float) y, (float) z);
//creation of a 3D transform group
Transform3D transform = new Transform3D();
transform.setTranslation(position);
tg2.setTransform(transform);
//add new item box to the transform group
tg2.addChild(box);
return tg2;
}
}
当 运行 程序时,我得到多个框架,其中绘制了容器,但没有项目
我知道我的问题出在 BranchGroup scene
因为我得到这个错误:
javax.media.j3d.RestrictedAccessException: Group: only a BranchGroup node may be added
因为这一行:
scene.addChild(Interface.addItem(item[p].getLength(), item[p].getBreadth(), item[p].getBreadth(), x, y, z));
我尝试用 scene.detach()
解决问题,但没有任何改变(也许
我把分离方法放在错误的地方,我不确定)
有谁知道如何创建多个场景并在每个场景中绘制容器和包装好的物品?
希望你能理解我的问题。
我不能 post 完整代码和所有 class 在这里,因为它太复杂了。
谢谢。
所以,我做了如下工作:
我制作了 BranchGroup scene1 、 canvas 和 universe 全局变量:
private static SimpleUniverse simpleU ;
private static Canvas3D canvas3D;
private static BranchGroup scene1;
我在容器的 class 构造函数中创建了 3D 界面:
public ContainerW(final int iD) {
super();
cRef = iD;
this.emptyBreadth = 0;
this.length = 0.8;
this.height = 0.6;
this.breadth = 0.5 - emptyBreadth;
this.usedVolume = 0;
volume = length * breadth * height;
this.packedItems = new ArrayList<ItemsUnit>();
//----------------------INTERFACE----------------------------
this.setLayout(new BorderLayout());
//create a canvas3D
canvas3D = new Canvas3D(SimpleUniverse.getPreferredConfiguration());
this.add(canvas3D, BorderLayout.CENTER);
//create a universe
simpleU = new SimpleUniverse(canvas3D);
simpleU.getViewingPlatform().setNominalViewingTransform();
//create a branch group scene
scene1 = new BranchGroup();
//create an appearance for the container
Appearance app = new Appearance();
//Create the polygon attributes
PolygonAttributes polyAttr = new PolygonAttributes();
//Set them so that the draw mode is polygon line
polyAttr.setPolygonMode(PolygonAttributes.POLYGON_LINE);
polyAttr.setCullFace(PolygonAttributes.CULL_NONE);
//Use these in the appearance
app.setPolygonAttributes(polyAttr);
//rotate the view angle
Transform3D rotateCube = new Transform3D();
rotateCube.set(new AxisAngle4d(1.0, 1.0, 0.0, Math.PI /4.0));
//create a transform Group for the container
TransformGroup tg1 = new TransformGroup(rotateCube);
tg1.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
//create a box: container
Box container = new Box ( (float) length , (float) breadth, (float)height, app);
//add the container to the first transform group
tg1.addChild(container);
//add the first transform group to the branch group
scene1.addChild(tg1);
scene1.setCapability(BranchGroup.ALLOW_CHILDREN_EXTEND);
scene1.compile();
simpleU.addBranchGraph(scene1);
Frame frame = new MainFrame(this,800, 800);
}
每添加一个新项目,我都会创建另一个 BranchGroup scene
并将其添加到全局 BranchGroup scene1
,如下所示:
TransformGroup tg2 = new TransformGroup ();
//set the appearance of the product boxes (transparent)
Appearance app = new Appearance();
app.setTransparencyAttributes(new TransparencyAttributes(TransparencyAttributes.BLENDED,0.2f));
//create a box having length = l, breadth = b and height = h
Box box = new Box( (float )item[p].getLength(), (float) item[p].getBreadth(), (float) item[p].getHeight(), app);
// vector to translate the item box to the position (x, y, z)
Vector3f position = new Vector3f((float) x, (float) y, (float) z);
//creation of a 3D transform group
Transform3D transform = new Transform3D();
transform.setTranslation(position);
tg2.setTransform(transform);
//add new item box to the transform group
tg2.addChild(box);
tg2.setCapability(Group.ALLOW_CHILDREN_EXTEND);
BranchGroup scene = new BranchGroup();
scene.addChild(tg2);
scene1.addChild(scene);
现在,我在框架中同时获得了容器和包装好的物品。