Created
June 12, 2016 16:31
-
-
Save ChristophHaag/661be992429b451218e9ee1fb0eacdec to your computer and use it in GitHub Desktop.
This file contains 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.application.Application; | |
import javafx.scene.Group; | |
import javafx.scene.Scene; | |
import javafx.scene.image.Image; | |
import javafx.scene.image.ImageView; | |
import javafx.scene.paint.Color; | |
import javafx.stage.Stage; | |
import javafx.animation.AnimationTimer; | |
import javafx.scene.canvas.Canvas; | |
import javafx.scene.canvas.GraphicsContext; | |
import javafx.scene.image.WritableImage; | |
import javafx.scene.SnapshotParameters; | |
public class Jfxtest extends Application { | |
public static void main(String[] args) { | |
Application.launch(args); | |
} | |
@Override | |
public void start(Stage primaryStage) { | |
primaryStage.setTitle("Title"); | |
Group root = new Group(); | |
Scene scene = new Scene(root); | |
Canvas canvas = new Canvas(100, 100); //create some image | |
GraphicsContext gc = canvas.getGraphicsContext2D(); | |
gc.setFill(Color.GREEN); | |
gc.setStroke(Color.BLUE); | |
gc.setLineWidth(5); | |
gc.strokeLine(0, 0, 100, 100); | |
Image img = canvas.snapshot(new SnapshotParameters(), new WritableImage(100,100)); | |
ImageView imv = new ImageView(); | |
imv.setImage(img); | |
root.getChildren().add(imv); | |
primaryStage.setScene(scene); | |
primaryStage.show(); | |
primaryStage.setFullScreen(true); | |
AnimationTimer at = new AnimationTimer() { | |
int deltax = 1; | |
int deltay = 1; | |
@Override | |
public void handle(long now) { | |
if (imv.getTranslateX() > primaryStage.getWidth() - img.getWidth() || imv.getTranslateX() < 0) { | |
deltax = -deltax; | |
} | |
if (imv.getTranslateY() > primaryStage.getHeight() - img.getHeight() || imv.getTranslateY() < 0) { | |
deltay = -deltay; | |
} | |
imv.setTranslateX(imv.getTranslateX() + deltax); | |
imv.setTranslateY(imv.getTranslateY() + deltay); | |
imv.setRotate(imv.getRotate() + 1); | |
} | |
}; | |
at.start(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment