java多容器加载Java3D
我正在尝试使用Java3D为多容器装载问题创建一个3D界面
我想将每个容器和已经包装在其中的物品分别显示在不同的框架中。(例如,如果我有3个容器,将有3个不同的框架)
在容器的类中,我添加了BranchGroup变量:
private static BranchGroup scene ;
在构造函数中,我创建了BranchGroup场景和一个框架(我将发布上面的接口类)
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
类中,我编写了一个方法来创建框架并在其中绘制容器,以及另一个方法来添加项目的3DNode
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;
}
}
运行该程序时,我得到多个框架,其中绘制了容器,但没有项目
我知道我的问题在分支组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()
来解决这个问题,但没有任何改变(可能是这样)
我将分离方法放错了位置,我不确定)
有人知道如何创建多个场景,在每个场景中我都会绘制容器和包装物品吗
我希望你能理解我的问题
我不能在这里发布完整的代码和所有的类,因为它太复杂了
多谢各位
# 1 楼答案
因此,我所做的工作如下:
我制作了BranchGroup场景1、画布和宇宙全局变量:
我在容器的类构造函数中创建了3D接口:
每添加一个新项目,我就会创建另一个分支组
scene
,并将其添加到全局分支组scene1
,如下所示:现在,我把集装箱和包装好的东西都放进了镜框里