in Programming

Java 3D Programming: Mouse events and primitives

This code shows a simple example about using primitives and mouse events in Java 3D.

 

public class EjemploJava3D extends javax.swing.JFrame{

	



	
private com.sun.j3d.utils.universe.SimpleUniverse simpleUniverse;
	private javax.media.j3d.Canvas3D canvas3D;
	private javax.media.j3d.BranchGroup bg;
	private com.sun.j3d.utils.behaviors.mouse.MouseRotate mr;
	private com.sun.j3d.utils.behaviors.mouse.MouseTranslate mt;
	private com.sun.j3d.utils.behaviors.mouse.MouseZoom mz;
	
	
	public EjemploJava3D(){
		super();
		setLayout(new java.awt.BorderLayout());
		canvas3D=new javax.media.j3d.Canvas3D(com.sun.j3d.utils.universe.SimpleUniverse.getPreferredConfiguration());
		simpleUniverse=new com.sun.j3d.utils.universe.SimpleUniverse(canvas3D);
		bg=new javax.media.j3d.BranchGroup();
		javax.media.j3d.TransformGroup tg=new javax.media.j3d.TransformGroup();
		tg.setCapability(javax.media.j3d.TransformGroup.ALLOW_TRANSFORM_WRITE);
		tg.setCapability(javax.media.j3d.TransformGroup.ALLOW_TRANSFORM_READ);
// 		tg.addChild(new com.sun.j3d.utils.geometry.ColorCube(1.3f));
		
		javax.media.j3d.Appearance appearance1=new javax.media.j3d.Appearance();
		javax.media.j3d.ColoringAttributes ca=new javax.media.j3d.ColoringAttributes();
		ca.setColor(new javax.vecmath.Color3f(0.7f,0.7f,0.f));
		appearance1.setColoringAttributes(ca);
		javax.media.j3d.TransparencyAttributes ta=
		new javax.media.j3d.TransparencyAttributes();
		ta.setTransparency(0.5f);
		ta.setTransparencyMode(javax.media.j3d.TransparencyAttributes.NICEST);//FASTEST
		appearance1.setTransparencyAttributes(ta);
		com.sun.j3d.utils.geometry.Cylinder cylinder=new com.sun.j3d.utils.geometry.Cylinder(1.2f,0.3f);
		cylinder.setAppearance(appearance1);
 		tg.addChild(cylinder);
		com.sun.j3d.utils.geometry.Cylinder cylinderL=new com.sun.j3d.utils.geometry.Cylinder(1.2f,0.3f);
		javax.media.j3d.Appearance appearance2=new
		javax.media.j3d.Appearance();
		javax.media.j3d.PolygonAttributes pa=
		new javax.media.j3d.PolygonAttributes();
		pa.setPolygonMode(javax.media.j3d.PolygonAttributes.POLYGON_LINE);//POLYGON_FILL, POLYGON_POINT
		pa.setCullFace(javax.media.j3d.PolygonAttributes.CULL_NONE);//CULL_FRONT, CULL_NONE
		appearance2.setPolygonAttributes(pa);
		cylinderL.setAppearance(appearance2);
		tg.addChild(cylinderL);
		
		javax.media.j3d.BoundingSphere bs=new javax.media.j3d.BoundingSphere();
		mr=new com.sun.j3d.utils.behaviors.mouse.MouseRotate(tg);
		mz=new com.sun.j3d.utils.behaviors.mouse.MouseZoom(tg);
		mt=new com.sun.j3d.utils.behaviors.mouse.MouseTranslate(tg);
		mr.setSchedulingBounds(bs);
		mz.setSchedulingBounds(bs);
		mt.setSchedulingBounds(bs);
		bg.addChild(mr);
		bg.addChild(mt);
		bg.addChild(mz);
		simpleUniverse.getViewingPlatform().setNominalViewingTransform();
		bg.addChild(tg);
		bg.compile();
		simpleUniverse.addBranchGraph(bg);
		add(canvas3D);
		setSize(300,300);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
	}
	
	public static void main(String[]args){
		new EjemploJava3D().setVisible(true);
	}
	
}

 

Write a Comment

Comment