JavaFX Tutorial - JavaFX Effect








Blend Effect

Blend is an effect that combines two inputs together using one of the predefined blending modes.

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.effect.BlendMode;
import javafx.scene.layout.HBox;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
// ww w. j  av a2  s.  c o m
public class Main extends Application {
  public static void main(String[] args) throws Exception {
    launch(args);
  }

  @Override
  public void start(final Stage stage) throws Exception {

    Rectangle r = new Rectangle();
    r.setX(590);
    r.setY(50);
    r.setWidth(50);
    r.setHeight(50);
    r.setFill(Color.BLUE);

    Circle c = new Circle();
    c.setFill(Color.RED);
    c.setCenterX(590);
    c.setCenterY(50);
    c.setRadius(25);
    c.setBlendMode(BlendMode.SRC_ATOP);

    Group g = new Group();
    g.setBlendMode(BlendMode.SRC_OVER);
    g.getChildren().add(r);
    g.getChildren().add(c);

    HBox box = new HBox();
    box.getChildren().add(g);

    Scene scene = new Scene(box, 400, 450);
    stage.setScene(scene);
    stage.show();
  }
}

The following code uses the COLOR_BURN Blend Mode.

Text text1 = new Text(25, 25, "java2s.com");
text1.setFill(Color.CHOCOLATE);
text1.setFont(Font.font(java.awt.Font.MONOSPACED, 35));
      
text1.setBlendMode(BlendMode.COLOR_BURN);   

The code above generates the following result.

null




BlendMode.MULTIPLY

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.effect.BlendMode;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
/*from w  w  w. ja va2 s . co  m*/
public class Main extends Application {
  public static void main(String[] args) {
    Application.launch(args);
  }

  @Override
  public void start(Stage primaryStage) {
    primaryStage.setTitle("Text Fonts");

    Group g = new Group();
    Scene scene = new Scene(g, 550, 250);

    Rectangle r = new Rectangle();
    r.setX(50);
    r.setY(50);
    r.setWidth(50);
    r.setHeight(50);
    r.setFill(Color.BLUE);

    Circle c = new Circle();
    c.setFill(Color.rgb(255, 0, 0, 0.5));
    c.setCenterX(50);
    c.setCenterY(50);
    c.setRadius(25);
    c.setBlendMode(BlendMode.MULTIPLY);

    g.getChildren().add(r);
    g.getChildren().add(c);

    primaryStage.setScene(scene);
    primaryStage.show();
  }
}

The code above generates the following result.

null




Blur Effects GaussianBlur

JavaFX supports boxblur, a motion blur, or a gaussian blur. Text with GaussianBlur.

The Gaussian blur uses a Gaussian algorithm with a configurable radius to blur objects.

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.effect.GaussianBlur;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.stage.Stage;
/*  ww w .jav  a 2 s .  c  o  m*/
public class Main extends Application {
  public static void main(String[] args) {
    Application.launch(args);
  }

  @Override
  public void start(Stage primaryStage) {
    primaryStage.setTitle("");
    Group root = new Group();
    Scene scene = new Scene(root, 300, 250, Color.WHITE);

    Group g = new Group();

    Text t = new Text();
    t.setX(10.0);
    t.setY(40.0);
    t.setCache(true);
    t.setText("Blurry Text");
    t.setFill(Color.RED);
    t.setFont(Font.font(null, FontWeight.BOLD, 36));

    t.setEffect(new GaussianBlur());
    
    g.getChildren().add(t);

    root.getChildren().add(g);
    primaryStage.setScene(scene);
    primaryStage.show();
  }
}

The code above generates the following result.

null

BoxBlur effect

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.effect.BoxBlur;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.stage.Stage;
//from  ww w .  j  a v  a 2s  .  com
public class Main extends Application {
    public static void main(String[] args) {
        Application.launch(args);
    }
    @Override
    public void start(Stage primaryStage) {
        primaryStage.setTitle("Text Fonts");
        Group root = new Group();
        Scene scene = new Scene(root, 550, 250,Color.web("0x0000FF",1.0));

        Text text = new Text(50, 100, "JavaFX 2.0 from Java2s.com");
        Font sanSerif = Font.font("Dialog", 30);
        text.setFont(sanSerif);
        text.setFill(Color.RED);
        root.getChildren().add(text);

        BoxBlur bb = new BoxBlur();
        bb.setWidth(15);
        bb.setHeight(15);
        bb.setIterations(3);

        text.setEffect(bb);
        primaryStage.setScene(scene);
        primaryStage.show();
    }
}

The code above generates the following result.

null

Motion Blur

With motion blur we can configure radius and angle to create the effect of a moving object.

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.effect.MotionBlur;
import javafx.scene.layout.HBox;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.stage.Stage;
/*from w ww . j  av a2 s  .  c om*/
public class Main extends Application {
  public static void main(String[] args) throws Exception {
    launch(args);
  }
  @Override
  public void start(final Stage stage) throws Exception {

    Text t = new Text();
    t.setX(20.0f);
    t.setY(80.0f);
    t.setText("Motion Blur");
    t.setFill(Color.RED);
    t.setFont(Font.font("Arial", FontWeight.BOLD, 60));

    MotionBlur mb = new MotionBlur();
    mb.setRadius(15.0f);
    mb.setAngle(45.0f);

    t.setEffect(mb);

    t.setTranslateX(10);
    t.setTranslateY(150);

    HBox box = new HBox();
    box.getChildren().add(t);

    Scene scene = new Scene(box, 400, 450);
    stage.setScene(scene);
    stage.show();
  }
}

The code above generates the following result.

null

Bloom Effect

The bloom effect makes brighter portions appear to glow, based on a configurable threshold.

The threshold is between 0.0 to 1.0. By default, the threshold is set to 0.3.

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.effect.Bloom;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.stage.Stage;
//from   ww  w.j av  a2  s . c  om
public class Main extends Application {
    public static void main(String[] args) {
        Application.launch(args);
    }
    @Override
    public void start(Stage primaryStage) {
        primaryStage.setTitle("Text Fonts");

        Group g = new Group();
        Scene scene = new Scene(g, 550, 250,Color.web("0x0000FF",1.0));

        Rectangle r = new Rectangle();
        r.setX(10);
        r.setY(10);
        r.setWidth(160);
        r.setHeight(80);
        r.setFill(Color.DARKBLUE);

        Text t = new Text();
        t.setText("Bloom!");
        t.setFill(Color.YELLOW);
        t.setFont(Font.font(null, FontWeight.BOLD, 36));
        t.setX(25);
        t.setY(65);

        g.setCache(true);
        g.setEffect(new Bloom());
        g.getChildren().add(r);
        g.getChildren().add(t);
        

        primaryStage.setScene(scene);
        primaryStage.show();
    }
}

The code above generates the following result.

null

Drop Shadow Effect

A drop shadow effect renders a shadow of the content. We can configure the color, the radius, the offset, and other parameters of the shadow.

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.effect.DropShadow;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.stage.Stage;
/*from   w  w w  . j a v  a 2s .  c  om*/
public class Main extends Application {
    public static void main(String[] args) {
        Application.launch(args);
    }
    @Override
    public void start(Stage primaryStage) {
        primaryStage.setTitle("Text Fonts");
        Group root = new Group();
        Scene scene = new Scene(root, 550, 250, Color.WHITE);

        Text text = new Text(150, 50, "JavaFX from Java2s.com");
        text.setFill(Color.BLUE);
        DropShadow dropShadow = new DropShadow();
        dropShadow.setOffsetX(2.0f);
        dropShadow.setOffsetY(4.0f);
        dropShadow.setColor(Color.rgb(150, 50, 50, .688));
        text.setEffect(dropShadow);
        
        root.getChildren().add(text);

        primaryStage.setScene(scene);
        primaryStage.show();
    }
}

The code above generates the following result.

null

Inner Shadow Effect

An inner shadow effect renders a shadow inside the content with the specified color, radius, and offset.

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.effect.InnerShadow;
import javafx.scene.layout.HBox;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.stage.Stage;
/* w w  w  . j  a va 2  s .c  om*/
public class Main extends Application {
  public static void main(String[] args) throws Exception {
    launch(args);
  }
  @Override
  public void start(final Stage stage) throws Exception {
    Text t = new Text();
    t.setX(20.0f);
    t.setY(80.0f);
    t.setText("java2s.com");
    t.setFill(Color.RED);
    t.setFont(Font.font("Arial", FontWeight.BOLD, 60));
    
    InnerShadow is = new InnerShadow();
    is.setOffsetX(2.0f);
    is.setOffsetY(2.0f);
    
    t.setEffect(is);

    t.setTranslateX(10);
    t.setTranslateY(150);

    HBox box = new HBox();
    box.getChildren().add(t);

    Scene scene = new Scene(box, 400, 450);
    stage.setScene(scene);
    stage.show();
  }
}

The code above generates the following result.

null

Reflection

Reflection effect renders a reflected version of the object below the actual object.

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.effect.Reflection;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.stage.Stage;
//w  w w . jav  a  2 s.  c  o m
public class Main extends Application {
    public static void main(String[] args) {
        Application.launch(args);
    }
    @Override
    public void start(Stage primaryStage) {
        primaryStage.setTitle("Text Fonts");
        Group root = new Group();
        Scene scene = new Scene(root, 550, 250, Color.WHITE);

        Text text = new Text(50, 50, "JavaFX 2.0 from Java2s.com");
        Font monoFont = Font.font("Dialog", 30);
        text.setFont(monoFont);
        text.setFill(Color.BLACK);
        root.getChildren().add(text);

        Reflection refl = new Reflection();
        refl.setFraction(0.8f);
        text.setEffect(refl);

        primaryStage.setScene(scene);
        primaryStage.show();
    }
}

The code above generates the following result.

null

Lighting Effect

The lighting effect creates a light source shining on the given content. It can give flat objects a more realistic three-dimensional appearance.

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.effect.Effect;
import javafx.scene.effect.Glow;
import javafx.scene.effect.Light;
import javafx.scene.effect.Lighting;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.stage.Stage;
/* ww w .ja  va  2 s  .  c  om*/
public class Main extends Application
{
   @Override
   public void start(final Stage stage) throws Exception
   {
      Group rootGroup = new Group();
      Scene scene =new Scene(rootGroup, 800, 400);
 
      Text text1 = new Text(25, 25, "java2s.com");
      text1.setFill(Color.CHOCOLATE);
      text1.setFont(Font.font(java.awt.Font.MONOSPACED, 35));
      
      final Light.Distant light = new Light.Distant();
      light.setAzimuth(-135.0);
      final Lighting lighting = new Lighting();
      lighting.setLight(light);
      lighting.setSurfaceScale(9.0);
      text1.setEffect(lighting);

      
      
      rootGroup.getChildren().add(text1);

      stage.setScene(scene);
      stage.show();
   }
   public static void main(final String[] arguments)
   {
      Application.launch(arguments);
   }
}

The code above generates the following result.

null

Glowing Text

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.effect.Effect;
import javafx.scene.effect.Glow;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.Text;
import javafx.stage.Stage;
/*ww  w. j  a va 2 s. c  o  m*/
public class Main extends Application
{
   @Override
   public void start(final Stage stage) throws Exception
   {
      Group rootGroup = new Group();
      Scene scene =new Scene(rootGroup, 800, 400);
 
      Text text1 = new Text(25, 25, "java2s.com");
      text1.setFill(Color.CHOCOLATE);
      text1.setFont(Font.font(java.awt.Font.MONOSPACED, 35));
      
      Effect glow = new Glow(1.0);
      text1.setEffect(glow);
      
      rootGroup.getChildren().add(text1);

      stage.setScene(scene);
      stage.show();
   }
   public static void main(final String[] arguments)
   {
      Application.launch(arguments);
   }
}

The code above generates the following result.

null

Perspective Effect

The perspective effect creates a three-dimensional effect out of two-dimensional object.

//ww w . j  a  va2  s  . c  o m
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.effect.PerspectiveTransform;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.stage.Stage;

public class Main extends Application {

  @Override
  public void start(Stage stage) {
    Group root = new Group();
    Scene scene = new Scene(root, 260, 80);
    stage.setScene(scene);

    
    Group g = new Group();
    PerspectiveTransform pt = new PerspectiveTransform();
    pt.setUlx(10.0);
    pt.setUly(10.0);
    pt.setUrx(310.0);
    pt.setUry(40.0);
    pt.setLrx(310.0);
    pt.setLry(60.0);
    pt.setLlx(10.0);
    pt.setLly(90.0);

    g.setEffect(pt);
    g.setCache(true);

    Rectangle r = new Rectangle();
    r.setX(10.0);
    r.setY(10.0);
    r.setWidth(280.0);
    r.setHeight(80.0);
    r.setFill(Color.BLUE);

    Text t = new Text();
    t.setX(20.0);
    t.setY(65.0);
    t.setText("JavaFX");
    t.setFill(Color.YELLOW);
    t.setFont(Font.font(null, FontWeight.BOLD, 36));

    g.getChildren().add(r);
    g.getChildren().add(t);
    
    scene.setRoot(g);
    stage.show();
  }

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

The code above generates the following result.

null

Creating a Chain of Effects

import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.effect.DropShadow;
import javafx.scene.effect.Reflection;
import javafx.scene.layout.HBox;
import javafx.scene.paint.Color;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.stage.Stage;
/*from w ww .  j a v  a2s  . co m*/
public class Main extends Application {
  public static void main(String[] args) throws Exception {
    launch(args);
  }
  @Override
  public void start(final Stage stage) throws Exception {
    Text t = new Text();
    t.setX(20.0f);
    t.setY(80.0f);
    t.setText("java2s.com");
    t.setFill(Color.RED);
    t.setFont(Font.font("Arial", FontWeight.BOLD, 60));
    
    DropShadow ds = new DropShadow();
    ds.setOffsetY(5.0);
    ds.setOffsetX(5.0);
    ds.setColor(Color.GRAY);
    
    Reflection reflection = new Reflection();

    ds.setInput(reflection);   
    
    t.setEffect(ds);

    t.setTranslateX(10);
    t.setTranslateY(150);

    HBox box = new HBox();
    box.getChildren().add(t);

    Scene scene = new Scene(box, 400, 450);
    stage.setScene(scene);
    stage.show();
  }
}

The code above generates the following result.

null