Javafx 简明教程

JavaFX - InnerShadow Effect

虽然下拉阴影效果通过模糊其边缘创建了原始节点背后的副本,但内阴影效果将在节点的边缘内创建阴影。它们都类似于阴影效果。

While the drop shadow effect creates a duplicate no behind the original node by blurring its edges, the inner shadow effect will be create a shadow inside the edges of the node. They are both similar to shadow effect.

包 javafx.scene.effect 中名为 InnerShadow 的类表示内阴影效果。此类包含十个属性,它们是 −

The class named InnerShadow of the package javafx.scene.effect represents the inner shadow effect. This class contains ten properties, which are −

  1. color − This property is of Color type representing the color of the shadow.

  2. blur type − This property is of BlurType and it represents the type of blur effect used to blur the shadow.

  3. radius − This property is of the type double and it represents the radius of the shadow blur kernel.

  4. width − This property is of the type double and it represents the width of the shadow blur kernel.

  5. height − This property is of the type double and it represents the height of the shadow blur kernel.

  6. input − This property is of the type Effect and it represents an input to the shadow effect.

  7. spread − This property is of the type double; it represents the spread of the shadow.

  8. offsetX − This property is of the type double, it represents the shadow offset in the x direction, in pixels.

  9. offsetY − This property is of the type double, it represents the shadow offset in the y direction in pixels.

  10. choke − This property is of double type; it represents the choke of the shadow.

Example

以下程序是一个示例,演示 JavaFX 的内阴影效果。在这里,我们在场景中绘制文本“Welcome to Tutorialspoint”和一个圆。

The following program is an example demonstrating the inner shadow effect of JavaFX. In here, we are drawing a text “Welcome to Tutorialspoint”, and a circle in a scene.

对此,我们正在应用内阴影效果。在此名称为 InnerShadowEffectExample.java 的文件中保存此代码。

To these, we are applying the inner shadow effect. Save this code in a file with the name InnerShadowEffectExample.java.

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.effect.InnerShadow;
import javafx.scene.paint.Color;
import javafx.scene.shape.Circle;
import javafx.stage.Stage;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;

public class InnerShadowEffectExample extends Application {
   @Override
   public void start(Stage stage) {
      //Creating a Text object
      Text text = new Text();

      //Setting font to the text
      text.setFont(Font.font(null, FontWeight.BOLD, 40));

      //setting the position of the text
      text.setX(60);
      text.setY(50);

      //Setting the text to be embedded.
      text.setText("Welcome to Tutorialspoint");

      //Setting the color of the text
      text.setFill(Color.RED);

      //Drawing a Circle
      Circle circle = new Circle();

      //Setting the center of the circle
      circle.setCenterX(300.0f);
      circle.setCenterY(160.0f);

      //Setting the radius of the circle
      circle.setRadius(100.0f);

      //setting the fill color of the circle
      circle.setFill(Color.CORNFLOWERBLUE);

      //Instantiating the InnerShadow class
      InnerShadow innerShadow = new InnerShadow();

      //Setting the offset values of the inner shadow
      innerShadow.setOffsetX(4);
      innerShadow.setOffsetY(4);

      //Setting the color of the inner shadow
      innerShadow.setColor(Color.GRAY);

      //Applying inner shadow effect to the text
      text.setEffect(innerShadow);

      //Applying inner shadow effect to the circle
      circle.setEffect(innerShadow);

      //Creating a Group object
      Group root = new Group(text,circle);

      //Creating a scene object
      Scene scene = new Scene(root, 600, 300);

      //Setting title to the Stage
      stage.setTitle("Inner shadow effect example");

      //Adding scene to the stage
      stage.setScene(scene);

      //Displaying the contents of the stage
      stage.show();
   }
   public static void main(String args[]){
      launch(args);
   }
}

使用以下命令,从命令提示符编译并执行已保存的 java 文件。

Compile and execute the saved java file from the command prompt using the following commands.

javac --module-path %PATH_TO_FX% --add-modules javafx.controls InnerShadowEffectExample.java
java --module-path %PATH_TO_FX% --add-modules javafx.controls InnerShadowEffectExample

Output

执行后,上述程序会生成如下所示的 JavaFX 窗口。

On executing, the above program generates a JavaFX window as shown below.

inner shadow effect