package application_fx; import java.io.File; import javafx.application.Application; import javafx.scene.Node; import javafx.scene.Scene; import javafx.scene.effect.Bloom; import javafx.scene.effect.BoxBlur; import javafx.scene.effect.ColorAdjust; import javafx.scene.effect.ColorInput; import javafx.scene.effect.DisplacementMap; import javafx.scene.effect.DropShadow; import javafx.scene.effect.FloatMap; import javafx.scene.effect.GaussianBlur; import javafx.scene.effect.Glow; import javafx.scene.effect.ImageInput; import javafx.scene.effect.InnerShadow; import javafx.scene.effect.Light.Distant; import javafx.scene.effect.Lighting; import javafx.scene.effect.MotionBlur; import javafx.scene.effect.PerspectiveTransform; import javafx.scene.effect.Reflection; import javafx.scene.effect.SepiaTone; import javafx.scene.effect.Shadow; import javafx.scene.image.Image; import javafx.scene.image.ImageView; import javafx.scene.layout.HBox; import javafx.scene.layout.VBox; import javafx.scene.paint.Color; import javafx.scene.text.Font; import javafx.scene.text.FontWeight; import javafx.scene.text.Text; import javafx.stage.Stage; public class TestEffect extends Application { public static void main(String[] args) { launch( args ); } @Override public void start(Stage primaryStage) throws Exception { // シーングラフの構成 VBox root = new VBox(); // 画像読み込み Image img = new Image( new File( "img/chara_one.png" ).toURI().toString() ); // エフェクトなし ImageView imgView = new ImageView( img ); root.getChildren().add( imgView ); // 各エフェクトサンプルを追加 root.getChildren().add( createBoxBlur( img ) ); // ボックスぼかし画像の作成 root.getChildren().add( createMotionBlur( img ) ); // モーションぼかし root.getChildren().add( createGaussianBlur( img ) ); // ガウスぼかし root.getChildren().add( createShadow( img ) ); // シャドウ root.getChildren().add( createDropShadow( img ) ); // ドロップシャドウ root.getChildren().add( createInnerShadow( img ) ); // インナーシャドウ root.getChildren().add( createColorAdjust( img ) ); // カラー・アジャスト root.getChildren().add( createSepiaTone( img ) ); // セピアトーン root.getChildren().add( createColorInput( img ) ); // カラー・インプット root.getChildren().add( createImageInput( img ) ); // イメージ・インプット root.getChildren().add( createReflection( img ) ); // リフレクション root.getChildren().add( createLighting(img) ); // ライティング root.getChildren().add( createBloom( img ) ); // ブルーム root.getChildren().add( createGlow( img ) ); // グロー root.getChildren().add( createDisplacementMap( img ) ); // ディスプレースメント・マップ root.getChildren().add( createPerspective( img ) ); // パースペクティブ root.getChildren().add( createComplex( img ) ); // 複合エフェクト(ドロップシャドウ+リフレクション) // シーンの作成 Scene scene = new Scene( root , 700 , 700 , Color.web( "9FCC7F" ) ); // ウィンドウ表示 primaryStage.setScene( scene ); primaryStage.show(); } /** * ボックスブラーのサンプルを作成 * @param img * @return */ public Node createBoxBlur( Image img ) { // 水平レイアウトを作成 HBox root = new HBox( 10.0f ); // ボックスブラーエフェクトを作成 // ボックスぼかし効果を3回に設定 BoxBlur bb = new BoxBlur(); bb.setWidth( 2 ); bb.setHeight( 2 ); bb.setIterations( 3 ); // 表示オブジェクトを作成 Text explainText = new Text( "Box Blur" ); Text text = new Text( "Box Blur" ); ImageView blurView = new ImageView( img ); explainText.setFont( Font.font( null , FontWeight.BOLD , 16 ) ); text.setFont( Font.font( null , FontWeight.BOLD , 16 ) ); text.setFill( Color.CHOCOLATE ); // エフェクトを適用 text.setEffect( bb ); blurView.setEffect( bb ); // ノードを追加 root.getChildren().addAll( explainText , text , blurView ); // 戻り値を返す return root; } /** * モーションブラーのサンプルを作成 * @param img * @return */ public Node createMotionBlur( Image img ) { // 水平レイアウトを作成 HBox root = new HBox( 10.0f ); // モーションブラーエフェクトを作成 // ぼかしカーネル直径3, 角度45度に設定 MotionBlur mb = new MotionBlur(); mb.setRadius( 3.0f ); mb.setAngle( 45.0f ); // 表示オブジェクトを作成 Text explainText = new Text( "Motion Blur" ); Text text = new Text( "Motion Blur" ); ImageView mBlurView = new ImageView( img ); explainText.setFont( Font.font( null , FontWeight.BOLD , 16 ) ); text.setFont( Font.font( null , FontWeight.BOLD , 16 ) ); text.setFill( Color.CHOCOLATE ); // エフェクトを適用 text.setEffect( mb ); mBlurView.setEffect( mb ); // ノードを追加 root.getChildren().addAll( explainText , text , mBlurView ); // 戻り値を返す return root; } /** * ガウスブラーのサンプルを作成 * @param img * @return */ public Node createGaussianBlur( Image img ) { // 水平レイアウトを作成 HBox root = new HBox( 10.0f ); // シャドウ・エフェクトを作成 GaussianBlur gb = new GaussianBlur(); // 表示オブジェクトを作成 Text explainText = new Text( "Gaussian Blur" ); Text text = new Text( "Gaussian Blur" ); ImageView gBlurView = new ImageView( img ); explainText.setFont( Font.font( null , FontWeight.BOLD , 16 ) ); text.setFont( Font.font( null , FontWeight.BOLD , 16 ) ); text.setFill( Color.CHOCOLATE ); // エフェクトを適用 text.setEffect( gb ); gBlurView.setEffect( gb ); // ノードを追加 root.getChildren().addAll( explainText , text , gBlurView ); // 戻り値を返す return root; } /** * シャドウのサンプルを作成 * @param img * @return */ public Node createShadow( Image img ) { // 水平レイアウトを作成 HBox root = new HBox( 10.0f ); // ドロップシャドウ・エフェクトを作成 // 影の位置を(x,y)=(2,2)ずらした位置に設定し、 // 影の色を黒に設定 Shadow shadow = new Shadow(); shadow.setColor( Color.BLACK ); // 表示オブジェクトを作成 Text explainText = new Text( "Shadow" ); Text text = new Text( "Shadow" ); ImageView shadowView = new ImageView( img ); explainText.setFont( Font.font( null , FontWeight.BOLD , 16 ) ); text.setFont( Font.font( null , FontWeight.BOLD , 16 ) ); text.setFill( Color.CHOCOLATE ); // エフェクトを適用 text.setEffect( shadow ); shadowView.setEffect( shadow ); // ノードを追加 root.getChildren().addAll( explainText , text , shadowView ); // 戻り値を返す return root; } /** * ドロップシャドウのサンプルを作成 * @param img * @return */ public Node createDropShadow( Image img ) { // 水平レイアウトを作成 HBox root = new HBox( 10.0f ); // ドロップシャドウ・エフェクトを作成 // 影の位置を(x,y)=(2,2)ずらした位置に設定し、 // 影の色を黒に設定 DropShadow ds = new DropShadow(); ds.setOffsetX( 2.0f ); ds.setOffsetY( 2.0f ); ds.setColor( Color.BLACK ); // 表示オブジェクトを作成 Text explainText = new Text( "Drop Shadow" ); Text text = new Text( "Drop Shadow" ); ImageView dropView = new ImageView( img ); explainText.setFont( Font.font( null , FontWeight.BOLD , 16 ) ); text.setFont( Font.font( null , FontWeight.BOLD , 16 ) ); text.setFill( Color.CHOCOLATE ); // エフェクトを適用 text.setEffect( ds ); dropView.setEffect( ds ); // ノードを追加 root.getChildren().addAll( explainText , text , dropView ); // 戻り値を返す return root; } /** * インナーシャドウのサンプルを作成 * @param img * @return */ public Node createInnerShadow( Image img ) { // 水平レイアウトを作成 HBox root = new HBox( 10.0f ); // インナーシャドウ・エフェクトを作成 // 影の位置を(x,y)=(2,2)ずらした位置に設定し、 // 影の色を黒に設定 InnerShadow is = new InnerShadow(); is.setOffsetX( 2.0f ); is.setOffsetY( 2.0f ); is.setColor( Color.BLACK ); // 表示オブジェクトを作成 Text explainText = new Text( "Inner Shadow" ); Text text = new Text( "Inner Shadow" ); ImageView innerView = new ImageView( img ); explainText.setFont( Font.font( null , FontWeight.BOLD , 16 ) ); text.setFont( Font.font( null , FontWeight.BOLD , 16 ) ); text.setFill( Color.CHOCOLATE ); // エフェクトを適用 text.setEffect( is ); innerView.setEffect( is ); // ノードを追加 root.getChildren().addAll( explainText , text , innerView ); // 戻り値を返す return root; } /** * カラーアジャストのサンプルを作成 * @param img * @return */ public Node createColorAdjust( Image img ) { // 水平レイアウトを作成 HBox root = new HBox( 10.0f ); // カラーアジャスト・エフェクトを作成 ColorAdjust ca = new ColorAdjust(); ca.setContrast( 0.1f ); // コントラストの補正値( -1~1で表現。デフォルト0) ca.setHue( -1.0 ); // 色相の補正値( -1~1で表現。デフォルト0) ca.setBrightness( 0.1 ); // 明度・輝度の補正値( -1~1で表現。デフォルト0) ca.setSaturation( 0.2 ); // 彩度の補正値( -1~1で表現。デフォルト0) // 表示オブジェクトを作成 Text explainText = new Text( "Color Adjust" ); Text text = new Text( "Color Adjust" ); ImageView caView = new ImageView( img ); explainText.setFont( Font.font( null , FontWeight.BOLD , 16 ) ); text.setFont( Font.font( null , FontWeight.BOLD , 16 ) ); text.setFill( Color.CHOCOLATE ); // エフェクトを適用 text.setEffect( ca ); caView.setEffect( ca ); // ノードを追加 root.getChildren().addAll( explainText , text , caView ); // 戻り値を返す return root; } /** * セピアトーンのサンプルを作成 * @param img * @return */ public Node createSepiaTone( Image img ) { // 水平レイアウトを作成 HBox root = new HBox( 10.0f ); // セピアトーン・エフェクトを作成 SepiaTone st = new SepiaTone(); // 表示オブジェクトを作成 Text explainText = new Text( "Sepia Tone" ); Text text = new Text( "Sepia Tone" ); ImageView stView = new ImageView( img ); explainText.setFont( Font.font( null , FontWeight.BOLD , 16 ) ); text.setFont( Font.font( null , FontWeight.BOLD , 16 ) ); text.setFill( Color.CHOCOLATE ); // エフェクトを適用 text.setEffect( st ); stView.setEffect( st ); // ノードを追加 root.getChildren().addAll( explainText , text , stView ); // 戻り値を返す return root; } /** * カラーインプットのサンプルを作成 * @param img * @return */ public Node createColorInput( Image img ) { // 水平レイアウトを作成 HBox root = new HBox( 10.0f ); // カラーインプット・エフェクトを作成 ColorInput ci = new ColorInput( 0, 0 , 32 , 32 , Color.GRAY ); // 表示オブジェクトを作成 Text explainText = new Text( "Color Input" ); Text text = new Text( "Color Input" ); ImageView ciView = new ImageView( img ); explainText.setFont( Font.font( null , FontWeight.BOLD , 16 ) ); text.setFont( Font.font( null , FontWeight.BOLD , 16 ) ); text.setFill( Color.CHOCOLATE ); // エフェクトを適用 text.setEffect( ci ); ciView.setEffect( ci ); // ノードを追加 root.getChildren().addAll( explainText , text , ciView ); // 戻り値を返す return root; } /** * イメージインプットのサンプルを作成 * @param img * @return */ public Node createImageInput( Image img ) { // 水平レイアウトを作成 HBox root = new HBox( 10.0f ); // イメージインプット・エフェクトを作成 ImageInput ii = new ImageInput( img, 0 , 0 ); // 表示オブジェクトを作成 Text explainText = new Text( "Image Input" ); Text text = new Text( "Image Input" ); ImageView iiView = new ImageView( img ); explainText.setFont( Font.font( null , FontWeight.BOLD , 16 ) ); text.setFont( Font.font( null , FontWeight.BOLD , 16 ) ); text.setFill( Color.CHOCOLATE ); // エフェクトを適用 text.setEffect( ii ); iiView.setEffect( ii ); // ノードを追加 root.getChildren().addAll( explainText , text , iiView ); // 戻り値を返す return root; } /** * リフレクションのサンプルを作成 * @param img * @return */ public Node createReflection( Image img ) { // 水平レイアウトを作成 HBox root = new HBox( 10.0f ); // リフレクション・エフェクトを作成 // 反射率を0.9に設定 Reflection reflection = new Reflection(); reflection.setFraction( 0.9 ); // 表示オブジェクトを作成 Text explainText = new Text( "Reflection" ); Text text = new Text( "Reflection" ); ImageView reflectView = new ImageView( img ); explainText.setFont( Font.font( null , FontWeight.BOLD , 16 ) ); text.setFont( Font.font( null , FontWeight.BOLD , 16 ) ); text.setFill( Color.CHOCOLATE ); // エフェクトを適用 text.setEffect( reflection ); reflectView.setEffect( reflection ); // ノードを追加 root.getChildren().addAll( explainText , text , reflectView ); // 戻り値を返す return root; } /** * ディスプレイスメント・マップのサンプルを作成 * @param img * @return */ public Node createDisplacementMap( Image img ) { // 水平レイアウトを作成 HBox root = new HBox( 10.0f ); // ディスプレイスメント・マップ・エフェクトを作成 FloatMap floatMap = new FloatMap(); floatMap.setWidth( 250 ); floatMap.setHeight( 32 ); for( int i = 0 ; i < 250 ; i++ ) for( int j = 0 ; j < 32 ; j++ ) { double v = ( Math.sin( i / 20.0 * Math.PI ) - 0.5 ) / 40.0; floatMap.setSamples( i , j , 0.0f , (float) v ); } DisplacementMap dMap = new DisplacementMap(); dMap.setMapData( floatMap ); // 表示オブジェクトを作成 Text explainText = new Text( "Displacement Map" ); Text text = new Text( "Displacement Map" ); ImageView dMapView = new ImageView( img ); explainText.setFont( Font.font( null , FontWeight.BOLD , 16 ) ); text.setFont( Font.font( null , FontWeight.BOLD , 16 ) ); text.setFill( Color.CHOCOLATE ); // エフェクトを適用 text.setEffect( dMap ); dMapView.setEffect( dMap ); // ノードを追加 root.getChildren().addAll( explainText , text , dMapView ); // 戻り値を返す return root; } /** * ライティングのサンプルを作成 * @param img * @return */ public Node createLighting( Image img ) { // 水平レイアウトを作成 HBox root = new HBox( 10.0f ); // ライティング・エフェクトを作成 Distant light = new Distant(); Lighting lighting = new Lighting(); light.setAzimuth( -135.0f ); lighting.setLight( light ); lighting.setSurfaceScale( 5.0f ); // 表示オブジェクトを作成 Text explainText = new Text( "Lighting" ); Text text = new Text( "Lighting" ); ImageView lightView = new ImageView( img ); explainText.setFont( Font.font( null , FontWeight.BOLD , 16 ) ); text.setFont( Font.font( null , FontWeight.BOLD , 16 ) ); text.setFill( Color.CHOCOLATE ); // エフェクトを適用 text.setEffect( lighting ); lightView.setEffect( lighting ); // ノードを追加 root.getChildren().addAll( explainText , text , lightView ); // 戻り値を返す return root; } /** * ブルームのサンプルを作成 * @param img * @return */ public Node createBloom( Image img ) { // 水平レイアウトを作成 HBox root = new HBox( 10.0f ); // ブルーム・エフェクトを作成 Bloom bloom = new Bloom(); // 表示オブジェクトを作成 Text explainText = new Text( "Bloom" ); Text text = new Text( "Bloom" ); ImageView bloomView = new ImageView( img ); explainText.setFont( Font.font( null , FontWeight.BOLD , 16 ) ); text.setFont( Font.font( null , FontWeight.BOLD , 16 ) ); text.setFill( Color.CHOCOLATE ); // エフェクトを適用 text.setEffect( bloom ); bloomView.setEffect( bloom ); // ノードを追加 root.getChildren().addAll( explainText , text , bloomView ); // 戻り値を返す return root; } /** * グローのサンプルを作成 * @param img * @return */ public Node createGlow( Image img ) { // 水平レイアウトを作成 HBox root = new HBox( 10.0f ); // グロー・エフェクトを作成 Glow glow = new Glow(); // 表示オブジェクトを作成 Text explainText = new Text( "Glow" ); Text text = new Text( "Glow" ); ImageView glowView = new ImageView( img ); explainText.setFont( Font.font( null , FontWeight.BOLD , 16 ) ); text.setFont( Font.font( null , FontWeight.BOLD , 16 ) ); text.setFill( Color.CHOCOLATE ); // エフェクトを適用 text.setEffect( glow ); glowView.setEffect( glow ); // ノードを追加 root.getChildren().addAll( explainText , text , glowView ); // 戻り値を返す return root; } /** * パースペクティブのサンプルを作成 * @param img * @return */ public Node createPerspective( Image img ) { // 水平レイアウトを作成 HBox root = new HBox( 10.0f ); // パースペクティブ・エフェクトを作成 PerspectiveTransform pt = new PerspectiveTransform(); pt.setUlx(0.0f); pt.setUly(0.0f); pt.setUrx(32.0f); pt.setUry(12.0f); pt.setLlx(0.0f); pt.setLly(32.0f); pt.setLrx(32.0f); pt.setLry(20.0f); // 表示オブジェクトを作成 Text explainText = new Text( "Perspective" ); Text text = new Text( "Perspective" ); ImageView persView = new ImageView( img ); explainText.setFont( Font.font( null , FontWeight.BOLD , 16 ) ); text.setFont( Font.font( null , FontWeight.BOLD , 16 ) ); text.setFill( Color.CHOCOLATE ); // エフェクトを適用 text.setEffect( pt ); persView.setEffect( pt ); // ノードを追加 root.getChildren().addAll( explainText , text , persView ); // 戻り値を返す return root; } /** * 複合エフェクトのサンプルを作成 * @param img * @return */ public Node createComplex( Image img ) { // 水平レイアウトを作成 HBox root = new HBox( 10.0f ); // 2つのエフェクトを作成し合成する Reflection compReflect = new Reflection(); DropShadow compDS = new DropShadow(); compReflect.setFraction( 0.9 ); compDS.setOffsetX( 2.0f ); compDS.setOffsetY( 2.0f ); compDS.setColor( Color.BLACK ); compDS.setInput( compReflect ); // 表示オブジェクトを作成 Text explainText = new Text( "Complex( DropShadow + Reflection )" ); Text text = new Text( "Complex( DropShadow + Reflection )" ); ImageView complexView = new ImageView( img ); explainText.setFont( Font.font( null , FontWeight.BOLD , 16 ) ); text.setFont( Font.font( null , FontWeight.BOLD , 16 ) ); text.setFill( Color.CHOCOLATE ); // エフェクトを適用 text.setEffect( compDS ); complexView.setEffect( compDS ); // ノードを追加 root.getChildren().addAll( explainText , text , complexView ); // 戻り値を返す return root; } }