我正在尝试使用Java3D为多容器加载问题制作3D界面。
我希望在单独的框架中可视化每个容器和已包装在其中的项目。 (例如,如果我有3个容器,则会有3个不同的框架)
在容器的类中,我添加了一个BranchGroup变量:
private static BranchGroup scene ;
在构造函数中,我创建了BranchGroup场景和一个框架(我将在上面发布Interface类)
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
类中,我编写了一个方法来创建框架并在其中绘制容器,另一种方法是添加项目的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()
解决问题,但没有改变(可能是
我把分离方法放在错误的地方,我不确定)
有谁知道如何创建多个场景?在每个场景中我绘制容器和打包的项目?
我希望你能理解我的问题。
我不能在这里发布完整代码和所有类,因为它太复杂了。
谢谢。
答案 0 :(得分:0)
所以,我做的工作如下:
我制作了BranchGroup scene1,画布和Universe全局变量:
private static SimpleUniverse simpleU ;
private static Canvas3D canvas3D;
private static BranchGroup scene1;
我在容器的类构造函数中创建了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);
现在,我在框架中同时获得容器和包装物品。