现在我想在javafx中创建一个哑铃形状我知道如何创建它并控制它但我需要将形状视为多个三个对象而不仅仅是一个对象的问题我是java和oop中的新手 我只是想创建扩展javafx节点的dumbell形状 提前谢谢
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; }
}
答案 0 :(得分: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);
答案 1 :(得分:0)
创建一个窗格,您可以在其上添加形状并访问它而不是形状。这是你的意思吗? 我不完全理解你的问题,因为英语有点不好意思。
如果需要为组件设置特定的布局,还可以通过创建扩展Pane的类然后覆盖其layoutChildren()方法来创建自定义节点。