Javafx canvas 内容消失
Javafx canvas content disappears
我正在尝试使用 2-opt-change 算法可视化解决旅行商问题的过程,但有时程序的执行似乎卡住了并且 window 无任何错误地冻结.
有时甚至 canvas 上的线条也会消失。
有帮助就好了。
绘图代码如下:
int numCities = getNumOfCities();
Order currentOrder = data.getCurrentOrder();
if (newValue) {
GraphicsContext gc = canvasCities.getGraphicsContext2D();
gc.clearRect(0, 0, canvasCities.getWidth(), canvasCities.getHeight());
gc.setStroke(new Color(0, 0, 0, 1));
gc.setLineWidth(1);
gc.setFill((Color.GRAY));
for (int i = 0; i < currentOrder.getSize(); i++) {
City current = data.getCities()[currentOrder.getValue(i)];
City next;
if ((i + 1) > numCities - 1) {
next = data.getCities()[currentOrder.getValue(0)];
} else {
next = data.getCities()[currentOrder.getValue(i + 1)];
}
gc.fillOval(current.getX() - 5, current.getY() - 5, 10, 10);
gc.strokeLine(current.getX(), current.getY(), next.getX(), next.getY());
}
if(!(data.getBestEver()==null)) {
Order best = data.getBestEver();
gc.setStroke(new Color(1, 0, 0, 1));
gc.setLineWidth(3);
gc.setFill((Color.GRAY));
for (int i = 0; i < best.getSize(); i++) {
City current = data.getCities()[best.getValue(i)];
City next;
if ((i + 1) > numCities - 1) {
next = data.getCities()[best.getValue(0)];
} else {
next = data.getCities()[best.getValue(i + 1)];
}
gc.fillOval(current.getX() - 5, current.getY() - 5, 10, 10);
gc.strokeLine(current.getX(), current.getY(), next.getX(), next.getY());
}
}
repaint.set(false); //boolean that indicated if something changed and a repaint is necessairy
}
but sometimes it seems like the execution of the program gets stuck
and the window freezes without any error.
JFX 消耗大量资源...尤其是 Canvas 和形状(例如递归组等)如果卡住,可能是 "stack" 处于溢出的边缘。尝试使用 -Xss[nnM],其中 nn 是任意数字,M 代表兆字节。示例 -Xss16M:为 JFX 线程堆栈 16MB。如果它仍然表现 "weirdly" 然后结合 -Xss 和 -Xms (对于堆)。
问题出在多线程部分。在将绘画部分转换为使用 Platform.runLater()
调用的可运行对象后,它工作正常。
感谢所有花时间思考我的问题的人。
我正在尝试使用 2-opt-change 算法可视化解决旅行商问题的过程,但有时程序的执行似乎卡住了并且 window 无任何错误地冻结. 有时甚至 canvas 上的线条也会消失。
有帮助就好了。
绘图代码如下:
int numCities = getNumOfCities();
Order currentOrder = data.getCurrentOrder();
if (newValue) {
GraphicsContext gc = canvasCities.getGraphicsContext2D();
gc.clearRect(0, 0, canvasCities.getWidth(), canvasCities.getHeight());
gc.setStroke(new Color(0, 0, 0, 1));
gc.setLineWidth(1);
gc.setFill((Color.GRAY));
for (int i = 0; i < currentOrder.getSize(); i++) {
City current = data.getCities()[currentOrder.getValue(i)];
City next;
if ((i + 1) > numCities - 1) {
next = data.getCities()[currentOrder.getValue(0)];
} else {
next = data.getCities()[currentOrder.getValue(i + 1)];
}
gc.fillOval(current.getX() - 5, current.getY() - 5, 10, 10);
gc.strokeLine(current.getX(), current.getY(), next.getX(), next.getY());
}
if(!(data.getBestEver()==null)) {
Order best = data.getBestEver();
gc.setStroke(new Color(1, 0, 0, 1));
gc.setLineWidth(3);
gc.setFill((Color.GRAY));
for (int i = 0; i < best.getSize(); i++) {
City current = data.getCities()[best.getValue(i)];
City next;
if ((i + 1) > numCities - 1) {
next = data.getCities()[best.getValue(0)];
} else {
next = data.getCities()[best.getValue(i + 1)];
}
gc.fillOval(current.getX() - 5, current.getY() - 5, 10, 10);
gc.strokeLine(current.getX(), current.getY(), next.getX(), next.getY());
}
}
repaint.set(false); //boolean that indicated if something changed and a repaint is necessairy
}
but sometimes it seems like the execution of the program gets stuck and the window freezes without any error.
JFX 消耗大量资源...尤其是 Canvas 和形状(例如递归组等)如果卡住,可能是 "stack" 处于溢出的边缘。尝试使用 -Xss[nnM],其中 nn 是任意数字,M 代表兆字节。示例 -Xss16M:为 JFX 线程堆栈 16MB。如果它仍然表现 "weirdly" 然后结合 -Xss 和 -Xms (对于堆)。
问题出在多线程部分。在将绘画部分转换为使用 Platform.runLater()
调用的可运行对象后,它工作正常。
感谢所有花时间思考我的问题的人。