Created
December 6, 2011 05:56
Reflect an image in JavaFX using a 180 degree rotation about the Y Axis
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.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(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
https://forums.oracle.com/forums/post!reply.jspa?messageID=10015123