gpt4 book ai didi

JavaFX 进入鼠标事件延迟/滞后

转载 作者:行者123 更新时间:2023-12-01 03:10:55 29 4
gpt4 key购买 nike

我创建了一个矩形对象网格并将它们添加到一个 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);
}
}
}
}

最佳答案

您可以使用 Canvas创建更快的 View :

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

关于JavaFX 进入鼠标事件延迟/滞后,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52322935/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com