SF23
SF23

Reputation: 184

Javafx canvas content disappears

I'm trying to visualize the process of solving the travelling salesman problem using the 2-opt-change algorithm but sometimes it seems like the execution of the program gets stuck and the window freezes without any error. Sometimes even the lines drawn on a canvas disappear.

It shold look like this:

But it looks like this:

Some help would be nice.

Here's the drawing code:

 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
        }

Upvotes: 0

Views: 449

Answers (2)

SF23
SF23

Reputation: 184

The Problem was the multithreading part. After converting the paint part into a runnable that was invoked using Platform.runLater() it works fine.

Thank's to all who spent time on thinking about my problem.

Upvotes: 0

user2670200
user2670200

Reputation:

but sometimes it seems like the execution of the program gets stuck and the window freezes without any error.

JFX consumes lots of resources...especially with Canvas and Shapes (e.g. recursive Group, etc.) If it got stuck it could be the "stack" was on the brink of overflow. Try with -Xss[nnM] where nn is any number, M stands for Megabytes. Example -Xss16M : Stack with 16MB for JFX threads. If it still behaves "weirdly" then combine -Xss with -Xms (for the heap).

Upvotes: 0

Related Questions