Search code examples
javajavafxglow

javafx Glow shatter the animation


Im just starting to code and learn javafx and doing an easy project and my problem is after i add a Glow effect on my graphic context the animation slows down dramaticly.

/**
 * JavaFX rocks ;)
 */

public class CDLMatrix extends Application {
    Stage matrixStage;

    private final String characters = "Effect glow = new Glow(1.0); gc.setEffect(glow); WHY SHATTERS ?!";



    private final Random random = new Random();
    protected final Font font = Font.font("MS PGothic", FontWeight.BOLD, 15);
    char[] data = new char[2000 * 2000];
    int[] path = new int[2000 * 2000];
    private long lastTime = 0;

    int getNumberOfCharsPerRow() {
        return (int) matrixStage.getWidth() / 12;
    }

    int getNumberOfCharsPerColumn() {
        return (int) matrixStage.getHeight() / 12;
    }

    // takes random for now
    private char getChar() {
        return characters.charAt(Math.abs(random.nextInt()
                % characters.length()));
    }

    void update(long now) {
        if (lastTime == 0) {
            lastTime = now;
        }

        // fadeTime = how fast trail will fade out
        // flipRate = how fast trail chars will change

        final int fadeTime = 3;
        final float flipRate = 0.01f;
        final int fillStart = 100;
        final float fillRate = 0.01f;

        int numberOfCharsPerRow = getNumberOfCharsPerRow();
        int numberOfCharsPerColumn = getNumberOfCharsPerColumn();
        int numberOfChars = numberOfCharsPerRow * numberOfCharsPerColumn;

        for (int i = numberOfChars - 1; i >= 0; --i) {
            if (i + numberOfCharsPerRow < numberOfChars) {
                if (path[i] == 255) {
                    // This means char was just set
                    // Initialize the next row at this X
                    // position
                    path[i + numberOfCharsPerRow] = 255;
                    data[i + numberOfCharsPerRow] = getChar();
                }
            }

            // path[i] > 64 means if this char Green component > 25%
            if (path[i] > 64 && random.nextFloat() < flipRate) {
                data[i] = getChar();
            }

            // Decrement the char Green component
            if (path[i] > fadeTime) {
                path[i] -= fadeTime;
            } else {
                path[i] = 0;
            }

            // First row
            // Start doing stuff only if the Green component > 40%
            if (i < numberOfCharsPerRow && path[i] <= fillStart) {
                if (random.nextFloat() < fillRate) {
                    path[i] = 255;
                    data[i] = getChar();
                }
            }
        }

        lastTime = now;
    }

    @Override
    public void start(Stage stage) throws Exception {
        this.matrixStage = stage;
        matrixStage.setTitle("CDL Matrix");

        Group root = new Group();

        Scene scene = new Scene(root, 1024, 768);
        scene.addEventHandler(KeyEvent.KEY_PRESSED,
                new EventHandler<KeyEvent>() {
                    @Override
                    // F key for full screen ;)
                    public void handle(KeyEvent keyEvent) {
                        if (keyEvent.getCode() == KeyCode.F) {
                            matrixStage.setFullScreen(!matrixStage
                                    .isFullScreen());
                        }
                        // ctrl + Q = exit
                        if (keyEvent.isControlDown()
                                && keyEvent.getCode() == KeyCode.Q) {
                            matrixStage.close();
                        }
                    }
                });

        Canvas canvas = new Canvas();
        canvas.widthProperty().bind(matrixStage.widthProperty());
        canvas.heightProperty().bind(matrixStage.heightProperty());

        final GraphicsContext gc = canvas.getGraphicsContext2D();
        gc.setFont(font);

//      Effect glow = new Glow(1.0); <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<
//      gc.setEffect(glow);      <<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<

        new AnimationTimer() {
            @Override
            public void handle(long now) {
                update(now);

                gc.clearRect(0, 0, matrixStage.getWidth(),
                        matrixStage.getHeight());
                gc.setFill(Color.rgb(0, 1, 0));
                gc.fillRect(0, 0, matrixStage.getWidth(),
                        matrixStage.getHeight());

                int y = 0;
                int numberOfCharsPerRow = getNumberOfCharsPerRow();
                int numberOfCharsPerColumn = getNumberOfCharsPerColumn();

                // Colors
                for (int i = 0; i < numberOfCharsPerRow
                        * numberOfCharsPerColumn; ++i) {

                    gc.setFill(Color.rgb(0, path[i], 0));
                    String text = String.valueOf(data[i]);
                    gc.fillText(text, (i % numberOfCharsPerRow) * 12 + 1,
                            y + 13);

                    if (i % numberOfCharsPerRow == numberOfCharsPerRow - 1) {
                        y += 12;
                    }
                }
            }
        }.start();

        root.getChildren().add(canvas);

        matrixStage.setScene(scene);
        matrixStage.show();
    }
}

Solution

  • Apply the glow to the Canvas node rather than to the GraphicsContext.

    Animation will then occur at normal speed rather than slideshow speed.

    Change:

    gc.setEffect(glow); 
    

    To:

    canvas.setEffect(glow); 
    

    There must be something in the internal implementation of JavaFX which causes applying effects such as Glow to a GraphicsContext rather than a Node to be orders of magnitude less efficient. You might want to file an issue in the JavaFX issue tracker to have a developer look into it.

    Test environment: Java 8u40, OS X 10.9