Created
February 4, 2016 08:10
-
-
Save HanSolo/f25412352ff7ec5b9b53 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import javafx.animation.AnimationTimer; | |
import javafx.application.Application; | |
import javafx.geometry.Insets; | |
import javafx.scene.canvas.Canvas; | |
import javafx.scene.canvas.GraphicsContext; | |
import javafx.scene.paint.Color; | |
import javafx.stage.Stage; | |
import javafx.scene.layout.StackPane; | |
import javafx.scene.Scene; | |
/** | |
* User: hansolo | |
* Date: 04.02.16 | |
* Time: 09:02 | |
*/ | |
public class BlinkTest extends Application { | |
private Canvas canvas; | |
private GraphicsContext ctx; | |
private boolean toggle; | |
private long lastTimerCall; | |
private AnimationTimer timer; | |
@Override public void init() { | |
canvas = new Canvas(200, 200); | |
ctx = canvas.getGraphicsContext2D(); | |
toggle = true; | |
lastTimerCall = System.nanoTime(); | |
timer = new AnimationTimer() { | |
@Override public void handle(long now) { | |
if (now > lastTimerCall + 500_000_000l) { | |
blink(toggle); | |
toggle = !toggle; | |
lastTimerCall = now; | |
} | |
} | |
}; | |
} | |
@Override public void start(Stage stage) { | |
StackPane pane = new StackPane(canvas); | |
pane.setPadding(new Insets(10)); | |
Scene scene = new Scene(pane); | |
stage.setScene(scene); | |
stage.show(); | |
timer.start(); | |
} | |
private void blink(boolean on) { | |
if (on) { | |
ctx.setStroke(Color.RED); | |
ctx.strokeLine(50, 100, 150, 100); | |
} else { | |
ctx.clearRect(49, 99, 151, 101); | |
} | |
} | |
@Override public void stop() { | |
System.exit(0); | |
} | |
public static void main(String[] args) { | |
launch(args); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment