import java.util.logging.Level; import java.util.logging.Logger; import javafx.application.Application; import javafx.event.EventHandler; import javafx.geometry.Orientation; import javafx.scene.Camera; import javafx.scene.Group; import javafx.scene.Node; import javafx.scene.PerspectiveCamera; import javafx.scene.Scene; import javafx.scene.input.KeyEvent; 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 PickingIntersectedDistanceTestApp 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(200); root = new Group(pl, buildGroup()); scene = new Scene(root, HEIGHT, WIDTH, true); root.getChildren().add(new Group(addCamera(scene))); grp.setTranslateX(200); grp.setTranslateY(200); grp.setTranslateZ(1000); 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); return pc; } private void pick(MouseEvent t) { PickResult result = t.getPickResult(); // System.out.println("Point:(x=" + t.getSceneX() + ";y=" + t.getSceneY() + ")"); // System.out.println("Source: " + t.getSource()); // System.out.println("Target: " + t.getTarget()); 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 Sphere(); meshView.setMaterial(material); // meshView.setTranslateZ(20); meshView.setScaleX(SCALE); meshView.setScaleY(SCALE); meshView.setScaleZ(SCALE); grp = new Group(meshView); return grp; } public static void main(String[] args) { launch(args); } }