`
msh24
  • 浏览: 57719 次
  • 性别: Icon_minigender_1
  • 来自: 山西
文章分类
社区版块
存档分类
最新评论

java3D 箭头

阅读更多
public class Pos extends Applet {

	private static final long serialVersionUID = 1L;
	
	float knot[] = { 0.0f,  1.0f };
	Color3f color = new Color3f(0.0f, 0.0f, 1.0f);
	Color3f c[] = { color,  color };

	private BranchGroup createSceneGraph() {
		BranchGroup objRoot = new BranchGroup();
		BoundingSphere bound = new BoundingSphere(new Point3d(0.0, 0.0, 0.0), 50.);
		
		Color3f color = new Color3f(0f, 0f, 255f);
		
		Point3f pos[] = { 
				new Point3f(-1f, 0.4f, 0.0f), 
				new Point3f(1f, 0.4f, 0.0f),
			};
		Point3f pos1[] = { 
				new Point3f(-1f, -0.4f, 0.0f), 
				new Point3f(1f, -0.4f, 0.0f),
			};
		Point3f pos2[] = { 
				new Point3f(-3f, 0.4f, 0.0f), 
				new Point3f(-1.f, 0.4f, 0.0f),
			};
		Point3f pos3[] = { 
				new Point3f(-3f, -0.4f, 0.0f), 
				new Point3f(-1.f, -0.4f, 0.0f),
			};
		Point3f pos4[] = { 
				new Point3f(1f, 0.4f, 0.0f), 
				new Point3f(3f, 0.4f, 0.0f),
			};
		Point3f pos5[] = { 
				new Point3f(1f, -0.4f, 0.0f), 
				new Point3f(3f, -0.4f, 0.0f),
			};
		
		draw(objRoot, bound, 0d, 0d, 170d, color, pos,  2000, 0);
		draw(objRoot, bound, 0d, 0d, 170d, color, pos1, 2000, 0);
		draw(objRoot, bound, 0d, 0d, 170d, color, pos2, 2000, 0);
		draw(objRoot, bound, 0d, 0d, 170d, color, pos3, 2000, 0);
		draw(objRoot, bound, 0d, 0d, 170d, color, pos4, 2000, 0);
		draw(objRoot, bound, 0d, 0d, 170d, color, pos5, 2000, 0);
		
		
//		draw(objRoot, bound1, Math.PI/3, Math.PI/3, Math.PI/3, color, 1.f, 3.f, rotate, rotate2, 2000, 0);

		Color3f bgColor = new Color3f(1.0f, 1.0f, 1.0f);
		Background bg = new Background(bgColor);
		bg.setApplicationBounds(bound);
		
		objRoot.addChild(bg);
		
		return objRoot;
	}

	public Pos() {
		setLayout(new BorderLayout());
		Canvas3D c = new Canvas3D(SimpleUniverse.getPreferredConfiguration());
		add("Center", c);
		Viewer viewer = new Viewer(c);
		Vector3d viewpoint = new Vector3d(0.0, 0.0, 10.0);

		Transform3D t = new Transform3D();
		t.set(viewpoint);
		ViewingPlatform v = new ViewingPlatform();
		v.getViewPlatformTransform().setTransform(t);

		BranchGroup scene = createSceneGraph();
		SimpleUniverse u = new SimpleUniverse(v, viewer);
		u.getViewingPlatform();
		u.addBranchGraph(scene);
	}
	
	/**
	 * 
	 * @param objRoot
	 * @param bound
	 * @param directX 箭头方向
	 * @param directY
	 * @param directZ
	 * @param color3f 箭头颜色
	 * @param pos 运动轨迹
	 * @param forward 正向时间
	 * @param reverse 逆向时间
	 */
	
	public void draw(BranchGroup objRoot, BoundingSphere bound, Double directX, Double directY, Double directZ,
			Color3f color3f, Point3f pos[],
			long forward, long reverse) {
		
		Transform3D rotate = new Transform3D();
		rotate.rotY(Math.toRadians(directY));
		rotate.rotX(Math.toRadians(directX));
		rotate.rotZ(Math.toRadians(directZ));
		rotate.setScale(1.0f);
		rotate.setTranslation(new Vector3f(0.0f, 0.0f, 0.0f));
		
		//渲染外围动画路径
		Transform3D tr = new Transform3D();
//		tr.rotX(Math.PI/4);
//		tr.rotZ(Math.PI/4);
		
		tr.rotX((90+directX)*Math.PI/180);
		tr.rotY((90+directY)*Math.PI/180);
		tr.rotZ((90+directZ)*Math.PI/180);
		tr.setTranslation(new Vector3f(10f, 5.f, 5.0f));
		
		TransformGroup group = new TransformGroup(tr);
		group.setCapability(TransformGroup.ALLOW_TRANSFORM_WRITE);
		
		//渲染箭头
		Appearance app = new Appearance();
		Material material = new Material();
		//颜色
		material.setEmissiveColor(color3f);
		app.setMaterial(material);
		
		TransformGroup transformGroup = new TransformGroup();
		transformGroup.setTransform(rotate);
		
		
		TransformGroup transformGroup1 = new TransformGroup();
		transformGroup1.addChild(new Cone(0.1f, 0.4f, 3, app));
		
		Transform3D rotate2 = new Transform3D();
		rotate2.setScale(1.0f);
		rotate2.setTranslation(new Vector3f(0.0f, -0.2f, 0.0f));
		TransformGroup transformGroup2 = new TransformGroup();
		transformGroup2.setTransform(rotate2);
		transformGroup2.addChild(new Cylinder(0.01f, 0.6f, 3, app));
		
		transformGroup.addChild(transformGroup1);
		transformGroup.addChild(transformGroup2);
		
		group.addChild(transformGroup);
		Alpha xtranAlpha = new Alpha(-1, Alpha.INCREASING_ENABLE | Alpha.DECREASING_ENABLE, 100, 100, forward, 100, 200, reverse, 100, 0);
		PositionPathInterpolator tran = new PositionPathInterpolator(xtranAlpha, group, tr,  knot, pos);
		tran.setSchedulingBounds(bound);
		
		objRoot.addChild(group);
		group.addChild(tran);
	}

	
	public static void main(String[] args) {
		new MainFrame(new Pos(), 400, 400);
	}
}



用锥形和圆柱体 组成的箭头。
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics