Java Code Examples for javafx.animation.AnimationTimer#start()

The following examples show how to use javafx.animation.AnimationTimer#start() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: Main.java    From FXTutorials with MIT License 8 votes vote down vote up
@Override
public void start(Stage primaryStage) throws Exception {
    Scene scene = new Scene(appRoot);
    scene.setOnKeyPressed(event -> keys.put(event.getCode(), true));
    scene.setOnKeyReleased(event -> keys.put(event.getCode(), false));
    primaryStage.setTitle("Tutorial 14 Platformer");
    primaryStage.setScene(scene);
    primaryStage.show();

    AnimationTimer timer = new AnimationTimer() {
        @Override
        public void handle(long now) {
            update();
        }
    };
    timer.start();
}
 
Example 2
Source File: Main.java    From FXTutorials with MIT License 7 votes vote down vote up
@Override
public void start(Stage primaryStage) throws Exception {
    initContent();

    Scene scene = new Scene(appRoot);
    scene.setOnKeyPressed(event -> keys.put(event.getCode(), true));
    scene.setOnKeyReleased(event -> keys.put(event.getCode(), false));
    primaryStage.setTitle("Tutorial 14 Platformer");
    primaryStage.setScene(scene);
    primaryStage.show();

    AnimationTimer timer = new AnimationTimer() {
        @Override
        public void handle(long now) {
            if (running) {
                update();
            }

            if (dialogEvent) {
                dialogEvent = false;
                keys.keySet().forEach(key -> keys.put(key, false));

                GameDialog dialog = new GameDialog();
                dialog.setOnCloseRequest(event -> {
                    if (dialog.isCorrect()) {
                        System.out.println("Correct");
                    }
                    else {
                        System.out.println("Wrong");
                    }

                    running = true;
                });
                dialog.open();
            }
        }
    };
    timer.start();
}
 
Example 3
Source File: FroggerApp.java    From FXTutorials with MIT License 6 votes vote down vote up
private Parent createContent() {
    root = new Pane();
    root.setPrefSize(800, 600);

    frog = initFrog();

    root.getChildren().add(frog);

    timer = new AnimationTimer() {
        @Override
        public void handle(long now) {
            onUpdate();
        }
    };
    timer.start();

    return root;
}
 
Example 4
Source File: SpaceInvadersApp.java    From FXTutorials with MIT License 6 votes vote down vote up
private Parent createContent() {
    root.setPrefSize(600, 800);

    root.getChildren().add(player);

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

    timer.start();

    nextLevel();

    return root;
}
 
Example 5
Source File: ParticlesClockApp.java    From FXTutorials with MIT License 6 votes vote down vote up
private Parent createContent() {

        Pane root = new Pane();
        root.setPrefSize(800, 600);

        Canvas canvas = new Canvas(800, 600);
        g = canvas.getGraphicsContext2D();
        g.setFill(Color.BLUE);

        root.getChildren().add(canvas);

        populateDigits();
        populateParticles();

        AnimationTimer timer = new AnimationTimer() {
            @Override
            public void handle(long now) {
                onUpdate();
            }
        };
        timer.start();

        return root;
    }
 
Example 6
Source File: Main.java    From FXTutorials with MIT License 6 votes vote down vote up
@Override
public void start(Stage primaryStage) throws Exception {
    Scene scene = new Scene(createContent());
    scene.setOnMouseClicked(event -> {
        bullet.setTarget(event.getSceneX(), event.getSceneY());
    });

    primaryStage.setTitle("Tutorial");
    primaryStage.setScene(scene);
    primaryStage.show();
    AnimationTimer timer = new AnimationTimer() {
        @Override
        public void handle(long now) {
            bullet.move();
        }
    };
    timer.start();
}
 
Example 7
Source File: AlgorithmApp.java    From FXTutorials with MIT License 6 votes vote down vote up
private Parent createContent() {
    Pane root = new Pane();

    Canvas canvas = new Canvas(W, H);
    g = canvas.getGraphicsContext2D();
    root.getChildren().add(canvas);

    for (int y = 0; y < grid[0].length; y++) {
        for (int x = 0; x < grid.length; x++) {
            grid[x][y] = new Tile(x, y);
        }
    }

    AnimationTimer timer = new AnimationTimer() {
        @Override
        public void handle(long now) {
            update();
        }
    };
    timer.start();

    algorithm = AlgorithmFactory.breadthFirst();
    algorithmThread.scheduleAtFixedRate(this::algorithmUpdate, 0, 1, TimeUnit.MILLISECONDS);

    return root;
}
 
Example 8
Source File: DrawingApp.java    From FXTutorials with MIT License 6 votes vote down vote up
private Parent createContent() {
    Pane root = new Pane();
    root.setPrefSize(800, 600);

    Canvas canvas = new Canvas(800, 600);
    g = canvas.getGraphicsContext2D();

    AnimationTimer timer = new AnimationTimer() {
        @Override
        public void handle(long now) {
            t += 0.017;
            draw();
        }
    };
    timer.start();

    root.getChildren().add(canvas);
    return root;
}
 
Example 9
Source File: AsteroidsApp.java    From FXTutorials with MIT License 6 votes vote down vote up
private Parent createContent() {
    root = new Pane();
    root.setPrefSize(600, 600);

    player = new Player();
    player.setVelocity(new Point2D(1, 0));
    addGameObject(player, 300, 300);

    AnimationTimer timer = new AnimationTimer() {
        @Override
        public void handle(long now) {
            onUpdate();
        }
    };
    timer.start();

    return root;
}
 
Example 10
Source File: MainMenu.java    From mars-sim with GNU General Public License v3.0 5 votes vote down vote up
public void createMovingStarfield() {
	for (int i = 0; i < STAR_COUNT; i++) {
		nodes[i] = new Rectangle(1, 1, Color.DARKGOLDENROD);// .WHITE);
		angles[i] = 2.0 * Math.PI * random.nextDouble();
		start[i] = random.nextInt(TIME);
	}

	starsTimer = new AnimationTimer() {
		@Override
		public void handle(long now) {
			final double width = 0.25 * WIDTH;// primaryStage.getWidth();
			final double height = 0.5 * HEIGHT;// primaryStage.getHeight();
			final double radius = Math.sqrt(2) * Math.max(width, height);
			for (int i = 0; i < STAR_COUNT; i++) {
				final Node node = nodes[i];
				final double angle = angles[i];
				final long t = (now - start[i]) % TIME;
				final double d = t * radius / TIME;
				node.setTranslateX(Math.cos(angle) * d + width);
				node.setTranslateY(Math.sin(angle) * d + height);
			}
		}
	};

	starsTimer.start();

}
 
Example 11
Source File: Led.java    From Enzo with Apache License 2.0 5 votes vote down vote up
public Led(final Color LED_COLOR, final boolean FRAME_VISIBLE, final long INTERVAL, final boolean BLINKING) {
    getStylesheets().add(Led.class.getResource("led.css").toExternalForm());
    getStyleClass().add("led");
    _ledColor     = LED_COLOR;
    _frameVisible = FRAME_VISIBLE;
    _interval     = INTERVAL;
    _blinking     = BLINKING;
    toggle        = false;
    lastTimerCall = System.nanoTime();
    timer = new AnimationTimer() {
        @Override public void handle(final long NOW) {
            if (NOW > lastTimerCall + getInterval()) {
                toggle ^= true;
                setOn(toggle);
                lastTimerCall = NOW;
            }
        }
    };
    init();
    initGraphics();
    registerListeners();
    if (_blinking) {
        timer.start();
    } else {
        timer.stop();
    }
}
 
Example 12
Source File: Snake3dApp.java    From FXTutorials with MIT License 5 votes vote down vote up
private Scene createScene() {
    Cube cube = new Cube(Color.BLUE);
    snake.getChildren().add(cube);

    moveFood();

    root.getChildren().addAll(snake, food);

    Scene scene = new Scene(root, 1280, 720, true);

    PerspectiveCamera camera = new PerspectiveCamera(true);
    camera.getTransforms().addAll(new Translate(0, -20, -20), new Rotate(-45, Rotate.X_AXIS));

    scene.setCamera(camera);

    timer = new AnimationTimer() {
        @Override
        public void handle(long now) {
            t += 0.016;

            if (t > 0.1) {
                onUpdate();
                t = 0;
            }
        }
    };

    timer.start();

    return scene;
}
 
Example 13
Source File: GUIProfiler.java    From bisq with GNU Affero General Public License v3.0 5 votes vote down vote up
public static void init() {
    AnimationTimer fpsTimer = new AnimationTimer() {
        @Override
        public void handle(long l) {
            long elapsed = (System.currentTimeMillis() - lastFPSTime);
            if (elapsed > 50)
                log.trace("Profiler: last frame used {}ms", elapsed);

            lastFPSTime = System.currentTimeMillis();
        }
    };
    fpsTimer.start();
}
 
Example 14
Source File: Skybox.java    From FXyzLib with GNU General Public License v3.0 5 votes vote down vote up
private void startTimer(){
    timer = new AnimationTimer() {
        @Override
        public void handle(long now) {
            Transform ct = (camera != null) ? camera.getLocalToSceneTransform() : null;
            if(ct != null){
                affine.setTx(ct.getTx());
                affine.setTy(ct.getTy());
                affine.setTz(ct.getTz());
            }
        }
    };
    timer.start();
}
 
Example 15
Source File: ClockSkin.java    From Enzo with Apache License 2.0 4 votes vote down vote up
public ClockSkin(final Clock CONTROL) {
    super(CONTROL);
    nightDayStyleClass        = getSkinnable().isNightMode() ? "night-mode" : "day-mode";

    hourPointerWidthFactor    = 0.04;
    hourPointerHeightFactor   = 0.55;
    minutePointerWidthFactor  = 0.04;
    minutePointerHeightFactor = 0.4;
    secondPointerWidthFactor  = 0.075;
    secondPointerHeightFactor = 0.46;

    majorTickWidthFactor  = 0.04;
    majorTickHeightFactor = 0.12;
    minorTickWidthFactor  = 0.01;
    minorTickHeightFactor = 0.05;

    majorTickOffset = 0.018;
    minorTickOffset = 0.05;

    //tickLabelFont      = Font.loadFont(getClass().getResourceAsStream("/eu/hansolo/enzo/fonts/helvetica.ttf"), 12);
    tickLabelFont      = Font.loadFont(getClass().getResourceAsStream("/eu/hansolo/enzo/fonts/bebasneue.ttf"), 12);
    minute             = new SimpleDoubleProperty(0);
    currentMinuteAngle = new SimpleDoubleProperty(0);

    hourAngle = new Rotate();
    hourAngle.angleProperty().bind(currentMinuteAngle);
    minuteAngle = new Rotate();
    secondAngle = new Rotate();

    ticks      = new ArrayList<>(60);
    tickLabels = new ArrayList<>(12);

    timeline = new Timeline();
    timer = new AnimationTimer() {
        @Override public void handle(final long NOW) {
            if (NOW >= lastTimerCall + INTERVAL) {
                updateTime(LocalDateTime.now().plus(getSkinnable().getOffset()));                    
                lastTimerCall = NOW;
            }
        }
    };
    minute.addListener(observable -> moveMinutePointer(minute.get()) );

    init();
    initGraphics();
    registerListeners();
    if (getSkinnable().isRunning()) {
        timer.start();
    } else {
        updateTime(LocalDateTime.now().plus(getSkinnable().getOffset()));
    }
}
 
Example 16
Source File: GraphPanel.java    From charts with Apache License 2.0 4 votes vote down vote up
private void init() {
    width              = PREFERRED_WIDTH;
    height             = PREFERRED_HEIGHT;
    mouseHandler       = this::handleMouseEvents;
    lastTimerCall      = System.nanoTime();
    _physicsActive     = true;
    _forceInverted     = false;

    setInitialPosition((int)width, (int)height);

    timer              = new AnimationTimer() {
        @Override public void handle(final long now) {
            if (now > lastTimerCall + REFRESH_PERIOD) {
                fruchtermanReingold();
                lastTimerCall = now;
                redraw();
            }
        }
    };

    nodeChangeListener    = (o, ov, nv) -> redraw();
    temp                  = BASE_TEMPERATURE;
    area                  = width * height;
    k                     = Math.sqrt(area / nodeEdgeModel.getNodes().size());
    distanceScalingFactor = new SimpleDoubleProperty(DISTANCE_SCALING_FACTOR);
    _edgeColor            = Color.DARKGRAY;
    _edgeWidthFactor = 2;
    //_nodeBorderColor       = Color.WHITE;
    _nodeHighlightingColor = Color.RED;
    _nodeBorderWidth       = 3;
    _selectedNodeFillColor   = Color.BLACK;
    _selectedNodeBorderColor = Color.LIME;
    maxXPosition = 1;
    maxYPosition = 1;
    minXPosition = -1;
    minYPosition = -1;


    if(null != nodeEdgeModel.getCurrentGroupKey()){
        GROUPING_KEY = nodeEdgeModel.getCurrentGroupKey();
    } else{
        GROUPING_KEY = NodeEdgeModel.DEFAULT;
    }

    popup                    = new InfoPopup();


    initGraphics();
    registerListeners();
    timer.start();
}
 
Example 17
Source File: TetrisApp.java    From FXTutorials with MIT License 4 votes vote down vote up
private Parent createContent() {
        Pane root = new Pane();
        root.setPrefSize(GRID_WIDTH * TILE_SIZE, GRID_HEIGHT * TILE_SIZE);

        Canvas canvas = new Canvas(GRID_WIDTH * TILE_SIZE, GRID_HEIGHT * TILE_SIZE);
        g = canvas.getGraphicsContext2D();

        root.getChildren().addAll(canvas);

//        original.add(new Tetromino(Color.BLUE,
//                new Piece(0, Direction.DOWN),
//                new Piece(1, Direction.LEFT),
//                new Piece(1, Direction.RIGHT),
//                new Piece(2, Direction.RIGHT)
//        ));
//        original.add(new Tetromino(Color.RED,
//                new Piece(0, Direction.DOWN),
//                new Piece(1, Direction.LEFT),
//                new Piece(1, Direction.RIGHT),
//                new Piece(1, Direction.DOWN)
//        ));
//
        original.add(new Tetromino(Color.GREEN,
                new Piece(0, Direction.DOWN),
                new Piece(1, Direction.RIGHT),
                new Piece(2, Direction.RIGHT),
                new Piece(1, Direction.DOWN)));

        original.add(new Tetromino(Color.GRAY,
                new Piece(0, Direction.DOWN),
                new Piece(1, Direction.RIGHT),
                new Piece(1, Direction.RIGHT, Direction.DOWN),
                new Piece(1, Direction.DOWN)));

        spawn();

        AnimationTimer timer = new AnimationTimer() {
            @Override
            public void handle(long now) {
                time += 0.017;

                if (time >= 0.5) {
                    update();
                    render();
                    time = 0;
                }
            }
        };
        timer.start();

        return root;
    }
 
Example 18
Source File: MorphingClockSkin.java    From Medusa with Apache License 2.0 4 votes vote down vote up
public MorphingClockSkin(Clock clock) {
    super(clock);
    hourColor      = clock.getHourColor();
    hourOffColor   = Helper.getTranslucentColorFrom(hourColor, 0.15);
    minuteColor    = clock.getMinuteColor();
    minuteOffColor = Helper.getTranslucentColorFrom(minuteColor, 0.15);
    secondColor    = clock.getSecondColor();
    secondOffColor = Helper.getTranslucentColorFrom(secondColor, 0.15);
    hl             = new int[15][8]; // hour left digit
    hr             = new int[15][8]; // hour right digit
    ml             = new int[15][8]; // minute left digit
    mr             = new int[15][8]; // minute right digit
    sl             = new int[15][8]; // second left digit
    sr             = new int[15][8]; // second right digit
    hourLeft       = 0;
    hourRight      = 0;
    minLeft        = 0;
    minRight       = 0;
    secLeft        = 0;
    secRight       = 0;
    step           = 0;
    lastTimerCall  = System.nanoTime();
    timer          = new AnimationTimer() {
        @Override public void handle(final long now) {
            if (now > lastTimerCall + INTERVAL) {
                if (hourLeft  != oldHourLeft)  hl = animateArray(hourLeft, step);
                if (hourRight != oldHourRight) hr = animateArray(hourRight, step);
                if (minLeft   != oldMinLeft)   ml = animateArray(minLeft, step);
                if (minRight  != oldMinRight)  mr = animateArray(minRight, step);
                if (secLeft   != oldSecLeft)   sl = animateArray(secLeft, step);
                if (secRight  != oldSecRight)  sr = animateArray(secRight, step);
                drawTime();
                step++;
                if (step > 7) {
                    step = 0;
                    updateArrays();
                    this.stop();
                }
                lastTimerCall = now;
            }
        }
    };

    initGraphics();
    registerListeners();

    timer.start();
}