调整javafx的大小不会缩放它们的形状

时间:2018-03-12 20:53:15

标签: java javafx geometry javafx-8

当你按下 ctrl + T 时,我不想创建类似于photoshop的效果,它会在对象周围产生帧并调整大小但是我只想更改所包含的对象的位置没有缩放他们到目前为止创建的工作但是运动被激活了我无法弄清楚我的代码有什么问题我只是尝试使用西北处理程序 提前致谢 这里的图片说明了我想要的 第一个图像是原始组,蓝色圆圈是控制器 enter image description here 第二个是通过拖动西北部来调整组的大小后的组 但组件也会缩放 enter image description here 第三个正是我想要实现的只改变位置而不改变规模 enter image description here

//this creates the bounding box

public static void cr_resizer(Group root,double x,double y,double X,double Y) {
    Rectangle major=new Rectangle(x-10,y-10,X-x+20,Y-y+20);
    major.setFill(Color.TRANSPARENT);
    major.setId("major");
    major.setStrokeWidth(3);
    major.setStroke(Color.BLUE);
    major.setDisable(true);
    Circle NW=new Circle(x-10,y-10,3);
    Circle N=new Circle(((x+X)/2),y-10,3);
    Circle NE=new Circle(X+10,y-10,3);
    Circle W=new Circle(x-10,(Y+y)/2,3);
    Circle E=new Circle(X+10,(Y+y)/2,3);
    Circle SE=new Circle(X+10,Y+10,3);
    Circle S=new Circle(((x+X)/2),Y+10,3);
    Circle SW=new Circle(x-10,Y+10,3);
    resize_move( root,NW,SE);
    root.getChildren().addAll(major,NW,N,NE,SE,S,SW,E,W);

}
//this function calculates the calculate the distance of the mover point from the opposing point 
// to caclulate the scaling factor theen apply it to componnents of the group
public static void resize_move(Group root,Circle mover,Circle op) {
    double[]start={mover.getCenterX(),mover.getCenterY()};
    double[]end={op.getCenterX(),op.getCenterY()};
    double[]strt_length=dis2_vec_d(start, end);
    mover.setOnMouseClicked(ev->{
        difx=ev.getX();
        dify=ev.getY();
    });
    mover.setOnMouseDragged(ev->{
        double diffx=ev.getX()-difx;
        double diffy=ev.getY()-dify;
        System.out.println(diffx);
        System.out.println(diffy);
        difx=ev.getX();
        dify=ev.getY();
        mover.setCenterX(ev.getX());
        mover.setCenterY(ev.getY());
        double[] newp= {mover.getCenterX(),mover.getCenterY()};
        double[]end_length=dis2_vec_d(newp, end);
        double[]factor = {end_length[0]/strt_length[0],end_length[1]/strt_length[1]};
        for (Node nod:root.getChildren()) {
            if (nod instanceof Circle) {
                if (nod != mover) {
                    move_circ((Circle) nod,op,factor);
                }

            }
        }
    });

}
//this function relocate the position of the circle according to the position of the custom axis point
public static void move_circ(Circle cir,Circle op,double[]factor) {
double[] axis= {op.getCenterX(),op.getCenterY()};
double[] strt_point= {cir.getCenterX(),cir.getCenterY()};
double[] nstrt_point= new_cord(axis,strt_point,factor);
cir.setCenterX(nstrt_point[0]);
cir.setCenterY(nstrt_point[1]);

}
//this function scale the position of the point in relation to another point
public static double[] new_cord(double[]axis,double[]point,double[]factor) {
double[]strt_length=dis2_vec_d(point,axis);
double[]new_length= {strt_length[0]*factor[0],strt_length[1]*factor[1]};
double[]new_point= {new_length[0]+axis[0],new_length[1]+axis[1]};
return new_point;
}
//this function calculates the distance
public static double[] dis2_vec_d(double[]p1,double[]p2) {
double[] vec = new double[2];
vec[0]=p1[0]-p2[0];
vec[1]=p1[1]-p2[1];
return vec;
} 

1 个答案:

答案 0 :(得分:1)

看起来你只想让你的Node可以拖动。我创建了一个这样做的类,粘贴在下面。

您可能必须在下面的代码中替换名为GameScreen的类的几个实例。

╔══════════════════════════╦════════════╦══════════════════════════╗
║      CompanyNameNew      ║    City    ║         Address          ║
╠══════════════════════════╬════════════╬══════════════════════════╣
║ Starbucks (Seattle)      ║ Seattle    ║ NULL                     ║
║ Starbucks (Seattle - 1)  ║ Seattle    ║ 2701 Freedom way         ║
║ Starbucks (Seattle - 2)  ║ Seattle    ║ 3500 E Destination Drive ║
║ Starbucks (Seattle - 3)  ║ Seattle    ║ 44 East Ave              ║
║ Starbucks (Orleans)      ║ Orleans    ║ 2800 Rice St.            ║
║ Starbucks (St. Paul)     ║ St. Paul   ║ 6500 Henri-Bourassa BE   ║
║ Starbucks (St. Paul - 1) ║ St. Paul   ║ 6500 Henri-Bourassa BE   ║
║ Starbucks (Luxembourg)   ║ Luxembourg ║ N2915 Countrt Road AB    ║
╚══════════════════════════╩════════════╩══════════════════════════╝

public class Draggable { //////////////////////////////////////////////////////////////////////////// //variables AtomicBoolean myPressed = new AtomicBoolean(false); Point2D myInitialTranslates; Point2D dragInitialTranslates; EventHandler<? super MouseEvent> onMouseReleased; EventHandler<? super MouseEvent> onMousePressed; EventHandler<? super MouseEvent> onMouseDragged; OnDrag onDrag; boolean stayWithinBounds = true; double minX = 0; double maxX = WIDTH; double minY = 0; double maxY = GameScreen.PLAY_AREA_HEIGHT; boolean doNotTranslate = false; //handleX and handleY are offsets used strictly for the callbacks, they are not used for the translation of the destination Node. double handleX = 0; double handleY = 0; double X; double Y; boolean reverseX = false; boolean reverseY = false; float amountOfTargetNodeToAllowOutsideLimit = 0f; public void setOnDrag(OnDrag onDrag) { this.onDrag = onDrag; } //////////////////////////////////////////////////////////////////////////// //constructors public Draggable() { } public Draggable(double handleX, double handleY) { this.handleX = handleX; this.handleY = handleY; } //////////////////////////////////////////////////////////////////////////// //methods public void makeDraggable(Node source, Node target, double dragAdjustmentRatioX, double dragAdjustmentRatioY) { source.setOnMouseDragged((MouseEvent event) -> { if (myPressed.get() && !doNotTranslate) { double x = myInitialTranslates.getX() - dragInitialTranslates.getX() + dragAdjustmentRatioX * GameScreen.instance.scaleXToScreen(event.getSceneX()); double y = myInitialTranslates.getY() - dragInitialTranslates.getY() + dragAdjustmentRatioY * GameScreen.instance.scaleYToScreen(event.getSceneY()); double localMaxX = maxX - target.getBoundsInLocal().getWidth() * amountOfTargetNodeToAllowOutsideLimit; double localMinX = minX - target.getBoundsInLocal().getWidth() * amountOfTargetNodeToAllowOutsideLimit; if (x > localMinX && x < localMaxX) { target.setTranslateX(x); } else { // Log.debug("X is not within range. x = " + x); } double localMaxY = maxY - target.getBoundsInLocal().getHeight() * amountOfTargetNodeToAllowOutsideLimit; double localMinY = minY - target.getBoundsInLocal().getWidth() * amountOfTargetNodeToAllowOutsideLimit; if (y > localMinY && y < localMaxY) { target.setTranslateY(y); } else { // Log.debug("Y is not within range. x = " + y); } } else { } if (onMouseDragged != null) { onMouseDragged.handle(event); } if (onDrag != null) { if (reverseX) { X = handleX + dragInitialTranslates.getX() - dragAdjustmentRatioX * GameScreen.instance.scaleXToScreen(event.getSceneX()); } else { X = handleX - dragInitialTranslates.getX() + dragAdjustmentRatioX * GameScreen.instance.scaleXToScreen(event.getSceneX()); } if (reverseY) { Y = handleY + dragInitialTranslates.getY() - dragAdjustmentRatioY * GameScreen.instance.scaleYToScreen(event.getSceneY()); } else { Y = handleY - dragInitialTranslates.getY() + dragAdjustmentRatioY * GameScreen.instance.scaleYToScreen(event.getSceneY()); } onDrag.handle(X, Y); } }); source.setOnMousePressed((MouseEvent event) -> { myInitialTranslates = new Point2D(target.getTranslateX(), target.getTranslateY()); dragInitialTranslates = new Point2D( dragAdjustmentRatioX * GameScreen.instance.scaleXToScreen(event.getSceneX()), dragAdjustmentRatioY * GameScreen.instance.scaleYToScreen(event.getSceneY())); myPressed.set(true); if (onMousePressed != null) { onMousePressed.handle(event); } GameScreen.setCursorMove(); }); source.setOnMouseReleased((MouseEvent event) -> { myPressed.set(false); handleX = X; handleY = Y; if (onMouseReleased != null) { onMouseReleased.handle(event); } GameScreen.setCursorCustom(); }); } public void makeDraggable(Node node, double dragAdjustmentRatioX, double dragAdjustmentRatioY) { makeDraggable(node, node, dragAdjustmentRatioX, dragAdjustmentRatioY); } public void setOnMouseReleased(EventHandler<? super MouseEvent> onMouseReleased) { this.onMouseReleased = onMouseReleased; } public void setOnMousePressed(EventHandler<? super MouseEvent> onMousePressed) { this.onMousePressed = onMousePressed; } public void setOnMouseDragged(EventHandler<? super MouseEvent> onMouseDragged) { this.onMouseDragged = onMouseDragged; } public void setAmountOfTargetNodeToAllowOutsideLimit(float amountOfTargetNodeToAllowOutsideLimit) { this.amountOfTargetNodeToAllowOutsideLimit = amountOfTargetNodeToAllowOutsideLimit; } public interface OnDrag { public void handle(double x, double y); } public boolean isStayWithinBounds() { return stayWithinBounds; } public void setStayWithinBounds(boolean stayWithinBounds) { this.stayWithinBounds = stayWithinBounds; } public void setMinMax(double minX, double maxX, double minY, double maxY) { this.maxX = maxX; this.minX = minX; this.minY = minY; this.maxY = maxY; } public void setDoNotTranslate(boolean doNotTranslate) { this.doNotTranslate = doNotTranslate; } public boolean isReverseX() { return reverseX; } public void setReverseX(boolean reverseX) { this.reverseX = reverseX; } public boolean isReverseY() { return reverseY; } public void setReverseY(boolean reverseY) { this.reverseY = reverseY; } public double getHandleX() { return handleX; } public void setHandleX(double handleX) { this.handleX = handleX; } public double getHandleY() { return handleY; } public void setHandleY(double handleY) { this.handleY = handleY; } } GameScreen.instance.scaleXToScreen只需将其设置为1.将来如果缩放窗口,可能需要对其进行调整。

GameScreen.instance.scaleYToScreenGameScreen.WIDTH设置场景的宽度和高度。

可以删除更改鼠标光标的两个方法调用。

你使用它的方式是这样的......

GameScreen.PLAY_HEIGHT

那就是那个。

编辑:它还有onDrag,onMousePressed,onMouseDragged和onMouseReleased等基本事件的回调。