JavaFX输入的鼠标事件延迟/延迟

时间:2018-09-13 23:13:56

标签: javafx

我创建了一个Rectangle对象网格并将它们添加到Pane中。每个矩形都有一个附加的鼠标事件侦听器,该事件由MouseEvent.Entered触发器触发。当用户将鼠标移到矩形上时,处理程序仅更改矩形的颜色。问题在于触发器似乎在执行之前有相当大的延迟。有什么想法如何加快此速度,以便使用鼠标实时进行吗?

我已将录音上传到这里:https://screencast-o-matic.com/watch/cFQI0lqdHe

public class WarehouseMap extends Pane {

    private int xSpaces = 200;
    private int ySpaces = 100;
    private ArrayList<Rectangle> gridReferences = new ArrayList<Rectangle> ();

    public WarehouseMap() {
        setWidth(2000);
        setHeight(1000);
        initGrid();
    }

    public void initGrid() {
        double rectWidth = getWidth() / xSpaces;
        double rectHeight = getHeight() / ySpaces;

        for(int x=0; x<xSpaces; x++) {
            for(int y=0; y<ySpaces; y++) { 
                Rectangle gr = new Rectangle(x*rectWidth, y*rectHeight, rectWidth, rectHeight);
                gr.setStroke(Color.GRAY);
                gr.setFill(Color.TRANSPARENT);
                gr.setStrokeWidth(1);
                gr.addEventHandler(MouseEvent.MOUSE_ENTERED, new EventHandler<MouseEvent> () {
                    @Override
                    public void handle(MouseEvent event) {
                        gr.setFill(Color.DARKGRAY);
                    }

                });

                gr.addEventHandler(MouseEvent.MOUSE_EXITED, new EventHandler<MouseEvent> () {
                    @Override
                    public void handle(MouseEvent event) {
                        gr.setFill(Color.TRANSPARENT);
                    }

                });

                gridReferences.add(gr);
                this.getChildren().add(gr);
            }
        }
    }
}

1 个答案:

答案 0 :(得分:2)

您可以使用Canvas来创建更快的视图:

public class WarehouseCanvasMap extends Pane {
    private int xSpaces = 200;
    private int ySpaces = 100;
    private int cellSize = 10;
    private int lineSize = 1;
    private Canvas canvas;

    public WarehouseCanvasMap() {
        setWidth(xSpaces * cellSize);
        setHeight(ySpaces * cellSize);
        initGrid();
    }

    public void initGrid() {
        canvas = new Canvas();
        canvas.setWidth(getWidth());
        canvas.setHeight(getHeight());
        getChildren().add(canvas);
        GraphicsContext graphic = canvas.getGraphicsContext2D();

        graphic.setStroke(Color.GRAY);
        graphic.setFill(Color.DARKGRAY);
        graphic.setLineWidth(lineSize);

        canvas.setOnMouseMoved(event -> {
            graphic.clearRect(0, 0, canvas.getWidth(), canvas.getHeight());

            graphic.fillRect(event.getX() - event.getX() % cellSize, event.getY() - event.getY() % cellSize, cellSize, cellSize);

            for (int x = 0; x <= xSpaces; x++) {
                graphic.strokeLine(x * cellSize, 0, x * cellSize, canvas.getHeight());
            }
            for (int y = 0; y <= xSpaces; y++) {
                graphic.strokeLine(0, y * cellSize, canvas.getWidth(), y * cellSize);
            }
        });
    }
}

此解决方案如下所示: enter image description here

它看起来比原来的要快: enter image description here