简体   繁体   English

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

[英]Is it possible to put ImageView on Canvas in JavaFX?

I am a newbie to javafx. 我是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 ImageView on my canvas and implement a popup while clicking on the ImageView? 我想在我的画布上添加一个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. 我需要一些建议。

You can stack an ImageView on top of a Canvas . 您可以在Canvas顶部堆叠ImageView Use a StackPane . 使用StackPane You can later add mouse listeners to the ImageView . 您可以稍后将鼠标侦听器添加到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 , ImageView and Canvas itself extend from Node and 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是熊。

在此输入图像描述

If you just want add button, image ... on the top of your canvas you should use StackPane . 如果你只想在画布顶部添加按钮,图像......你应该使用StackPane You first add your canvas and then you can add Button or whatever you want. 您首先添加画布,然后您可以添加按钮或任何您想要的。

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

You should read this for a better understanding of Layouting with JavaFX. 您应该阅读本文,以便更好地理解使用JavaFX进行Layouting。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM