java 是否可以在 JavaFX 中将 ImageView 放在 Canvas 上?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/30068651/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 16:22:43  来源:igfitidea点击:

Is it possible to put ImageView on Canvas in JavaFX?

javacanvasjavafxpopupimageview

提问by Viz_Kid

I am a newbie to javafx. I am developing an application in which I have a canvas and I have drawn several images in it. I want to add an ImageViewon my canvas and implement a popup while clicking on the ImageView?

我是javafx的新手。我正在开发一个应用程序,其中有一个画布,并在其中绘制了多个图像。我想ImageView在我的画布上添加一个并在单击 ImageView 时实现一个弹出窗口?

Is there any possibilities with or without ImageView(buttons or any controls?) or is it restricted to use controls over canvas?

是否有任何可能性ImageView(按钮或任何控件?)或者是否仅限于在画布上使用控件?

I need some suggestions please.

我需要一些建议。

回答by ItachiUchiha

You can stack an ImageViewon top of a Canvas. Use a StackPane. You can later add mouse listeners to the ImageView.

您可以ImageViewCanvas. 使用一个StackPane. 您可以稍后将鼠标侦听器添加到ImageView.

Is there any possibilities with or without ImageView(buttons or any controls?) or is it restricted to use controls over canvas?

是否有任何可能性ImageView(按钮或任何控件?)或者是否仅限于在画布上使用控件?

All controls including Button, ImageViewand Canvasitself extend from Nodeand can be used to add in the StackPane.

所有的控制,包括ButtonImageViewCanvas本身从延伸Node,并且可以被用于在添加StackPane

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.canvas.Canvas;
import javafx.scene.canvas.GraphicsContext;
import javafx.scene.image.ImageView;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.stage.Stage;

public class Main extends Application {

    int count = 1;

    @Override
    public void start(Stage stage) {

        StackPane root = new StackPane();
        Scene s = new Scene(root, 400, 400, Color.BLACK);

        final Canvas canvas = new Canvas(300, 300);
        GraphicsContext gc = canvas.getGraphicsContext2D();

        gc.setFill(Color.BLUE);
        gc.fillRect(10, 10, 300, 300);

        ImageView image = new ImageView("https://cdn0.iconfinder.com/data/icons/toys/256/teddy_bear_toy_6.png");

        // Listener for MouseClick
        image.setOnMouseClicked(e -> {
            Stage popup = new Stage();
            popup.initOwner(stage);
            popup.show();
        });

        root.getChildren().addAll(canvas, image);

        stage.setScene(s);
        stage.show();
    }

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

Output:

输出:

  • Scene has Black Background
  • Canvas has Blue Background
  • ImageView is the bear.
  • 场景有黑色背景
  • 画布有蓝色背景
  • ImageView 是熊。

enter image description here

在此处输入图片说明

回答by agonist_

If you just want add button, image ... on the top of your canvas you should use StackPane. You first add your canvas and then you can add Button or whatever you want.

如果您只想在画布顶部添加按钮、图像...,您应该使用StackPane。你首先添加你的画布,然后你可以添加 Button 或任何你想要的。

 StackPane stack = new StackPane();
 stack.getChildren().addAll(yourCanvas, yourButton);

You should read thisfor a better understanding of Layouting with JavaFX.

您应该阅读本文以更好地理解使用 JavaFX 进行布局。