通过拖动

时间:2016-01-08 20:51:39

标签: java javafx transform drag perspective

我遇到了javafx和透视变换效果的问题。我想能够拖动矩形的一个边缘,并能够拉动该边缘。到此为止没有问题。但是我也希望通过我移动的点来变换矩形,这样点(边缘)将表示该特殊边缘的透视变换效果的位置。我希望你明白我的意思。我在谷歌搜索了很多,并尝试了一些不同的解决方案,但它没有奏效。所以这里是我的矩形的代码以及点(每个鼠标可以移动):

static double orgSceneX, orgSceneY;
    static double orgTranslateX, orgTranslateY;

    private static int c_X_off = 0;
    private static int c_Y_off = 0;
    private static int c1_X_off = 0;
    private static int c1_Y_off = 0;
    private static int c2_X_off = 0;
    private static int c2_Y_off = 0;
    private static int c3_X_off = 0;
    private static int c3_Y_off = 0;

    public static void addRectangle(int X, int Y, int WIDTH, int HEIGHT) {
        Rectangle r = new Rectangle();
        r.setX(X);
        r.setY(Y);
        r.setWidth(WIDTH);
        r.setHeight(HEIGHT);
        r.setCursor(Cursor.MOVE);
        r.setFill(Paint.valueOf("RED"));

        Rectangle r2 = new Rectangle();
        r2.setX(X/2);
        r2.setY(Y/2);
        r2.setWidth(WIDTH/2);
        r2.setHeight(HEIGHT/2);
        r2.setCursor(Cursor.MOVE);
        r2.setStroke(Color.BLUE);
        r2.setStrokeWidth(2);
        r2.setFill(Paint.valueOf("RED"));

        final PerspectiveTransform pt = new PerspectiveTransform();
        r2.setEffect(pt);

        Circle c = new Circle();
        c.setFill(Color.BLUE); c.setRadius(6); c.setCursor(Cursor.NW_RESIZE);
        c.setTranslateX(r2.getTranslateX()-WIDTH/4); 
        c.setTranslateY(r2.getTranslateY()-HEIGHT/4);

        Circle c1 = new Circle();
        c1.setFill(Color.BLUE); c1.setRadius(6); c1.setCursor(Cursor.NE_RESIZE);
        c1.setTranslateX(r2.getTranslateX()+WIDTH/4); 
        c1.setTranslateY(r2.getTranslateY()-HEIGHT/4);

        Circle c2 = new Circle();
        c2.setFill(Color.BLUE); c2.setRadius(6); c2.setCursor(Cursor.SE_RESIZE);
        c2.setTranslateX(r2.getTranslateX()+WIDTH/4); 
        c2.setTranslateY(r2.getTranslateY()+HEIGHT/4);

        Circle c3 = new Circle();
        c3.setFill(Color.BLUE);c3.setRadius(6); c3.setCursor(Cursor.SW_RESIZE);
        c3.setTranslateX(r2.getTranslateX()-WIDTH/4); 
        c3.setTranslateY(r2.getTranslateY()+HEIGHT/4);



        EventHandler<MouseEvent> circleOnMousePressedEventHandler = 
            new EventHandler<MouseEvent>() {

            @Override
            public void handle(MouseEvent t) {
                orgSceneX = t.getSceneX();
                orgSceneY = t.getSceneY();
                orgTranslateX = ((Rectangle)(t.getSource())).getTranslateX();
                orgTranslateY = ((Rectangle)(t.getSource())).getTranslateY();
            }
        };

        EventHandler<MouseEvent> circleOnMouseDraggedEventHandler = 
            new EventHandler<MouseEvent>() {

            @Override
            public void handle(MouseEvent t) {
                double offsetX = t.getSceneX() - orgSceneX;
                double offsetY = t.getSceneY() - orgSceneY;
                double newTranslateX = orgTranslateX + offsetX;
                double newTranslateY = orgTranslateY + offsetY;

                ((Rectangle)(t.getSource())).setTranslateX(newTranslateX);
                ((Rectangle)(t.getSource())).setTranslateY(newTranslateY);
                r.setTranslateX(newTranslateX * 2);
                r.setTranslateY(newTranslateY * 2);
                r.setX(newTranslateX * 2);
                r.setY(newTranslateY * 2);

                c.setTranslateX(r2.getTranslateX()-WIDTH/4+c_X_off); 
                c.setTranslateY(r2.getTranslateY()-HEIGHT/4+c_Y_off);

                c1.setTranslateX(r2.getTranslateX()+WIDTH/4+c1_X_off); 
                c1.setTranslateY(r2.getTranslateY()-HEIGHT/4+c1_Y_off);

                c2.setTranslateX(r2.getTranslateX()+WIDTH/4+c2_X_off); 
                c2.setTranslateY(r2.getTranslateY()+HEIGHT/4+c2_Y_off);

                c3.setTranslateX(r2.getTranslateX()-WIDTH/4+c3_X_off); 
                c3.setTranslateY(r2.getTranslateY()+HEIGHT/4+c3_Y_off);
            }
        };

        EventHandler<MouseEvent> circleOnMousePressedEventHandler2 = 
            new EventHandler<MouseEvent>() {

            @Override
            public void handle(MouseEvent t) {
                orgSceneX = t.getSceneX();
                orgSceneY = t.getSceneY();
                orgTranslateX = ((Circle)(t.getSource())).getTranslateX();
                orgTranslateY = ((Circle)(t.getSource())).getTranslateY();
            }
        };

        EventHandler<MouseEvent> circleOnMouseDraggedEventHandler2 = 
            new EventHandler<MouseEvent>() {

            @Override
            public void handle(MouseEvent t) {
                double offsetX = t.getSceneX() - orgSceneX;
                double offsetY = t.getSceneY() - orgSceneY;
                double newTranslateX = orgTranslateX + offsetX;
                double newTranslateY = orgTranslateY + offsetY;

                ((Circle)(t.getSource())).setTranslateX(newTranslateX);
                ((Circle)(t.getSource())).setTranslateY(newTranslateY);

                if(t.getSource().equals(c)) {
                    c_X_off = (int) offsetX;
                    c_Y_off = (int) offsetY;
                } else

                if(t.getSource().equals(c1)) {
                    c1_X_off = (int) offsetX;
                    c1_Y_off = (int) offsetY;
                } else

                if(t.getSource().equals(c2)) {
                    c2_X_off = (int) offsetX;
                    c2_Y_off = (int) offsetY;
                } else

                if(t.getSource().equals(c3)) {
                    c3_X_off = (int) offsetX;
                    c3_Y_off = (int) offsetY;
                }

                pt.setUlx(5);
                pt.setUly(5);
                pt.setUrx(5);
                pt.setUry(5);
                pt.setLrx(5);
                pt.setLry(5);
                pt.setLlx(5);
                pt.setLly(5);
            }
        };

        r2.setOnMousePressed(circleOnMousePressedEventHandler);
        r2.setOnMouseDragged(circleOnMouseDraggedEventHandler);

        c.setOnMousePressed(circleOnMousePressedEventHandler2);
        c.setOnMouseDragged(circleOnMouseDraggedEventHandler2);
        c1.setOnMousePressed(circleOnMousePressedEventHandler2);
        c1.setOnMouseDragged(circleOnMouseDraggedEventHandler2);
        c2.setOnMousePressed(circleOnMousePressedEventHandler2);
        c2.setOnMouseDragged(circleOnMouseDraggedEventHandler2);
        c3.setOnMousePressed(circleOnMousePressedEventHandler2);
        c3.setOnMouseDragged(circleOnMouseDraggedEventHandler2);

        PrjMapper.addObject(r);

        PrjGui.root.getChildren().add(r2);
        PrjGui.root.getChildren().add(c);
        PrjGui.root.getChildren().add(c1);
        PrjGui.root.getChildren().add(c2);
        PrjGui.root.getChildren().add(c3);
        PrjGui.scene.setRoot(PrjGui.root);
    }

矩形和圆圈被添加到我的gui和显示器的StackPane中。 为什么? 我正在尝试为VideoMapping创建一个软件。如果您需要更多代码,请告诉我! 非常感谢!

Paulee

0 个答案:

没有答案