简体   繁体   English

如何在画布上绘制东西javafx

[英]how to draw something on canvas javafx

I'm beginner of javafx: I am developing a graphical user interface using javafx and Scene builder that has a coordinate plane with x axis and y axis. 我是javafx的初学者:我正在使用javafx和Scene Builder开发图形用户界面,该场景的坐标平面带有x轴和y轴。 It should be like this: https://postimg.cc/image/98k9mvnb3/ when someone do a mouse click anywhere on this coordinate plane it will show the coordinate point(x,y) of the pixel on the console and there will a mark(like point or some text will be written) on the same place where mouse clicked. 它应该是这样的: https : //postimg.cc/image/98k9mvnb3/当某人在此坐标平面上的任意位置单击鼠标时,它将在控制台上显示像素的坐标点(x,y),并且会出现一个鼠标单击同一位置上的标记(将写入点或一些文本)。

For implementing these things I have to use canvas & i'm able to get the coordinate point but i'm not getting how to draw the coordinate plane and how to write something on the pixel where mouse clicked. 为了实现这些功能,我必须使用canvas&我能够获得坐标点,但是我没有获得如何绘制坐标平面以及如何在鼠标单击的像素上写入内容的方法。

Here is my code: Controller Class package application; 这是我的代码:Controller Class软件包应用程序;

import java.net.URL;
import java.util.ResourceBundle;

import javafx.fxml.FXML;
import javafx.fxml.Initializable;
import javafx.scene.canvas.Canvas;
import javafx.scene.input.MouseEvent;
import javafx.scene.layout.AnchorPane;
public class AxisController implements Initializable{
    @FXML
    private AnchorPane anchr;
    @FXML 
    private Canvas canvas;
    @Override
    public void initialize(URL location, ResourceBundle resources) {
         assert canvas != null : "fx:id=\"canvas\" was not injected: check your FXML file 'AxisFxml.fxml'.";
    }

    @FXML
    private void handleMouse(MouseEvent event){
        System.out.println(event.getX());
        System.out.println(event.getY());
    }   
}

Main Class: 主类:

package application;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.stage.Stage;
import javafx.scene.Scene;
import javafx.scene.layout.AnchorPane;
public class AxisMain extends Application {
    @Override
    public void start(Stage primaryStage) {
        try {
            AnchorPane root = FXMLLoader.load(getClass().getResource("/application/AxisFxml.fxml"));
            Scene scene = new Scene(root,400,400);
            scene.getStylesheets().add(getClass().getResource("application.css").toExternalForm());
            primaryStage.setScene(scene);
            primaryStage.show();
        } catch(Exception e) {
            e.printStackTrace();
        }
    }

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

Have you tried something as simple as putting some code like this into your handleMouse method? 您是否尝试过将诸如此类的代码放入handleMouse方法中这样简单的事情?

GraphicsContext gc = canvas.getGraphicsContext2D();

gc.setFill(Color.BLUE);
gc.fillRect(event.getX()-5,event.getY()-5,10,10);

This assumes of course that you have also attached this method to the canvas so that you actually get the events in the right coordinate system. 当然,这假定您也已将此方法附加到画布上,以便您实际在正确的坐标系中获取事件。

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

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