Created
December 6, 2011 05:55
-
-
Save jewelsea/1436935 to your computer and use it in GitHub Desktop.
Reflect an image in JavaFX using an affine transform
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.Affine; | |
import javafx.stage.Stage; | |
/** reflects an image using an affine transformation matrix */ | |
public class ReflectionByAffineTransform extends Application { | |
public static void main(String[] args) throws Exception { launch(args); } | |
public void start(final Stage stage) throws Exception { | |
stage.setTitle("Reflection by Affine Transform"); | |
// create a refectable image. | |
final ImageView imageView = new ImageView(new Image("http://bluebuddies.com/smurf_fun/smurf_personality_test/jpg/Painter_Smurf.jpg")); | |
// create an transformation matrix for the reflection. | |
final Affine reflectTransform = new Affine(); | |
reflectTransform.setMxx(-1); | |
reflectTransform.setTx(imageView.getBoundsInLocal().getWidth()); | |
// 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.getTransforms().add(reflectTransform); | |
reflect.setText("Restore"); | |
} else { | |
imageView.getTransforms().clear(); | |
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/thread.jspa?messageID=10015123