Skip to content

Instantly share code, notes, and snippets.

@jewelsea
Created December 6, 2011 05:56
Show Gist options
  • Save jewelsea/1436941 to your computer and use it in GitHub Desktop.
Save jewelsea/1436941 to your computer and use it in GitHub Desktop.
Reflect an image in JavaFX using a 180 degree rotation about the Y Axis
import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.VBoxBuilder;
import javafx.scene.transform.Rotate;
import javafx.stage.Stage;
/** reflects an image using a 180 degree rotation about the Y axis */
public class ReflectionByRotation extends Application {
public static void main(String[] args) throws Exception { launch(args); }
public void start(final Stage stage) throws Exception {
stage.setTitle("Reflection by Rotation");
// create a refectable image.
final ImageView imageView = new ImageView(new Image("http://bluebuddies.com/smurf_fun/smurf_personality_test/jpg/Painter_Smurf.jpg"));
imageView.setTranslateZ(imageView.getBoundsInLocal().getWidth() / 2.0);
imageView.setRotationAxis(Rotate.Y_AXIS);
// reflect and restore the image when a button is clicked by applying and unapplying the transformation matrix.
final Button reflect = new Button("Reflect");
reflect.setOnAction(new EventHandler<ActionEvent>() {
@Override public void handle(ActionEvent actionEvent) {
if ("Reflect".equals(reflect.getText())) {
imageView.setRotate(180);
reflect.setText("Restore");
} else {
imageView.setRotate(0);
reflect.setText("Reflect");
}
}
});
// layout the scene.
stage.setScene(new Scene(
VBoxBuilder.create()
.spacing(10).alignment(Pos.CENTER)
.children(imageView, reflect)
.style("-fx-background-color: linear-gradient(to bottom, cornsilk, midnightblue); -fx-padding:10; -fx-font-size: 22;")
.build()
));
stage.show();
}
}
@jewelsea
Copy link
Author

jewelsea commented Dec 6, 2011

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment