📄 showtext3d.java
字号:
package showtext3d;import java.awt.*;import java.awt.event.*;import java.applet.Applet;import java.awt.BorderLayout;import java.awt.event.*;import java.awt.GraphicsConfiguration;import java.awt.geom.Line2D;import java.awt.geom.QuadCurve2D;import java.awt.Font;import javax.media.j3d.*;import javax.vecmath.*;import com.sun.j3d.utils.applet.MainFrame;import com.sun.j3d.utils.geometry.ColorCube;import com.sun.j3d.utils.universe.*;import com.sun.j3d.utils.behaviors.mouse.*;/** * <p>Title: 展示三维立体文字</p> * <p>Description: 应用Java 3D API生成三维立体文字</p> * <p>Copyright: Copyright (c) 2004</p> * <p>Company: 北京师范大学计算机系</p> * @author 王大伟 * @version 1.0 */public class ShowText3D extends Applet{ boolean isStandalone = false; public BranchGroup createSceneGraph(){ BranchGroup objRoot = new BranchGroup(); TransformGroup objTrans = new TransformGroup(); objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE); objTrans.setCapability(TransformGroup.ALLOW_TRANSFORM_READ); objRoot.addChild(objTrans); BoundingSphere bounds = new BoundingSphere(new Point3d(0.0,0.0,0.0),100.0); Color3f bgColor = new Color3f(1.0f,1.0f,1.0f); Background bg = new Background(bgColor); bg.setApplicationBounds(bounds); objRoot.addChild(bg); Color3f DirectionalColor = new Color3f(1.f,1.f,1.f); Vector3f vec = new Vector3f(0.5f,-.5f,-1.0f); DirectionalLight directionalLight = new DirectionalLight(DirectionalColor,vec); directionalLight.setInfluencingBounds(bounds); objTrans.addChild(directionalLight); String text = "Java 3D"; FontExtrusion extrusion0 = new FontExtrusion(); QuadCurve2D.Double curve = new QuadCurve2D.Double(); curve.setCurve(0,0,0.2,0.05,0.4,0); FontExtrusion extrusion1 = new FontExtrusion(); extrusion1.setExtrusionShape(curve); Line2D.Double line = new Line2D.Double(); line.setLine(0,0,0.4,0); FontExtrusion extrusion2 = new FontExtrusion(); extrusion2.setExtrusionShape(line); Font3D f3d = new Font3D(new Font("", Font.PLAIN, 1), extrusion0); Text3D txt = new Text3D(f3d, text, new Point3f(0.0f, -1.f, -1.f)); Shape3D sh = new Shape3D(); Appearance app = new Appearance(); Material m = new Material(); m.setDiffuseColor(new Color3f(0.5f,0.0f,1.0f)); app.setMaterial(m); sh.setGeometry(txt); sh.setAppearance(app); objTrans.addChild(sh); MouseRotate behavior = new MouseRotate(); behavior.setTransformGroup(objTrans); behavior.setSchedulingBounds(bounds); objRoot.addChild(behavior); MouseZoom behavior2 = new MouseZoom(); behavior2.setTransformGroup(objTrans); behavior2.setSchedulingBounds(bounds); objRoot.addChild(behavior2); MouseTranslate behavior3 = new MouseTranslate(); behavior3.setTransformGroup(objTrans); behavior3.setSchedulingBounds(bounds); objRoot.addChild(behavior3); return objRoot; } public ShowText3D(){ setLayout(new BorderLayout()); GraphicsConfiguration config=SimpleUniverse.getPreferredConfiguration(); Canvas3D c = new Canvas3D(config); add("Center", c); BranchGroup scene = createSceneGraph(); SimpleUniverse u = new SimpleUniverse(c); u.getViewingPlatform().setNominalViewingTransform(); u.addBranchGraph(scene); } public static void main(String[] args) { ShowText3D applet = new ShowText3D(); applet.isStandalone = true; Frame frame; frame = new Frame() { protected void processWindowEvent(WindowEvent e) { super.processWindowEvent(e); if (e.getID() == WindowEvent.WINDOW_CLOSING) { System.exit(0); } } public synchronized void setTitle(String title) { super.setTitle(title); enableEvents(AWTEvent.WINDOW_EVENT_MASK); } }; frame.setTitle("展示三维立体文字"); frame.add(applet, BorderLayout.CENTER); applet.init(); applet.start(); frame.setSize(400,320); Dimension d = Toolkit.getDefaultToolkit().getScreenSize(); frame.setLocation((d.width - frame.getSize().width) / 2, (d.height - frame.getSize().height) / 2); frame.setVisible(true); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -