Created
December 25, 2015 13:47
-
-
Save skrb/705d5812702d84ac1751 to your computer and use it in GitHub Desktop.
Lens Effect
This file contains hidden or 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.scene.Scene; | |
import javafx.scene.effect.DisplacementMap; | |
import javafx.scene.effect.Effect; | |
import javafx.scene.effect.FloatMap; | |
import javafx.scene.image.Image; | |
import javafx.scene.image.ImageView; | |
import javafx.scene.layout.StackPane; | |
import javafx.scene.paint.Color; | |
import javafx.scene.shape.Rectangle; | |
import javafx.stage.Stage; | |
import static javafx.application.Application.launch; | |
public class LensEffect extends Application { | |
@Override | |
public void start(Stage stage) throws Exception { | |
StackPane root = new StackPane(); | |
root.setPrefSize(370, 620); | |
Rectangle rect = new Rectangle(350, 600); | |
rect.setFill(null); | |
rect.setStroke(Color.RED); | |
rect.setStrokeWidth(4); | |
root.getChildren().add(rect); | |
ImageView view = new ImageView(new Image(getClass().getResource("cat.jpg").toString())); | |
view.setEffect(createEffect(350, 600)); | |
root.getChildren().add(view); | |
Scene scene = new Scene(root); | |
stage.setScene(scene); | |
stage.show(); | |
} | |
private Effect createEffect(int width, int height) { | |
FloatMap floatMap = new FloatMap(width, height); | |
for (int x = 0; x < width; x++) { | |
float u = (float) Math.sin(Math.PI * x / width * 2.0) * 0.05f; | |
for (int y = 0; y < height; y++) { | |
float v = (float) Math.sin(Math.PI * y / height * 2.0) * 0.05f; | |
floatMap.setSamples(x, y, u, 0.0f); | |
} | |
} | |
DisplacementMap displacementMap = new DisplacementMap(floatMap); | |
return displacementMap; | |
} | |
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