package elte.java2_utikalauz5.java3d; import java.awt.*; import java.awt.event.*; import java.util.Enumeration; import javax.vecmath.*; import javax.media.j3d.*; /** 3D kiválasztás egérkattintással. @link.forrásfájl {@docRoot}/../data/java3d/src PickingDemo.java @link.letöltés {@docRoot}/../data/java3d PickingDemo.jar @since Java 2 Útikalauz programozóknak 5.0 */ class PickingDemo extends Frame { /** Verziószám */ private final static long serialVersionUID = 15L; public static void main(String[] args) { new PickingDemo(); } public PickingDemo() { super("PickingDemo - Press the mouse"); enableEvents(java.awt.AWTEvent.WINDOW_EVENT_MASK); GraphicsConfiguration config = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice().getBestConfiguration(new GraphicsConfigTemplate3D()); Canvas3D c = new Canvas3D(config); add(c); UniverseBuilder u = new UniverseBuilder(c); BranchGroup scene = createSceneGraph(c); u.addBranchGraph(scene); setSize(300,270); setVisible(true); } /** Ablakesemény feldolgozása, bezáráskor kilépés. @param esemény A fellépett esemény */ @Override protected void processWindowEvent(java.awt.event.WindowEvent esemény) { if (esemény.getID() == esemény.WINDOW_CLOSING) System.exit(0); super.processWindowEvent( esemény ); } public BranchGroup createSceneGraph(Canvas3D canvas3D) { BranchGroup branchGroup = new BranchGroup(); Transform3D transform3D1 = new Transform3D(); Transform3D transform3D2 = new Transform3D(); transform3D1.set(1.0f,new Vector3f(0.0f,1.6f,0.0f)); transform3D2.set(1.0f,new Vector3f(0.0f,0.2f,0.0f)); TransformGroup transformGroup1 = new TransformGroup(transform3D1); TransformGroup transformGroup2 = new TransformGroup(transform3D2); Font3D font3d = new Font3D(new Font("TestFont", Font.PLAIN, 1),new FontExtrusion()); Appearance appearance1 = new Appearance(); Material material1 = new Material(); material1.setAmbientColor(new Color3f(1.0f,0.0f,0.0f)); material1.setDiffuseColor(new Color3f(0.5f,0.0f,0.0f)); appearance1.setMaterial(material1); Text3D text3D1 = new Text3D(font3d, new String("CLICK1"),new Point3f(0.0f, -1.0f, 0.0f),Text3D.ALIGN_CENTER,Text3D.PATH_RIGHT); Shape3D shape3D1 = new Shape3D(text3D1,appearance1); shape3D1.setCapability(Shape3D.ALLOW_APPEARANCE_READ); appearance1.setCapability(Appearance.ALLOW_MATERIAL_READ); material1.setCapability(Material.ALLOW_COMPONENT_READ); material1.setCapability(Material.ALLOW_COMPONENT_WRITE); Appearance appearance2 = new Appearance(); Material material2 = new Material(); material2.setAmbientColor(new Color3f(0.0f,1.0f,0.0f)); material2.setDiffuseColor(new Color3f(0.5f,1.0f,0.0f)); appearance2.setMaterial(material2); Text3D text3D2 = new Text3D(font3d, new String("CLICK2"),new Point3f(0.0f, -1.0f, 0.0f),Text3D.ALIGN_CENTER,Text3D.PATH_RIGHT); Shape3D shape3D2 = new Shape3D(text3D2,appearance2); shape3D2.setCapability(Shape3D.ALLOW_APPEARANCE_READ); appearance2.setCapability(Appearance.ALLOW_MATERIAL_READ); material2.setCapability(Material.ALLOW_COMPONENT_READ); material2.setCapability(Material.ALLOW_COMPONENT_WRITE); PickBehavior pickBehavior = new PickBehavior(branchGroup,canvas3D,shape3D1,shape3D2); BoundingSphere bounds = new BoundingSphere(new Point3d(0.0,0.0,0.0), 100.0); pickBehavior.setSchedulingBounds(bounds); AmbientLight ambientLight = new AmbientLight(new Color3f(0.6f,0.6f,0.6f)); ambientLight.setInfluencingBounds(bounds); DirectionalLight directionalLight = new DirectionalLight(new Color3f(1.0f,1.0f,1.0f),new Vector3f(0.0f,0.0f,-1.0f)); directionalLight.setInfluencingBounds(bounds); transformGroup1.addChild(shape3D1); transformGroup2.addChild(shape3D2); branchGroup.addChild(transformGroup1); branchGroup.addChild(transformGroup2); branchGroup.addChild(ambientLight); branchGroup.addChild(directionalLight); branchGroup.addChild(pickBehavior); return branchGroup; } class UniverseBuilder { Locale locale; UniverseBuilder(Canvas3D c) { Transform3D t = new Transform3D(); Transform3D t2 = new Transform3D(); t2.setEuler( new Vector3d(-30.0*(Math.PI/180.0),22.5*(Math.PI/180.0),0.0)); t.set(4,new Vector3d(2.0,3.0,4.0)); t.mul(t,t2); VirtualUniverse universe = new VirtualUniverse(); locale = new Locale(universe); PhysicalBody body = new PhysicalBody(); PhysicalEnvironment environment = new PhysicalEnvironment(); BranchGroup viewPlatformBranchGroup = new BranchGroup(); TransformGroup viewPlatformTransformGroup = new TransformGroup(t); ViewPlatform viewPlatform = new ViewPlatform(); View view = new View(); view.addCanvas3D(c); view.setPhysicalBody(body); view.setPhysicalEnvironment(environment); view.attachViewPlatform(viewPlatform); viewPlatformTransformGroup.addChild(viewPlatform); viewPlatformBranchGroup.addChild(viewPlatformTransformGroup); locale.addBranchGraph(viewPlatformBranchGroup); } void addBranchGraph(BranchGroup bg) { locale.addBranchGraph(bg); } } class PickBehavior extends Behavior { WakeupCriterion mouseCriterion; BranchGroup branchGroup; Canvas3D canvas3D; Shape3D shape3D1, shape3D2; public PickBehavior(BranchGroup branchGroup, Canvas3D canvas3D, Shape3D shape3D1, Shape3D shape3D2) { this.branchGroup = branchGroup; this.canvas3D = canvas3D; this.shape3D1 = shape3D1; this.shape3D2 = shape3D2; } public void initialize() { mouseCriterion = new WakeupOnAWTEvent(MouseEvent.MOUSE_PRESSED); System.out.println("Initialize"); wakeupOn (mouseCriterion); } public void processStimulus (Enumeration criteria) { AWTEvent[] event = ((WakeupOnAWTEvent)criteria.nextElement()).getAWTEvent(); for (int i=0; i