我现在正在研究java3d,其中用到Billboard类时遇到了问题,我在场景中添加了多个坐标系,每个坐标系中都有文字,我想在整个场景旋转的时候所有文字都垂直屏幕显示,但我用billboard实现时,所有的文字都显示到屏幕最中间了都聚到一起了,我不知道什么原因,求billboard具体用法,网上很少例子啊。谢谢了。下面是用java3d编写的一个小例子,彩色六面体的坐标明明不是在中间,为什么加上billboard后六面体就显示在中间了:
import java.applet.Applet;
import java.awt.BorderLayout;
import java.awt.Frame;
import java.awt.GraphicsConfiguration;
import javax.media.j3d.Billboard;
import javax.media.j3d.BoundingSphere;
import javax.media.j3d.BranchGroup;
import javax.media.j3d.Canvas3D;
import javax.media.j3d.Transform3D;
import javax.media.j3d.TransformGroup;
import javax.vecmath.Point3f;
import javax.vecmath.Vector3f;
import com.sun.j3d.utils.applet.MainFrame;
import com.sun.j3d.utils.behaviors.mouse.MouseRotate;
import com.sun.j3d.utils.behaviors.mouse.MouseTranslate;
import com.sun.j3d.utils.behaviors.mouse.MouseZoom;
import com.sun.j3d.utils.geometry.ColorCube;
import com.sun.j3d.utils.universe.SimpleUniverse;
public class SimpleMouseBhv extends Applet {
public BranchGroup createSceneGraph() {
BranchGroup objRoot = new BranchGroup();
// 创建根分支节点
objRoot.addChild(new Axis());
// 创建x,y,x坐标轴
TransformGroup objTG = null;
Transform3D objT3D = new Transform3D();
MouseRotate myMR = null;
MouseTranslate myMT = null;
MouseZoom myMZ = null;
// 创建左边的局部坐标系,添加旋转、平移功能
objT3D.setTranslation(new Vector3f(-0.6f, 0.0f, -0.6f));
objTG = new TransformGroup(objT3D);
objTG.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
objTG.setCapability(TransformGroup.ALLOW_TRANSFORM_READ);
objRoot.addChild(objTG);
objTG.addChild(new ColorCube(0.4));
// 添加旋转功能
myMR = new MouseRotate();
myMR.setTransformGroup(objTG);
myMR.setSchedulingBounds(new BoundingSphere());
objRoot.addChild(myMR);
// 添加平移功能
myMT = new MouseTranslate();
myMT.setTransformGroup(objTG);
myMT.setSchedulingBounds(new BoundingSphere());
//添加广告牌,为什么添加了广告牌之后图形在中间显示??
Billboard billboard = new Billboard(objTG);
billboard.setSchedulingBounds(new BoundingSphere());
objTG.addChild(billboard);
objRoot.addChild(myMT);
objRoot.compile();
return objRoot;
}
public SimpleMouseBhv() {
setLayout(new BorderLayout());
GraphicsConfiguration config=SimpleUniverse.getPreferredConfiguration();
Canvas3D canvas3D = new Canvas3D(config);
add("Center", canvas3D);
BranchGroup scene = createSceneGraph();
SimpleUniverse simpleU = new SimpleUniverse(canvas3D);
simpleU.getViewingPlatform().setNominalViewingTransform();
simpleU.addBranchGraph(scene);
}
public static void main(String[] args) {
Frame frame = new MainFrame(new SimpleMouseBhv(), 256, 256);
}
}
------解决方案--------------------------------------------------------
上网找张杰的Java3d交互式三维图形编程电子版(可以下到的),有java3d的billboard广告牌的讲解,可以解决你这个问题。