This discussion is archived
2 Replies Latest reply: Mar 5, 2013 12:23 AM by 994761 RSS

Mouse event after PerspectiveTransform

994761 Newbie
Currently Being Moderated
Hello,
The javadoc of PerspectiveTransform has some words like this:

"Note that this effect does not adjust the coordinates of input events or any methods that measure containment on a Node. The results of mouse picking and the containment methods are undefined when a Node has a PerspectiveTransform effect in place. "

Is there anyway to adjust the coordinates of input events after PerspectiveTransform? Our application need transform a image, and we set a mouse click listener on the image, after PerspectiveTransform, the mouse click area is still the old image area! Please help me!

Edited by: 991758 on 2013-3-4 下午6:22
  • 1. Re: Mouse event after PerspectiveTransform
    shakir.gusaroff Expert
    Currently Being Moderated
    Hi. In the following example I have created a polygon with the exact coordinates of the perspective transform. The polygon is not a part of the view.
    When you click the image after transforming, it checks if the mouse coordinates are inside the polygon. If so it means you clicked the transformed image.
    The following works for me:
    import javafx.application.Application;
    import javafx.event.ActionEvent;
    import javafx.event.EventHandler;
    import javafx.scene.Group;
    import javafx.scene.Scene;
    import javafx.scene.control.Button;
    import javafx.scene.effect.PerspectiveTransform;
    import javafx.scene.input.MouseEvent;
    import javafx.scene.layout.StackPane;
    import javafx.scene.paint.Color;
    import javafx.scene.shape.Polygon;
    import javafx.scene.shape.Rectangle;
    import javafx.scene.text.Font;
    import javafx.scene.text.FontWeight;
    import javafx.scene.text.Text;
    import javafx.stage.Stage;
    
    public class PerspectiveTransformSample extends Application {
    
        @Override
        public void start(Stage primaryStage) {
            final PerspectiveTransform perspectiveTrasform = new PerspectiveTransform();
            perspectiveTrasform.setUlx(10.0);
            perspectiveTrasform.setUly(10.0);
            perspectiveTrasform.setUrx(310.0);
            perspectiveTrasform.setUry(40.0);
            perspectiveTrasform.setLrx(310.0);
            perspectiveTrasform.setLry(60.0);
            perspectiveTrasform.setLlx(10.0);
            perspectiveTrasform.setLly(90.0);
    
            final Polygon polygon = createPolygon(perspectiveTrasform);
    
            Group g = new Group();
            g.setEffect(perspectiveTrasform);
            g.setCache(true);
    
            Rectangle rect = new Rectangle();
            rect.setX(10.0);
            rect.setY(10.0);
            rect.setWidth(280.0);
            rect.setHeight(80.0);
            rect.setFill(Color.web("0x3b596d"));
    
            Text text = new Text();
            text.setX(20.0);
            text.setY(65.0);
            text.setText("Perspective");
            text.setFill(Color.ALICEBLUE);
            text.setFont(Font.font(null, FontWeight.BOLD, 36));
            g.getChildren().addAll(rect, text);
    
            g.setOnMouseClicked(new EventHandler<MouseEvent>() {
                @Override
                public void handle(MouseEvent event) {
    
                    if (polygon.contains(event.getX(), event.getY())) {
                        System.out.println("Click");
                    }
    
                }
            });
            Scene scene = new Scene(g, 300, 250);
            primaryStage.setScene(scene);
            primaryStage.show();
        }
    
        public Polygon createPolygon(PerspectiveTransform perspectiveTrasform) {
            double ulx = perspectiveTrasform.getUlx();
            double uly = perspectiveTrasform.getUly();
            double urx = perspectiveTrasform.getUrx();
            double ury = perspectiveTrasform.getUry();
            double lrx = perspectiveTrasform.getLrx();
            double lry = perspectiveTrasform.getLry();
            double llx = perspectiveTrasform.getLlx();
            double lly = perspectiveTrasform.getLly();
    
            final Polygon polygon = new Polygon();
    
            polygon.getPoints().addAll(new Double[]{
                        ulx, uly,
                        urx, ury,
                        lrx, lry,
                        llx, lly});
            return polygon;
    
        }
    
        public static void main(String[] args) {
            launch(args);
        }
    }
  • 2. Re: Mouse event after PerspectiveTransform
    994761 Newbie
    Currently Being Moderated
    Thanks, but how about set a PerspectiveTransform on a group, the group contains some images, and each image has its own mouse event listener?

    Edited by: 991758 on 2013-3-5 上午12:22

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points