package javafx8image; import javafx.application.Application; import javafx.beans.value.ChangeListener; import javafx.beans.value.ObservableValue; import javafx.collections.FXCollections; import javafx.collections.ObservableList; import javafx.scene.Scene; import javafx.scene.control.ListView; import javafx.scene.image.Image; import javafx.scene.image.ImageView; import javafx.scene.image.PixelReader; import javafx.scene.image.PixelWriter; import javafx.scene.image.WritableImage; import javafx.scene.layout.HBox; import javafx.scene.layout.StackPane; import javafx.scene.paint.Color; import javafx.stage.Stage; /** * * @web http://java-buddy.blogspot.com/ */ public class JavaFX8Image extends Application { @Override public void start(Stage primaryStage) { Image imageSrc = new Image("http://goo.gl/kYEQl"); ListView listView = new ListView(); ImageView imageViewDest = new ImageView(); ObservableList<String> items = FXCollections.observableArrayList ( "NO", "brighter", "darker", "saturate", "desaturate", "grayscale", "invert"); listView.setItems(items); listView.getSelectionModel().selectedItemProperty().addListener( new ChangeListener<String>(){ @Override public void changed(ObservableValue<? extends String> observable, String oldValue, String newValue) { WritableImage imageDest = copyImage(imageSrc, (String)listView.getSelectionModel().getSelectedItem()); imageViewDest.setImage(imageDest); } }); WritableImage imageDest = copyImage(imageSrc, (String)listView.getSelectionModel().getSelectedItem()); imageViewDest.setImage(imageDest); HBox hBox = new HBox(); hBox.getChildren().addAll(imageViewDest, listView); StackPane root = new StackPane(); root.getChildren().addAll(hBox); Scene scene = new Scene(root, 300, 250); primaryStage.setTitle("java-buddy.blogspot.com"); primaryStage.setScene(scene); primaryStage.show(); } public static void main(String[] args) { launch(args); } private WritableImage copyImage(Image src, String op) { PixelReader pixelReader = src.getPixelReader(); WritableImage dest = new WritableImage( (int) src.getWidth(), (int) src.getHeight()); PixelWriter pixelWriter = dest.getPixelWriter(); for (int y = 0; y < src.getHeight(); y++) { for (int x = 0; x < src.getWidth(); x++) { Color color = pixelReader.getColor(x, y); if(op!=null){ switch(op){ case "brighter": color = color.brighter(); break; case "darker": color = color.darker(); break; case "saturate": color = color.saturate(); break; case "desaturate": color = color.desaturate(); break; case "grayscale": color = color.grayscale(); break; case "invert": color = color.invert(); break; } } pixelWriter.setColor(x, y, color); } } return dest; } }
Wednesday, June 18, 2014
Various effect of creating Color with brighter(), darker(), saturate(), desaturate(), grayscale() and invert().
This example show various effect of creating Color when copying image, with brighter(), darker(), saturate(), desaturate(), grayscale() and invert().
No comments:
Post a Comment