连接多个对象以创建一个对象

问题描述 投票:0回答:2

现在我想在javafx中创建一个哑铃形状我知道如何创建它并控制它但我需要将形状视为多个三个对象而不仅仅是一个对象的问题我是java和oop中的新手我只是想创建哑铃扩展的形状提前感谢扩展javafx节点

public static void dumbell_builder(Group root) {
Line joiner= line_builder(30,50,20,40,"#00c9e0",1,"joiner");
    Circle rball=ball_builder(30,50, 2,"#00c9e0",true,joiner,"Rball");
    Circle lball=ball_builder(20,40, 2,"#00c9e0",false,joiner,"Lball");
    root.getChildren().addAll(joiner,lball,rball);

}
public static Line line_builder(int sx ,int sy, int ex,int ey,String Stroke,int width,String id) {
    Line line= new Line(sx,sy,ex,ey);
    line.setStrokeWidth(width);
    line.setStroke(Paint.valueOf(Stroke));
    line.setId(id);
    return line;
}
private static Circle ball_builder(int x,int y, int r,String fill,boolean right,Line line,String id) {
    Circle ball=new Circle(x,y,r);
    ball.setFill(Paint.valueOf(fill));
    ball.setId(id);
    ball.setOnMousePressed(event->{
        strtdrag=true;

    });
    ball.setOnMouseDragged(event->{

        if(strtdrag) {
            ball.setCenterY(event.getY());
            ball.setCenterX(event.getX());
                    if (right) {
                        line.setEndY(event.getY());
                        line.setEndX(event.getX());
                    }else {
                        line.setStartY(event.getY());
                       line.setStartX(event.getX());
                    }
            }

    });
    ball.setOnMouseReleased(event ->{
        strtdrag=false;

    });
    return ball;
}

这是我创建dumbell课程的试验

   public static void create_dumbell(Group root,double sx,double sy,double ex,double ey) {
DoubleProperty startX = new SimpleDoubleProperty(sx);
DoubleProperty startY = new SimpleDoubleProperty(sy);
DoubleProperty endX   = new SimpleDoubleProperty(ex);
DoubleProperty endY   = new SimpleDoubleProperty(ey);
Line ru = new ControlLine(startX, startY, endX, endY);
Anchor control1 = new Anchor(Color.FORESTGREEN, ru.startXProperty(), ru.startYProperty(), 3);
Anchor control2 = new Anchor(Color.FORESTGREEN, ru.endXProperty(), ru.endYProperty(), 3);
 root.getChildren().addAll(ru,control1,control2);
 }
      class ControlLine extends Line {
    ControlLine(DoubleProperty startX, DoubleProperty startY, DoubleProperty endX, DoubleProperty endY) {
        startXProperty().bind(startX);
        startYProperty().bind(startY);
        endXProperty().bind(endX);
        endYProperty().bind(endY);
        setStrokeWidth(2);
        setStroke(Color.FORESTGREEN.deriveColor(0, 1, 1, 0.5));
    }
}


// a draggable anchor displayed around a point.
class Ball extends Circle {
    Ball(Color color, DoubleProperty x, DoubleProperty y, double radius) {
        super(x.get(), y.get(), radius);
        setFill(color.deriveColor(1, 1, 1, 0.5));
        setStroke(color);
        setStrokeWidth(2);
        setStrokeType(StrokeType.OUTSIDE);
        x.bind(centerXProperty());
        y.bind(centerYProperty());
        enableDrag();
    }
    Ball(Color color, double x, double y, double radius) {
        super(x, y, radius);
        setFill(color.deriveColor(1, 1, 1, 0.5));
        setStroke(color);
        setStrokeWidth(2);
        setStrokeType(StrokeType.OUTSIDE);
        enableDrag();
    }
    // make a node movable by dragging it around with the mouse.
    private void enableDrag() {
        final Delta dragDelta = new Delta();
        setOnMousePressed(mouseEvent -> {
            // record a delta distance for the drag and drop operation.
            dragDelta.x = getCenterX() - mouseEvent.getX();
            dragDelta.y = getCenterY() - mouseEvent.getY();

        });
        setOnMouseReleased(mouseEvent -> getScene().setCursor(Cursor.HAND));
        setOnMouseDragged(mouseEvent -> {
            double newX = mouseEvent.getX() + dragDelta.x;
            if (newX > 0 && newX < getScene().getWidth()) {
                setCenterX(newX);
            }
            double newY = mouseEvent.getY() + dragDelta.y;
            if (newY > 0 && newY < getScene().getHeight()) {
                setCenterY(newY);
            }
        });

    }
    // records relative x and y co-ordinates.
    private class Delta { double x, y; }
}
java oop javafx
2个回答
1
投票

创建Group的子类,它将行和圆添加到自身。您还可以使用绑定来保持线与圆相连:

public class Dumbell extends Group {

    private boolean dragging ;

    public Dumbell() {

        Line joiner = new Line();
        joiner.setId("joiner");
        joiner.setStroke(Paint.valueOf("#00c9e0"));
        joiner.setStrokeWidth(1);

        Circle rball = createBall(30, 50);
        Circle lball = createBall(20, 40);

        joiner.startXProperty().bind(rball.centerXProperty());
        joiner.startYProperty().bind(rball.centerYProperty());
        joiner.endXProperty().bind(lball.centerXProperty());
        joiner.endYProperty().bind(lball.centerYProperty());

        getChildren().addAll(joiner, lball, rball);
    }

    private Circle createBall(double x, double y) {
        Circle ball = new Circle(x, y, 2);
        ball.setFill(Paint.valueOf("#00c9e0"));
        ball.setOnMousePressed(e -> dragging = true );
        ball.setOnMouseDragged(e -> {
            if (dragging) {
                ball.setCenterX(e.getX());
                ball.setCenterY(e.getY());
            }
        });
        ball.setOnMouseReleased(e -> dragging = false);
        return ball ;
    }
}

那你就需要了

Dumbell dumbell = new Dumbell();
root.getChildren().add(dumbell);

0
投票

创建一个窗格,您可以在其上添加形状并访问它而不是形状。你是这个意思吗? 我完全不理解你的问题,因为英语有点难过。

您还可以通过创建扩展Pane的类来创建自定义节点,然后在需要为组件设置特定布局时覆盖其layoutChildren()方法。

© www.soinside.com 2019 - 2024. All rights reserved.