1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
| import com.sun.j3d.utils.geometry.*; import com.sun.j3d.utils.universe.*; import com.sun.j3d.utils.image.*; import com.sun.j3d.utils.behaviors.vp.*; import java.awt.*; import java.awt.Font; import javax.swing.*; import javax.media.j3d.*; import javax.vecmath.*;
public class Text3DTest { public Text3DTest() { BranchGroup group = new BranchGroup(); Color3f green = new Color3f(0.0f, 1.0f, 0.0f); Color3f white = new Color3f(1.0f, 1.0f, 1.0f); Color3f red = new Color3f(0.7f, .15f, .15f); Color3f black = new Color3f(0.0f, 0.0f, 0.0f); Vector3f direction = new Vector3f(4.0f,-7.0f, -12.0f); BoundingSphere bounds = new BoundingSphere(new Point3d(0.0,0.0,0.0),300.0);
AmbientLight ambientLightNode = new AmbientLight(white); ambientLightNode.setInfluencingBounds(bounds); group.addChild(ambientLightNode);
Transform3D rot = new Transform3D(); rot.setIdentity(); rot.rotZ(Math.toRadians(90)); TransformGroup tg = new TransformGroup(rot); Appearance ap = new Appearance(); ap.setMaterial(new Material(red, black, red, white, 1.0f)); Cylinder RedAxis = new Cylinder(0.01f, 1000.0f, ap); tg.addChild(RedAxis); group.addChild(tg);
BranchGroup text1 = Text3DTest.text("alpha", new Point3f(-5.0f, 0.0f, 15.0f)); BranchGroup text2 = Text3DTest.text("bravo", new Point3f(-5.0f, 0.0f, 0.0f)); BranchGroup text3 = Text3DTest.text("charlie", new Point3f(-5.0f, 0.0f, -15.0f)); group.addChild(text1); group.addChild(text2); group.addChild(text3);
BranchGroup text10 = Text3DTest.text("alpha", new Point3f(-5.0f, 5.0f, 15.0f)); BranchGroup text20 = Text3DTest.text("bravo", new Point3f(-5.0f, 5.0f, 0.0f)); BranchGroup text30 = Text3DTest.text("charlie", new Point3f(-5.0f, 5.0f, -15.0f)); group.addChild(text10); group.addChild(text20); group.addChild(text30);
BranchGroup text101 = Text3DTest.text("alpha", new Point3f(-5.0f, -5.0f, 15.0f)); BranchGroup text201 = Text3DTest.text("bravo", new Point3f(-5.0f, -5.0f, 0.0f)); BranchGroup text301 = Text3DTest.text("charlie", new Point3f(-5.0f, -5.0f, -15.0f)); group.addChild(text101); group.addChild(text201); group.addChild(text301);
BranchGroup cylinder1 = Text3DTest.cylinder(green, 0.0f, 0.0f, 10.0f); BranchGroup cylinder2 = Text3DTest.cylinder(red, 0.0f, 0.0f, 0.0f); BranchGroup cylinder3 = Text3DTest.cylinder(white, 0.0f, 0.0f, -10.0f); group.addChild(cylinder1); group.addChild(cylinder2); group.addChild(cylinder3);
JFrame f = new JFrame(); GraphicsConfiguration gc = SimpleUniverse.getPreferredConfiguration(); Canvas3D canvas = new Canvas3D(gc); f.add(new JPanel().add("Center",canvas));
SimpleUniverse universe = new SimpleUniverse(canvas); OrbitBehavior orbit = new OrbitBehavior(canvas, OrbitBehavior.REVERSE_ALL); orbit.setSchedulingBounds(bounds); ViewingPlatform vp = universe.getViewingPlatform(); vp.setViewPlatformBehavior(orbit); vp = universe.getViewingPlatform(); vp.getViewPlatform().setActivationRadius(1000.0f); TransformGroup steerTG = vp.getViewPlatformTransform(); Transform3D t3d = new Transform3D( ); steerTG.getTransform( t3d ); Point3d viewerInitPos = new Point3d(0, 0, -20); Point3d viewerLooksAt = new Point3d(0, 0, 0); Vector3d upDirection = new Vector3d(0,1,0); t3d.lookAt( viewerInitPos, viewerLooksAt, upDirection); t3d.invert(); steerTG.setTransform(t3d);
universe.addBranchGraph(group);
f.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE); f.setPreferredSize(new Dimension(600,600)); f.pack(); f.setVisible(true); } private static BranchGroup text(String text, Point3f pos) { BranchGroup textBG = new BranchGroup(); Font3D font3d = new Font3D(new Font("Helvetica", Font.PLAIN, 2), new FontExtrusion()); Text3D textGeom = new Text3D(font3d, text, pos); Shape3D textShape = new Shape3D(textGeom);
Color3f red = new Color3f(0.7f, .15f, .15f); Color3f black = new Color3f(0.0f, 0.0f, 0.0f); Color3f white = new Color3f(1.0f, 1.0f, 1.0f); Appearance redAp = new Appearance(); redAp.setMaterial(new Material(red, black, red, white, 1.0f)); textShape.setAppearance(redAp); textBG.addChild(textShape); return textBG; } private static BranchGroup cylinder(Color3f color, float x, float y, float z) { Color3f blue = new Color3f(0.0f, 0.0f, 1.0f); Color3f white = new Color3f(1.0f, 1.0f, 1.0f); Color3f red = new Color3f(0.7f, .15f, .15f); Color3f black = new Color3f(0.0f, 0.0f, 0.0f);
BranchGroup bg = new BranchGroup(); Appearance ap2 = new Appearance(); ap2.setMaterial(new Material(color, black, color, white, 1.0f));
Cylinder cylinder = new Cylinder(0.5f, 1.0f, ap2); Transform3D transform = new Transform3D(); Vector3f vector = new Vector3f(x, y, z); transform.set(vector); TransformGroup tg = new TransformGroup(transform); tg.addChild(cylinder); bg.addChild(tg); return bg; } public static void main(String[] args ) { new Text3DTest(); } } |