Skip to content

Instantly share code, notes, and snippets.

@miho
Created October 2, 2012 10:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save miho/3817986 to your computer and use it in GitHub Desktop.
Save miho/3817986 to your computer and use it in GitHub Desktop.
FXZoomTest01 (testing what happens if size of scaled nodes changes)
/*
* To change this template, choose Tools | Templates
* and open the template in the editor.
*/
package fxzoomtest01;
import javafx.animation.KeyFrame;
import javafx.animation.KeyValue;
import javafx.animation.ScaleTransition;
import javafx.animation.Timeline;
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.layout.HBox;
import javafx.scene.layout.Pane;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import javafx.util.Duration;
/**
*
* @author Michael Hoffer <info@michaelhoffer.de>
*/
public class FXZoomTest01 extends Application {
@Override
public void start(Stage primaryStage) {
VBox root = new VBox();
Pane pane = new Pane();
pane.setPrefSize(100, 300);
root.getChildren().add(pane);
// original node position and size
final Pane originalNode = new StackPane();
originalNode.setStyle(
"-fx-background-color: #aaaaaa; "
+ "-fx-text-fill: black; "
+ "-fx-border-color: black; "
+ "-fx-border-style: dotted");
originalNode.setPrefSize(100, 100);
originalNode.setLayoutX(100);
originalNode.setLayoutY(50);
originalNode.getChildren().add(new Text("original pos"));
pane.getChildren().add(originalNode);
// the node that shall be scaled
final Pane zoomedNode = new Pane();
zoomedNode.setStyle(
"-fx-background-color: #334455; "
+ "-fx-text-fill: black; "
+ "-fx-border-color: black;");
zoomedNode.setPrefSize(100, 100);
zoomedNode.setLayoutX(100);
zoomedNode.setLayoutY(50);
pane.getChildren().add(zoomedNode);
// button pane
HBox btnPane = new HBox(8);
root.getChildren().add(btnPane);
// buttons
Button scaleOnBtn = new Button();
scaleOnBtn.setText("Scale On");
scaleOnBtn.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
ScaleTransition st = new ScaleTransition(Duration.seconds(1), zoomedNode);
st.setFromX(zoomedNode.getScaleX());
st.setToX(0.3);
st.setFromY(zoomedNode.getScaleY());
st.setToY(0.3);
st.play();
}
});
btnPane.getChildren().add(scaleOnBtn);
Button scaleOffBtn = new Button();
scaleOffBtn.setText("Scale Off");
scaleOffBtn.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
ScaleTransition st = new ScaleTransition(Duration.seconds(1), zoomedNode);
st.setFromX(zoomedNode.getScaleX());
st.setToX(1);
st.setFromY(zoomedNode.getScaleY());
st.setToY(1);
st.play();
}
});
btnPane.getChildren().add(scaleOffBtn);
Button increaseSizeBtn = new Button();
increaseSizeBtn.setText("Increase Size");
increaseSizeBtn.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
KeyFrame kf0 = new KeyFrame(Duration.ZERO,
new KeyValue(zoomedNode.prefWidthProperty(),
zoomedNode.getWidth()));
KeyFrame kf1 = new KeyFrame(Duration.seconds(1),
new KeyValue(zoomedNode.prefWidthProperty(),
zoomedNode.getWidth()*2));
Timeline tl = new Timeline(kf0,kf1);
tl.play();
}
});
btnPane.getChildren().add(increaseSizeBtn);
Button decreaseSizeBtn = new Button();
decreaseSizeBtn.setText("Decrease Size");
decreaseSizeBtn.setOnAction(new EventHandler<ActionEvent>() {
@Override
public void handle(ActionEvent event) {
KeyFrame kf0 = new KeyFrame(Duration.ZERO,
new KeyValue(zoomedNode.prefWidthProperty(),
zoomedNode.getWidth()));
KeyFrame kf1 = new KeyFrame(Duration.seconds(1),
new KeyValue(zoomedNode.prefWidthProperty(),
zoomedNode.getWidth()/2.0f));
Timeline tl = new Timeline(kf0,kf1);
tl.play();
}
});
btnPane.getChildren().add(decreaseSizeBtn);
// finally, show the stage
Scene scene = new Scene(root, 400, 300);
primaryStage.setTitle("ZoomTest");
primaryStage.setScene(scene);
primaryStage.show();
}
/**
* The main() method is ignored in correctly deployed JavaFX application.
* main() serves only as fallback in case the application can not be
* launched through deployment artifacts, e.g., in IDEs with limited FX
* support. NetBeans ignores main().
*
* @param args the command line arguments
*/
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