Java Reference
In-Depth Information
When the user clicks the mouse on a 3D shape, it touches the 3D shape in a particular point in a particular face.
The line segment starting from the eye of the camera in effect for the Scene or SubScene and ending at the point on
the 3D shape is called the pick ray . The point on the 3D shape is called the intersected point . The PickResult provides
information about this intersected point. The getIntersectedNode() method returns the 3D shape itself, either a
Sphere , a Cylinder , a Box , or a MeshView . The getIntersectedPoint() method returns the 3D coordinates of the
intersected point. The coordinates are relative to the 3D shape's local coordinate system. For a Sphere of radius 100,
the coordinates ( x , y , z ) of the returned Point3D will satisfy x 2 + y 2 + z 2 = 100 2 , regardless of the transforms applied
to itself or any of its containing nodes. The getIntersectedDistance() method returns the distance from the eye
of the camera to the intersected point. This is the length of the pick ray in the 3D model's world coordinate system.
The getIntersectedFace() method returns the face number of the face that contains the intersected point for a
MeshView , which has user-defined faces. It returns FACE_UNDEFINED for the predefined 3D shapes Sphere , Cylinder ,
and Box . The getIntersectedTexCoord() method returns the texture coordinates of the intersected point. Unlike the
getIntersectedFace() method, this method will return the texture coordinates for both user-defined and predefined
3D shapes.
The program in Listing 10-10 sets an event handler for the mouse clicked and the mouse dragged event for a
sphere, and changes the color of the sphere based on the intersected point's coordinates when you click on or drag
the mouse over the sphere.
Listing 10-10. SphereWithEvents.java
import javafx.application.Application;
import javafx.beans.property.ObjectProperty;
import javafx.beans.property.SimpleObjectProperty;
import javafx.event.EventHandler;
import javafx.geometry.Point3D;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.input.PickResult;
import javafx.scene.paint.Color;
import javafx.scene.paint.Material;
import javafx.scene.paint.PhongMaterial;
import javafx.scene.shape.Sphere;
import javafx.stage.Stage;
import static java.lang.Math.abs;
import static java.lang.Math.min;
public class SphereWithEvents extends Application {
private Model model;
private View view;
public SphereWithEvents() {
model = new Model();
}
@Override
public void start(Stage primaryStage) throws Exception {
view = new View(model);
primaryStage.setTitle("Sphere with MouseEvents");
primaryStage.setScene(view.scene);
primaryStage.show();
}
 
Search WWH ::




Custom Search