多读书多实践,勤思考善领悟

JavaFX effect Glow(发光效果)

本文于1665天之前发表,文中内容可能已经过时。

就像Bloom Effect一样,Glow Effect也会使给定的输入图像发光。此效果使输入的像素更亮。

javafx.scene.effect的名为Glow的类表示发光效果。该类包含两个属性,即 -

  • input - 此属性的类型为Effect,它表示发光效果的输入。
  • level - 此属性的类型为double; 它代表了发光的强度。级别值的范围是0.0到1.0。

以下程序是演示JavaFX的Glow效果的示例。在这里,我们使用ImageImageView类在JavaFX场景中嵌入以下图像(Tutorialspoint Logo)。这将在位置100,70处完成,并且分别具有配合高度和配合宽度200和400。

对于此图像,我们应用了Glow Effect,其级别值为0.9。将此代码保存在名为GlowEffectExample.java的文件中。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import javafx.application.Application; 
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.effect.Glow;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.stage.Stage;

public class GlowEffectExample extends Application {
@Override
public void start(Stage stage) {
//Creating an image
Image image = new Image("https://www.ljjyy.com/img/logo.png");

//Setting the image view
ImageView imageView = new ImageView(image);

//setting the fit width of the image view
imageView.setFitWidth(200);

//Setting the preserve ratio of the image view
imageView.setPreserveRatio(true);

//Instantiating the Glow class
Glow glow = new Glow();

//setting level of the glow effect
glow.setLevel(0.9);

//Applying bloom effect to text
imageView.setEffect(glow);

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

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

//Setting title to the Stage
stage.setTitle("Sample Application");

//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文件。

1
2
javac GlowEffectExample.java 
java GlowEffectExample

执行时,上面的程序生成一个JavaFX窗口,如下所示。