在JavaFX中绘制矩形边框中的圆圈

时间:2014-07-04 13:29:59

标签: java javafx javafx-2 javafx-8

我想在事件onMouseMoved中在矩形的边框中添加一些圆圈。 需要使用JavaFX开发图形,这些圆圈将用于将边连接到图形节点。 见下图:

enter image description here

我正在使用JavaFX。 见到代码:

public class SampleDragAndDrop extends Application {

public static void main(String[] args) throws Exception {
    launch(args);
}

@Override
public void start(final Stage stage) throws Exception {
    DoubleProperty entity1X = new SimpleDoubleProperty(100);
    DoubleProperty entity1Y = new SimpleDoubleProperty(100);        

    Entity entity1 = new Entity(Color.STEELBLUE, entity1X, entity1Y);


    Screen screen = Screen.getPrimary();
    Rectangle2D bounds = screen.getVisualBounds();
    stage.setX(bounds.getMinX());
    stage.setY(bounds.getMinY());
    stage.setWidth(bounds.getWidth());
    stage.setHeight(bounds.getHeight());

    stage.setTitle("Draw circle in rectangle");
    stage.setScene(new Scene(new Group(entity1), 400, 400, Color.ALICEBLUE));
    stage.show();
}

class Anchor extends Circle {

    Anchor(Color color, DoubleProperty x, DoubleProperty y) {
        super(x.get(), y.get(), 20);
        setFill(color.deriveColor(1, 1, 1, 0.5));
        setStroke(color);
        setStrokeWidth(2);
        setStrokeType(StrokeType.OUTSIDE);

        x.bind(centerXProperty());
        y.bind(centerYProperty());
    }

class Entity extends Rectangle {

    Entity(Color color, DoubleProperty x, DoubleProperty y) {
        setX(x.get());
        setY(y.get());
        setWidth(120);
        setHeight(50);
        setFill(color.deriveColor(1, 1, 1, 0.5));
        setStroke(color);
        setStrokeWidth(2);
        setStrokeType(StrokeType.OUTSIDE);
        setArcWidth(20);
        setArcHeight(20);      


        x.bind(xProperty());
        y.bind(yProperty());

        enableDrag();
    }

    private void enableDrag() {
        final Entity.Delta dragDelta = new Entity.Delta();
        setOnMousePressed(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent mouseEvent) {
                // record a delta distance for the drag and drop operation.                    
                dragDelta.x = getX() - mouseEvent.getX();
                dragDelta.y = getY() - mouseEvent.getY();
                getScene().setCursor(Cursor.MOVE);
            }
        });
        setOnMouseReleased(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent mouseEvent) {
                getScene().setCursor(Cursor.HAND);
            }
        });
        setOnMouseDragged(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent mouseEvent) {
                double newX = mouseEvent.getX() + dragDelta.x;
                if (newX > 0 && newX < getScene().getWidth()) {
                    setX(newX);
                }
                double newY = mouseEvent.getY() + dragDelta.y;
                if (newY > 0 && newY < getScene().getHeight()) {
                    setY(newY);
                }
            }
        });
        setOnMouseEntered(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent mouseEvent) {
                if (!mouseEvent.isPrimaryButtonDown()) {
                    getScene().setCursor(Cursor.HAND);
                }
            }
        });
        setOnMouseExited(new EventHandler<MouseEvent>() {
            @Override
            public void handle(MouseEvent mouseEvent) {
                if (!mouseEvent.isPrimaryButtonDown()) {
                    getScene().setCursor(Cursor.DEFAULT);
                }
            }
        });

        setOnMouseMoved(new EventHandler<MouseEvent>(){

            @Override
            public void handle(MouseEvent mouseEvent) {
                // Create circles in rectangle here                    
            }
        });
    }        

    private class Delta {
        double x, y;
    }
}
}

我该怎么办?

谢谢!

1 个答案:

答案 0 :(得分:1)

这有效:

// Create circles in rectangle here                    

// Not sure you really need these?
DoubleProperty leftX = new SimpleDoubleProperty();
DoubleProperty leftY = new SimpleDoubleProperty();

Anchor leftAnchor = new Anchor(Color.STEELBLUE, leftX, leftY);
leftAnchor.centerXProperty().bind(xProperty());
leftAnchor.centerYProperty().bind(yProperty().add(heightProperty().divide(2)));
((Group)getParent()).getChildren().add(leftAnchor);