import java.util.LinkedList; import java.util.List; import javafx.application.Application; import javafx.event.EventHandler; import javafx.scene.Camera; import javafx.scene.Group; import javafx.scene.PerspectiveCamera; import javafx.scene.Scene; import javafx.scene.input.MouseEvent; import javafx.scene.input.PickResult; import javafx.scene.light.PointLight; import javafx.scene.material.PhongMaterial; import javafx.scene.paint.Color; import javafx.scene.shape3d.Box; import javafx.scene.shape3d.Cylinder; import javafx.scene.shape3d.Shape3D; import javafx.scene.shape3d.Sphere; import javafx.stage.Stage; /* * Copyright (c) 2012, Oracle and/or its affiliates. All rights reserved. * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. */ /** * * @author Andrew Glushchenko */ public class PickingTestApp extends Application { static { System.setProperty("prism.dirtyopts", "false"); } private static int HEIGHT = 500; private static int WIDTH = 500; private Scene scene; private Group root; private PointLight pl; protected Group grp; private Shape3D meshView[]; private PhongMaterial material; private static double SCALE = 100; @Override public void start(Stage stage) throws Exception { pl = new PointLight(Color.RED); pl.setTranslateZ(-800); root = new Group(pl, buildGroup()); scene = new Scene(root, HEIGHT, WIDTH); addCamera(scene); scene.addEventHandler(MouseEvent.MOUSE_CLICKED, new EventHandler() { @Override public void handle(MouseEvent t) { pick(t); } }); stage.setScene(scene); stage.show(); } private Camera addCamera(Scene scene) { PerspectiveCamera pc = new PerspectiveCamera(false); scene.setCamera(pc); pc.setTranslateX(-200); pc.setTranslateY(-200); pc.setTranslateZ(-1000); return pc; } private void pick(MouseEvent t) { PickResult result = t.getPickResult(); System.out.println("Point:(x="+t.getSceneX()+";y="+t.getSceneY()+")"); if (result != null) { System.out.println(result.getIntersectedDistance()); System.out.println(result.getIntersectedFace()); System.out.println(result.getIntersectedNode()); System.out.println(result.getIntersectedPoint()); System.out.println(result.getIntersectedTexCoord()); } else { System.out.println("null"); } } protected Group buildGroup() { material = new PhongMaterial(); material.setDiffuseColor(Color.LIGHTGRAY); material.setSpecularColor(Color.rgb(30, 30, 30)); meshView = new Shape3D[]{ new Box(), new Sphere(), new Cylinder() }; for (int i = 0; i < 3; i++) { meshView[i].setMaterial(material); meshView[i].setTranslateZ(20); meshView[i].setScaleX(SCALE); meshView[i].setScaleY(SCALE); meshView[i].setScaleZ(SCALE); } meshView[0].setTranslateX(3 * SCALE); meshView[2].setTranslateX(-3 * SCALE); grp = new Group(meshView); return grp; } public static void main(String[] args) { launch(args); } }