📜  JavaFX | DropShadow 类

📅  最后修改于: 2022-05-13 01:55:27.463000             🧑  作者: Mango

JavaFX | DropShadow 类

DropShadow 类是 JavaFX 的一部分。 DropShadow 类创建一个高级效果,它使用指定的半径、散布、模糊类型和偏移来渲染内容后面的对象的阴影。 DropShadow 类继承Effect类。

类的构造函数:

  1. DropShadow() : 创建一个新的 DropShadow 对象。
  2. DropShadow(BlurType blurType, Color color, double radius, double spread, double offsetX, double offsetY) :使用指定值 blurType、radius、spread、offsetX、offsetY 创建 DropShadow 的新对象。
  3. DropShadow(double radius, Color color) :创建具有指定半径和颜色值的 DropShadow 的新对象。
  4. DropShadow(double radius, double offsetX, double offsetY, Color color) :使用指定的半径、offsetX、offsetY 和颜色创建一个新的 DropShadow 对象。

常用方法:

MethodExplanation
getBlurType()Returns the blur type of the effect.
getColor()Returns the color of the effect.
getInput()Returns the value of property input.
getRadius()Returns the radius of the dropShadow effect.
setBlurType(BlurType v)Sets the blur type of the dropShadow effect.
setColor(Color v)Sets the Color of the dropShadow effect.
setInput(Effect v)Sets the value of the property input.
setRadius(double v)Sets the Radius of the dropshadow effect.
setOffsetX(double v)Sets the value of offsetX.
setOffsetY(double v)Sets the value of offsetY.
getOffsetX()Returns the value of offsetX.
getOffsetY()Returns the value of offsetY.
getSpread()Returns the value of spread of drop Shadow effect.
setSpread(double v)Sets the value of spread of drop Shadow effect.

下面的程序说明了 DropShadow 类的使用:

  1. 创建圆形并为其添加 DropShadow 效果的Java程序:在此程序中,我们将创建一个名为circle的圆形,并创建一个具有指定半径和颜色的 DropShadow 效果drop_shadow 。 DropShadow 效果将使用setEffect()函数添加到圆圈中,并且圆圈将添加到组中。圆圈将使用setTranslateX()setTranslateY()函数转换到舞台中的特定位置。组将添加到场景中,场景将添加到舞台中。
    // Java program to create a Circle and
    // add DropShadow effect to it
    import javafx.application.Application;
    import javafx.scene.Scene;
    import javafx.scene.control.*;
    import javafx.scene.layout.*;
    import javafx.stage.Stage;
    import javafx.scene.image.*;
    import javafx.scene.effect.*;
    import java.io.*;
    import javafx.scene.shape.Circle;
    import javafx.scene.paint.Color;
    import javafx.scene.Group;
      
    public class drop_shadow_1 extends Application {
      
        // launch the application
        public void start(Stage stage) throws Exception
        {
      
            // set title for the stage
            stage.setTitle("drop_shadow example");
      
            // create a circle
            Circle circle = new Circle(50.0f, 50.0f, 25.0f);
      
            // set fill for circle
            circle.setFill(Color.BLUE);
      
            // translate to a position
            circle.setTranslateX(50.0f);
            circle.setTranslateY(50.0f);
      
            // create a drop_shadow effect
            DropShadow drop_shadow = new DropShadow(10, Color.RED);
      
            // set effect
            circle.setEffect(drop_shadow);
      
            // create a Group
            Group group = new Group(circle);
      
            // create a scene
            Scene scene = new Scene(group, 200, 200);
      
            // set the scene
            stage.setScene(scene);
      
            stage.show();
        }
      
        // Main Method
        public static void main(String args[])
        {
      
            // launch the application
            launch(args);
        }
    }
    

    输出:

  2. Java程序创建四个圆形并为其添加 DropShadow 效果,它们具有不同的模糊类型和不同的 offsetX、offsetY 和半径值:在此程序中,我们将创建名为circlecircle1circle2circle3的圆形并创建一个名为 DropShadow 效果drop_shadow1 , drop_shadow2 , drop_shadow3 , drop_shadow4具有指定的半径、颜色、offsetX、offsetY、传播和模糊类型。 DropShadow 效果将使用setEffect()函数添加到圆圈中,并且圆圈将添加到组中。圆圈将使用setTranslateX()setTranslateY()函数转换到舞台中的特定位置。组将添加到场景中,场景将添加到舞台中。
    // Java program to create four Circles and 
    // add DropShadow effect to it which are of
    // different blur types and different values 
    // of offsetX, offsetY and radius
    import javafx.application.Application;
    import javafx.scene.Scene;
    import javafx.scene.control.*;
    import javafx.scene.layout.*;
    import javafx.stage.Stage;
    import javafx.scene.image.*;
    import javafx.scene.effect.*;
    import java.io.*;
    import javafx.scene.shape.Circle;
    import javafx.scene.paint.Color;
    import javafx.scene.Group;
      
    public class drop_shadow_2 extends Application {
      
        // launch the application
        public void start(Stage stage) throws Exception
        {
      
            // set title for the stage
            stage.setTitle("drop_shadow example");
      
            // create a circle
            Circle circle = new Circle(0.0f, 0.0f, 25.0f, Color.RED);
            Circle circle1 = new Circle(0.0f, 0.0f, 25.0f, Color.RED);
            Circle circle2 = new Circle(0.0f, 0.0f, 25.0f, Color.RED);
            Circle circle3 = new Circle(0.0f, 0.0f, 25.0f, Color.RED);
      
            // translate to a position
            circle.setTranslateX(50.0f);
            circle.setTranslateY(50.0f);
      
            // translate to a position
            circle1.setTranslateX(150.0f);
            circle1.setTranslateY(50.0f);
      
            // translate to a position
            circle2.setTranslateX(50.0f);
            circle2.setTranslateY(150.0f);
      
            // translate to a position
            circle3.setTranslateX(150.0f);
            circle3.setTranslateY(150.0f);
      
            // create drop_shadow effect
            DropShadow drop_shadow1 = new DropShadow(BlurType.values()[0], 
                                         Color.BLUE, 5, 3.0f, 2.0f, 2.0f);
      
            DropShadow drop_shadow2 = new DropShadow(BlurType.values()[1], 
                                         Color.BLUE, 5, 3.0f, 3.0f, 3.0f);
      
            DropShadow drop_shadow3 = new DropShadow(BlurType.values()[2], 
                                         Color.BLUE, 5, 4.0f, 3.0f, 3.0f);
      
            DropShadow drop_shadow4 = new DropShadow(BlurType.values()[3], 
                                         Color.BLUE, 5, 4.0f, 2.0f, 2.0f);
      
            // set effect
            circle.setEffect(drop_shadow1);
            circle1.setEffect(drop_shadow2);
            circle2.setEffect(drop_shadow3);
            circle3.setEffect(drop_shadow4);
      
            // create a Group
            Group group = new Group(circle, circle1, circle2, circle3);
      
            // create a scene
            Scene scene = new Scene(group, 400, 400);
      
            // set the scene
            stage.setScene(scene);
      
            stage.show();
        }
      
        // Main Method
        public static void main(String args[])
        {
      
            // launch the application
            launch(args);
        }
    }
    

    输出:

注意:上述程序可能无法在在线 IDE 中运行。请使用离线编译器。

参考: https://docs.oracle.com/javase/8/javafx/api/javafx/scene/effect/DropShadow.html