Javafx 简明教程

JavaFX - DisplacementMap

正如你可能已经知道的那样,图像由无限数量的像素组成。位移映射效果通过一定的距离平移输入图像的像素,并产生具有这些像素的不同位置的输出图像。

As you might already know, an image is composed of infinite number of pixels. The Displacement Map Effect shifts the pixels of an input image by a certain distance and produces an output image with different locations of these pixels.

要在 JavaFX 节点上应用此效果,你需要使用 DisplacementMap 类。要平移输入图像上的像素,距离由 FloatMap 的前两个频段指定。FloatMap 是包含浮点数据的缓冲区,在这种情况下,即距离。

To apply this effect on a JavaFX node, you need to use the DisplacementMap class. To displace the pixels on input image, the distance is specified by the first two bands of a FloatMap. A FloatMap is a buffer that contains floating point data, which in this case, is the distance.

此类具有以下属性 -

This class has the following properties −

  1. input − specifies the input data, i.e. the image.

  2. mapData − specifies the map data for this displacement map effect. This specifies how to map pixels.

  3. offsetX − specifies the offset by which all x coordinate offset values in the FloatMap are displaced after they are scaled

  4. offsetY − specifies the offset by which all y coordinate offset values in the FloatMap are displaced after they are scaled

  5. scaleX − specifies the scale factor by which all x coordinate offset values in the FloatMap are multiplied

  6. scaleY − specifies the scale factor by which all y coordinate offset values in the FloatMap are multiplied

  7. wrap − Defines whether values taken from outside the edges of the map "wrap around" or not.

Example

在此示例中,我们尝试使用 DisplacementMap 类将位移贴图效果应用于文本。将此代码保存在文件名为 DisplacementMapExample.java 的文件中。

In this example, we are trying to apply the Displacement Map effect on a text using the DisplacementMap class. Save this code in a file with the name DisplacementMapExample.java.

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.effect.Effect;
import javafx.scene.effect.DisplacementMap;
import javafx.scene.effect.FloatMap;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import javafx.scene.text.Font;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;

public class DisplacementMapExample extends Application {
   @Override
   public void start(Stage stage) {
      int width = 220;
      int height = 100;

      FloatMap floatMap = new FloatMap();
      floatMap.setWidth(width);
      floatMap.setHeight(height);

      for (int i = 0; i < width; i++) {
         double v = (Math.sin(i / 20.0 * Math.PI) - 0.5) / 40.0;
         for (int j = 0; j < height; j++) {
            floatMap.setSamples(i, j, 0.0f, (float) v);
         }
      }

      DisplacementMap displacementMap = new DisplacementMap();
      displacementMap.setMapData(floatMap);

      Text text = new Text();
      text.setX(40.0);
      text.setY(80.0);
      text.setText("Displacement Map");
      text.setFill(Color.web("0x3b596d"));
      text.setFont(Font.font(null, FontWeight.BOLD, 50));
      text.setEffect(displacementMap);

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

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

      //Setting title to the Stage
      stage.setTitle("DisplacementMap Effect");

      //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 DisplacementMapExample.java
java --module-path %PATH_TO_FX% --add-modules javafx.controls DisplacementMapExample

Output

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

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

displacementmap effect