📄 example6_15.java.bak
字号:
/* 3D文字 */
import com.sun.j3d.loaders.objectfile.*;
import java.awt.*;
import com.sun.j3d.utils.universe.*;
import javax.media.j3d.*;
import javax.vecmath.*;
import com.sun.j3d.utils.behaviors.vp.*;
import com.sun.j3d.utils.behaviors.keyboard.*;
class Text3d
{
private String fontName = "default";
private String textString = null;
private double tessellation = 0.0;
private SimpleUniverse u;
private OrbitBehavior orbit;
public BranchGroup createSceneGraph()
{
float sl = textString.length();
BranchGroup objRoot = new BranchGroup();
TransformGroup objScale = new TransformGroup();
Transform3D t3d = new Transform3D();
// 设置字体大小
t3d.setScale(1.5/sl);
objScale.setTransform(t3d);
objRoot.addChild(objScale);
TransformGroup objTrans = new TransformGroup();
objScale.addChild(objTrans);
//建立3D字
Font3D f3d = new Font3D(new Font(fontName, Font.PLAIN, 2),
new FontExtrusion());
Text3D txt = new Text3D(f3d, textString,
new Point3f( -sl/2.0f, -1.f, -1.f));
Shape3D sh = new Shape3D();
Appearance app = new Appearance();
Material mm = new Material();
mm.setLightingEnable(true);
app.setMaterial(mm);
sh.setGeometry(txt);
sh.setAppearance(app);
objTrans.addChild(sh);
//设置场景的范围
BoundingSphere bounds=new BoundingSphere(new Point3d(0.0,0.0,0.0),100.0);
//使物体旋转
if (false)
{
Transform3D yAxis = new Transform3D();
Alpha rotationAlpha = new Alpha(-1, Alpha.INCREASING_ENABLE,
0, 0, 4000, 0, 0, 0, 0, 0);
RotationInterpolator rotator =
new RotationInterpolator(rotationAlpha, objTrans, yAxis,
0.0f, (float) Math.PI*2.0f);
rotator.setSchedulingBounds(bounds);
objTrans.addChild(rotator);
}
// 设置背景
Color3f bgColor = new Color3f(0.15f, 0.15f, 0.5f);
Background bgNode = new Background(bgColor);
bgNode.setApplicationBounds(bounds);
objRoot.addChild(bgNode);
// 设置环境光源
Color3f ambientColor = new Color3f(0.9f, 0.9f, 0.9f);
AmbientLight ambientLightNode = new AmbientLight(ambientColor);
ambientLightNode.setInfluencingBounds(bounds);
objRoot.addChild(ambientLightNode);
// 设置光源方向
Color3f light1Color = new Color3f(1.0f, 1.0f, 0.9f);
Vector3f light1Direction = new Vector3f(1.0f, 1.0f, 1.0f);
Color3f light2Color = new Color3f(1.0f, 1.0f, 0.9f);
Vector3f light2Direction = new Vector3f(-1.0f, -1.0f, -1.0f);
DirectionalLight light1
= new DirectionalLight(light1Color, light1Direction);
light1.setInfluencingBounds(bounds);
objRoot.addChild(light1);
DirectionalLight light2
= new DirectionalLight(light2Color, light2Direction);
light2.setInfluencingBounds(bounds);
objRoot.addChild(light2);
return objRoot;
}
public Text3d()
{
textString = "我喜欢Java";
BranchGroup scene = createSceneGraph();
u = new SimpleUniverse();
//在虚拟空间中添加鼠标行为
ViewingPlatform viewingPlatform = u.getViewingPlatform();
viewingPlatform.setNominalViewingTransform();
//设置鼠标控制
orbit = new OrbitBehavior();
BoundingSphere bounds =
new BoundingSphere(new Point3d(0.0, 0.0, 0.0), 100.0);
orbit.setSchedulingBounds(bounds);
viewingPlatform.setViewPlatformBehavior(orbit);
u.addBranchGraph(scene);
}
}
public class Example6_15
{
public static void main(String[] args)
{ new Text3d(); }
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -