Created
May 20, 2013 18:59
-
-
Save tarrsalah/5614603 to your computer and use it in GitHub Desktop.
The ShakingStage.
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.animation.KeyFrame; | |
import javafx.animation.RotateTransitionBuilder; | |
import javafx.animation.Timeline; | |
import javafx.animation.TimelineBuilder; | |
import javafx.application.Application; | |
import javafx.event.ActionEvent; | |
import javafx.event.EventHandler; | |
import javafx.scene.Scene; | |
import javafx.scene.SceneBuilder; | |
import javafx.scene.control.Button; | |
import javafx.scene.control.ButtonBuilder; | |
import javafx.scene.layout.StackPaneBuilder; | |
import javafx.stage.Stage; | |
import javafx.util.Duration; | |
/** | |
* Created with IntelliJ IDEA. | |
* User: tarrsalah | |
* Date: 5/20/13 | |
* Time: 1:37 AM | |
* To change this template use File | Settings | File Templates. | |
*/ | |
public class ShakingStage extends Application { | |
public Stage primaryStage; | |
boolean xyState= true; | |
public static void main(String[] args) { | |
launch(args); | |
} | |
@Override | |
public void start(Stage stage) throws Exception { | |
primaryStage= stage; | |
Button shakeButton= ButtonBuilder.create() | |
.text("shake it") | |
.onAction(new EventHandler<ActionEvent>() { | |
@Override | |
public void handle(ActionEvent actionEvent) { | |
TimelineBuilder.create() | |
.keyFrames(new KeyFrame(Duration.seconds(0.2), new EventHandler<ActionEvent>() { | |
@Override | |
public void handle(ActionEvent actionEvent) { | |
if (xyState) { | |
primaryStage.setX(primaryStage.getX() + 10); | |
primaryStage.setY(primaryStage.getY() + 10); | |
} else { | |
primaryStage.setX(primaryStage.getX() - 10); | |
primaryStage.setY(primaryStage.getY() - 10); | |
} | |
xyState= !xyState; | |
} | |
})) | |
.autoReverse(true) | |
.cycleCount(Timeline.INDEFINITE) | |
.build().play(); | |
} | |
}) | |
.build(); | |
Scene scene= SceneBuilder.create() | |
.root(StackPaneBuilder.create() | |
.children(shakeButton) | |
.build() | |
) | |
.width(300) | |
.height(400) | |
.build(); | |
stage.setScene(scene); | |
stage.show(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment