java - 在 JavaFX2 中的图片顶部绘制矩形

标签 java javafx-2 javafx fxml scenebuilder

我正在尝试使用 JavaFX2 中的鼠标事件在图片上绘制矩形。

现在,我在 StackPane 中有一个 ImageView,并在其上添加了矩形。问题是即使我将 Rectangles 的 X 和 Y 设置为 MouseEvent X 和 Y,Rectangles 仍然在 StackPane 中居中。

我想默认情况下是 StackPane 使每个 child 居中,但我找不到解决该问题的合适方法。你们能给我指出正确的方向吗?

这是我的代码:

@FXML
private StackPane stack_pane;

private final ImageView image_view = new ImageView();

private final Set<Rectangle> rectangles = new HashSet<Rectangle>();

private final SimpleDoubleProperty selectionRectInitialX = new SimpleDoubleProperty();
private final SimpleDoubleProperty selectionRectInitialY = new SimpleDoubleProperty();

private final SimpleDoubleProperty selectionRectCurrentX = new SimpleDoubleProperty();
private final SimpleDoubleProperty selectionRectCurrentY = new SimpleDoubleProperty();

private Rectangle selectionRect;

@Override
public void initialize(final URL fxmlFileLocation, final ResourceBundle resources)
{
    this.stack_pane.getChildren().add(this.image_view);
    this.selectionRect = this.getRectangle();

    this.selectionRect.widthProperty().bind(this.selectionRectCurrentX.subtract(this.selectionRectInitialX));
    this.selectionRect.heightProperty().bind(this.selectionRectCurrentY.subtract(this.selectionRectInitialY));

    this.stack_pane.setOnMousePressed(new EventHandler<MouseEvent>()
    {
        @Override
        public void handle(final MouseEvent event)
        {
            MainWindowController.this.selectionRect.xProperty().set(event.getX());
            MainWindowController.this.selectionRect.yProperty().set(event.getY());
            MainWindowController.this.selectionRectInitialX.set(event.getX());
            MainWindowController.this.selectionRectInitialY.set(event.getY());
        }
    });

    this.stack_pane.setOnMouseDragged(new EventHandler<MouseEvent>()
    {
        @Override
        public void handle(final MouseEvent event)
        {
            MainWindowController.this.selectionRectCurrentX.set(event.getX());
            MainWindowController.this.selectionRectCurrentY.set(event.getY());
            MainWindowController.this.repaint();
        }
    });

    this.stack_pane.setOnMouseReleased(new EventHandler<MouseEvent>()
    {
        @Override
        public void handle(final MouseEvent event)
        {
            final Rectangle newRect = MainWindowController.this.getRectangle();

            newRect.setWidth(MainWindowController.this.selectionRect.getWidth());
            newRect.setHeight(MainWindowController.this.selectionRect.getHeight());
            newRect.setX(MainWindowController.this.selectionRect.getX());
            newRect.setY(MainWindowController.this.selectionRect.getY());

            MainWindowController.this.selectionRectCurrentX.set(0);
            MainWindowController.this.selectionRectCurrentY.set(0);

            MainWindowController.this.rectangles.add(newRect);
            MainWindowController.this.repaint();
        }
    });
}

public Rectangle getRectangle()
{
    final Rectangle rect = new Rectangle();
    rect.setFill(Color.web("firebrick", 0.4));
    rect.setStroke(Color.web("firebrick", 0.4));
    return rect;
}

public void repaint()
{
    this.stack_pane.getChildren().clear();
    this.stack_pane.getChildren().add(this.image_view);
    this.stack_pane.getChildren().add(this.selectionRect);
    for (final Rectangle rect : this.rectangles)
    {
        this.stack_pane.getChildren().add(rect);
    }
}

最佳答案

将 StackPane 更改为 AnchorPane。 AnchorPane 允许您设置每个 child 相对于 Pane 的 X、Y。 http://docs.oracle.com/javafx/2/api/javafx/scene/layout/AnchorPane.html

关于java - 在 JavaFX2 中的图片顶部绘制矩形,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17776180/

相关文章:

java - 如何在 Linux 上编译 JavaMail Mbox Store?

java - 每当鼠标在场景上移动时如何使节点移动(在 JavaFX 中)?

image - JavaFX 2.2 图像支持 .ico?

java - 使用 css 自定义 JavaFx Alert

java - 从一个长流创建流

java - 使用 android Java 电话、电子邮件、日期进行正则表达式验证

java - JAVA 字符数组中的特定元素排列?

javafx-2 - 从 FXML 文件引用节点

JavaFX - 执行 JavaFX WebView 示例代码时出现 java.lang.reflect.InitationTargetException

Java 将子列表绑定(bind)到主列表