Wednesday, September 18, 2013

JavaFX BoxBlur effect

javafx.scene.effect.BoxBlur using a simple box filter kernel, with separately configurable sizes in both dimensions. And also an iteration parameter that controls the quality of the resulting blur.

BoxBlur effect
BoxBlur effect

package javafx_boxblur;

import javafx.application.Application;
import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.scene.Scene;
import javafx.scene.control.Label;
import javafx.scene.control.Slider;
import javafx.scene.control.SliderBuilder;
import javafx.scene.control.Tooltip;
import javafx.scene.effect.BoxBlur;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.HBox;
import javafx.scene.layout.StackPane;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;

/**
 *
 * @web http://java-buddy.blogspot.com/
 */
public class JavaFX_BoxBlur extends Application {
    
    @Override
    public void start(Stage primaryStage) {
        Image image = new Image("http://goo.gl/kYEQl");
        ImageView imageView = new ImageView();
        imageView.setImage(image);
        Label label = new Label("Java-buddy");
        HBox hBox = new HBox();
        hBox.getChildren().addAll(imageView, label);
        
        final BoxBlur boxBlur = new BoxBlur();
        hBox.setEffect(boxBlur);
        
        Slider sliderWidth = SliderBuilder.create()
                .layoutX(50)
                .layoutY(50)
                .prefWidth(400)
                .min(0)
                .max(255)
                .majorTickUnit(20)
                .minorTickCount(3)
                .showTickMarks(true)
                .showTickLabels(true)
                .value(5)
                .tooltip(new Tooltip("BoxBlur.width"))
                .build();
        sliderWidth.valueProperty().addListener(new ChangeListener<Number>() {

            @Override
            public void changed(ObservableValue<? extends Number> ov, 
                    Number t, Number t1) {
                boxBlur.setWidth(t1.doubleValue());
            }
        });
        
        Slider sliderHeight = SliderBuilder.create()
                .layoutX(50)
                .layoutY(50)
                .prefWidth(400)
                .min(0)
                .max(255)
                .majorTickUnit(20)
                .minorTickCount(3)
                .showTickMarks(true)
                .showTickLabels(true)
                .value(5)
                .tooltip(new Tooltip("BoxBlur.height"))
                .build();
        sliderHeight.valueProperty().addListener(new ChangeListener<Number>() {

            @Override
            public void changed(ObservableValue<? extends Number> ov, 
                    Number t, Number t1) {
                boxBlur.setHeight(t1.doubleValue());
            }
        });
        
        VBox vBox = new VBox();
        vBox.getChildren().addAll(hBox, sliderWidth, sliderHeight);
 
        StackPane root = new StackPane();
        root.getChildren().add(vBox);
        Scene scene = new Scene(root, 300, 300);
        primaryStage.setTitle("java-buddy.blogspot.com");
        primaryStage.setScene(scene);
        primaryStage.show();
    }

    public static void main(String[] args) {
        launch(args);
    }
}

No comments:

Post a Comment