将文本设置为随机颜色和不透明度 javaFX
Set text to random color & opacity javaFX
我需要一个 javafx 程序来将文本设置为随机颜色和不透明度我不确定该怎么做?这是我的代码示例
Text text1 = new Text();
text1.setText("Java");
text1.setFont(Font.font("Times New Roman", FontWeight.BOLD, FontPosture.ITALIC, 22));
text1.setRotate(90);
gridpane.add(text1, 3, 1);
您可以使用Math.random()
在[0,1)
范围内生成一个Double
,所以您需要做:
text.
setOpacity
(Math.random());
Color 在文档中进行了更多挖掘,但可以通过以下方式完成:
text.
setFill
(
Color.color
(Math.random(), Math.random(), Math.random());
setFill
来自 Shape
,Text
继承自。 setFill
需要一个 Paint
,其中 Color
是最简单的实现。 Color.color(double, double, double)
取 [0,1].
范围内双倍的 rgb 值
了解如何浏览文档,以后您将能够自己快速找到此类内容!
注意:opacity/rgb 颜色都取范围 [0,1] 的两倍,其中 Math.random()
产生范围 [0,1)。如果您不熟悉这种表示法,这意味着 Math.random()
永远不会产生 1,只会产生一个可能准确度小于 1 的数字。这意味着您永远不会使用此方法获得 100% 的完全 opaque/r/g/b,但实际上您可能无法区分,因此最好使用不太复杂的方法。
注意2:javafx.scene.paint.Color#color
实际上提供了一个包含不透明度的四参数构造函数,但我建议像上面那样设置Text节点本身的不透明度,而不是Paint的不透明度。
像这样:
Text randomColorText(String txt) {
Text t = new Text(txt);
Random rng = new Random();
int c = rng.nextInt();
int r = c & 255;
int g = (c >>> 8) & 255;
int b = (c >>> 16) & 255;
double op = (c >>> 24) / 255.0;
t.setFill(Color.rgb(r, g, b, op));
// or use only r,g,b above and set opacity of the Text shape: t.setOpacity(op);
return t;
}
请注意,另一个提到 Random 永远不会 return a double == 1.0 的答案是错误的。颜色 RGB 值的范围与 double 不同——通常它们最终会在某个点以 0-255 范围内的 8 位值结束,在某些高端应用程序中,您可能每个通道使用 16 位。您将使用 Random 的双打获得全系列的颜色。
您会注意到,对于通常由 32 位值表示的值,我避免多次调用随机数生成器。 (微优化:调用 nextInt 完成了 nextDouble 的一半工作,我们只需要调用一次。我通常会将 Random 的实例保存为静态变量,而不是每次调用该方法时都创建一个。java.util.Random 是 threeadsafe。)
我需要一个 javafx 程序来将文本设置为随机颜色和不透明度我不确定该怎么做?这是我的代码示例
Text text1 = new Text();
text1.setText("Java");
text1.setFont(Font.font("Times New Roman", FontWeight.BOLD, FontPosture.ITALIC, 22));
text1.setRotate(90);
gridpane.add(text1, 3, 1);
您可以使用Math.random()
在[0,1)
范围内生成一个Double
,所以您需要做:
text.
setOpacity
(Math.random());
Color 在文档中进行了更多挖掘,但可以通过以下方式完成:
text.
setFill
(
Color.color
(Math.random(), Math.random(), Math.random());
setFill
来自 Shape
,Text
继承自。 setFill
需要一个 Paint
,其中 Color
是最简单的实现。 Color.color(double, double, double)
取 [0,1].
了解如何浏览文档,以后您将能够自己快速找到此类内容!
注意:opacity/rgb 颜色都取范围 [0,1] 的两倍,其中 Math.random()
产生范围 [0,1)。如果您不熟悉这种表示法,这意味着 Math.random()
永远不会产生 1,只会产生一个可能准确度小于 1 的数字。这意味着您永远不会使用此方法获得 100% 的完全 opaque/r/g/b,但实际上您可能无法区分,因此最好使用不太复杂的方法。
注意2:javafx.scene.paint.Color#color
实际上提供了一个包含不透明度的四参数构造函数,但我建议像上面那样设置Text节点本身的不透明度,而不是Paint的不透明度。
像这样:
Text randomColorText(String txt) {
Text t = new Text(txt);
Random rng = new Random();
int c = rng.nextInt();
int r = c & 255;
int g = (c >>> 8) & 255;
int b = (c >>> 16) & 255;
double op = (c >>> 24) / 255.0;
t.setFill(Color.rgb(r, g, b, op));
// or use only r,g,b above and set opacity of the Text shape: t.setOpacity(op);
return t;
}
请注意,另一个提到 Random 永远不会 return a double == 1.0 的答案是错误的。颜色 RGB 值的范围与 double 不同——通常它们最终会在某个点以 0-255 范围内的 8 位值结束,在某些高端应用程序中,您可能每个通道使用 16 位。您将使用 Random 的双打获得全系列的颜色。
您会注意到,对于通常由 32 位值表示的值,我避免多次调用随机数生成器。 (微优化:调用 nextInt 完成了 nextDouble 的一半工作,我们只需要调用一次。我通常会将 Random 的实例保存为静态变量,而不是每次调用该方法时都创建一个。java.util.Random 是 threeadsafe。)