📜  JavaFX |影子班

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

JavaFX |影子班

Shadow 类是 JavaFX 的一部分。阴影类创建边缘模糊的单色阴影。阴影为黑色(默认情况下),可以与原件组合以创建阴影。不同颜色的阴影可以与原始添加以创建发光效果。 Shadow 类继承 Effect 类。

类的构造函数:

  1. Shadow() :创建一个新的 Shadow 对象。
  2. Shadow(BlurType t, Color c, double r) :创建具有指定模糊类型、颜色和半径的新阴影对象。
  3. Shadow(double r, Color c) :创建一个具有半径和颜色的新阴影对象。

常用方法:

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 Shadow effect.
setBlurType(BlurType v)Sets the blur type of the Shadow effect.
setColor(Color v)Sets the Color of the Shadow effect.
setInput(Effect v)Sets the value of the property input.
setRadius(double v)Sets the Radius of the shadow effect.

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

  1. 创建圆形并为其添加阴影效果的Java程序:在此程序中,我们将创建一个名为圆形的圆形,并创建一个具有指定半径和颜色的阴影效果阴影。使用setEffect()函数将阴影效果添加到圆中,并将圆添加到组中。圆圈将使用setTranslateX()setTranslateY()函数转换到舞台中的特定位置。组将添加到场景中,场景将添加到舞台中。
    // Java program to create a Circle 
    // and add Shadow 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 shadow_1 extends Application {
      
        // launch the application
        public void start(Stage stage) throws Exception
        {
      
            // set title for the stage
            stage.setTitle("shadow example");
      
            // create a circle
            Circle circle = new Circle(50.0f, 50.0f, 25.0f);
      
            // translate to a position
            circle.setTranslateX(50.0f);
            circle.setTranslateY(50.0f);
      
            // create a shadow effect
            Shadow shadow = new Shadow(10, Color.RED);
      
            // set effect
            circle.setEffect(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程序创建四个圆并为其添加阴影效果(不同的 BlurType):在这个程序中,我们将创建一个名为circlecircle1circle2circle3的圆,并创建一个名为shadow1shadow2shadow3shadow4的具有指定半径的阴影效果、 颜色和模糊类型。使用 setEffect()函数将阴影效果添加到圆形,并将圆形添加到组中。使用setTranslateX()setTranslateY()函数将圆形平移到舞台中的特定位置。组将添加到场景中,场景将添加到舞台中。
    // Java program to create four Circles and add Shadow
    // effect to it which are of different BlurType
    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 shadow_2 extends Application {
      
        // launch the application
        public void start(Stage stage) throws Exception
        {
      
            // set title for the stage
            stage.setTitle("shadow example");
      
            // create a circle
            Circle circle = new Circle(0.0f, 0.0f, 25.0f);
            Circle circle1 = new Circle(0.0f, 0.0f, 25.0f);
            Circle circle2 = new Circle(0.0f, 0.0f, 25.0f);
            Circle circle3 = new Circle(0.0f, 0.0f, 25.0f);
      
            // 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 shadow effect
            Shadow shadow1 = new Shadow(BlurType.values()[0], Color.BLUE, 10);
            Shadow shadow2 = new Shadow(BlurType.values()[1], Color.BLUE, 10);
            Shadow shadow3 = new Shadow(BlurType.values()[2], Color.BLUE, 10);
            Shadow shadow4 = new Shadow(BlurType.values()[3], Color.BLUE, 10);
      
            // set effect
            circle.setEffect(shadow1);
            circle1.setEffect(shadow2);
            circle2.setEffect(shadow3);
            circle3.setEffect(shadow4);
      
            // create a Group
            Group group = new Group(circle, circle1, circle2, circle3);
      
            // 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);
        }
    }
    

    输出:

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

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