Javafx 线性梯度重复行为
Javafx linear-gradient repeat behaviour
Javafx 线性渐变重复似乎反映颜色而不是重复。
我编写了一个简单的应用程序来展示在自定义节点(StackPane)上使用线性渐变和重复在我的应用程序中创建条纹图案时看到的内容。在我的应用程序中,它作为叠加层添加到 XYChart 中,并且它们的高度各不相同。使用 Rectangle 效果不佳,这就是为什么我使用 Stackpane 并在其上设置样式而不是以编程方式创建 LinearGradient 的原因。
颜色列表是动态的,在应用程序中大小不同。
问题是线性渐变翻转列表并在每次重复时反映颜色的方式,而不是仅仅重复。
这个 link 描述了一个类似的问题,但只是添加无穷无尽的停顿似乎是我问题的一个混乱的解决方案,最好添加一次颜色并重复。
linear gradient repeat on css for javafx
java.util.List;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage primaryStage) {
try {
BorderPane root = new BorderPane();
List<Color> colors = Arrays.asList( Color.RED,Color.BLUE,Color.YELLOW,Color.GREEN);
StackPane stackPane = new StackPane();
stackPane.setStyle(getLinearGradientStyle(colors));
root.setCenter(stackPane);
Scene scene = new Scene(root, 400, 400);
primaryStage.setScene(scene);
primaryStage.show();
}
catch (Exception e) {
e.printStackTrace();
}
}
private String getLinearGradientStyle(List<Color> colors) {
StringBuilder stringBuilder = new StringBuilder("-fx-background-color: linear-gradient(from 0px 0px to 10px 10px, repeat,");
for (int i = 0; i < colors.size(); i++) {
stringBuilder.append("rgb(")
.append((int) (colors.get(i).getRed() * 255)).append(",")
.append((int) (colors.get(i).getGreen() * 255)).append(",")
.append((int) (colors.get(i).getBlue() * 255))
.append(")")
.append(" ").append(getPercentage(i+1, colors.size()+1) );
if (i < colors.size() - 1) {
stringBuilder.append(",");
}
}
stringBuilder.append(");");
System.out.println("Main.getLinearGradientStyle():"+stringBuilder);
return stringBuilder.toString();
}
private String getPercentage(float i, int size) {
return (((1.0f / size) * 100 )*i)+ "%";
}
public static void main(String[] args) {
launch(args);
}
这是一个使用重复线性渐变的 CSS3 示例:
https://tympanus.net/codrops/css_reference/repeating-linear-gradient/
向下滚动到以下文本:将创建条纹背景,其中每个线性渐变都是三条纹渐变,无限重复(此示例)
我的示例使用了我需要的对角线图案,但上面的示例显示了我希望看到的纯色重复颜色,没有正常的反射 css。
感谢您的帮助
这看起来像是一个错误。如果您 运行 以下示例(将 CSS 移动到文件中):
Main.java
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.Region;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage primaryStage) {
Region region = new Region();
region.backgroundProperty().addListener((obs, ov, nv) ->
System.out.println(nv.getFills().get(0).getFill()));
Scene scene = new Scene(region, 500, 300);
scene.getStylesheets().add("Main.css");
primaryStage.setScene(scene);
primaryStage.show();
}
}
Main.css
.root {
-fx-background-color: linear-gradient(from 0px 0px to 10px 10px, repeat, red 20%, blue 40%, yellow 60%, green 80%);
}
您将看到以下打印出来的内容:
linear-gradient(from 0.0px 0.0px to 10.0px 10.0px, reflect, 0xff0000ff 0.0%, 0xff0000ff 20.0%, 0x0000ffff 40.0%, 0xffff00ff 60.0%, 0x008000ff 80.0%, 0x008000ff 100.0%)
如您所见,尽管在 CSS 中使用了 "repeat",但创建的 LinearGradient
使用了 "reflect".
您自己可能无法解决此错误,但如果您不介意在代码中设置背景(甚至可能是 FXML),那么以下应该可以满足您的要求:
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.Background;
import javafx.scene.layout.BackgroundFill;
import javafx.scene.layout.Region;
import javafx.scene.paint.Color;
import javafx.scene.paint.CycleMethod;
import javafx.scene.paint.LinearGradient;
import javafx.scene.paint.Stop;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage primaryStage) {
LinearGradient gradient = new LinearGradient(0, 0, 10, 10, false, CycleMethod.REPEAT,
new Stop(0.2, Color.RED),
new Stop(0.4, Color.BLUE),
new Stop(0.6, Color.YELLOW),
new Stop(0.8, Color.GREEN)
);
Region region = new Region();
region.setBackground(new Background(new BackgroundFill(gradient, null, null)));
Scene scene = new Scene(region, 500, 300);
primaryStage.setScene(scene);
primaryStage.show();
}
}
您可以将 LinearGradient
的创建移动到采用任意数量 Color
的方法中,就像您当前正在做的那样。
如果您有兴趣,我相信该错误位于 javafx.css.CssParser
行 1872
附近(在 JavaFX 12 中):
CycleMethod cycleMethod = CycleMethod.NO_CYCLE;
if ("reflect".equalsIgnoreCase(arg.token.getText())) {
cycleMethod = CycleMethod.REFLECT;
prev = arg;
arg = arg.nextArg;
} else if ("repeat".equalsIgnoreCase(arg.token.getText())) {
cycleMethod = CycleMethod.REFLECT;
prev = arg;
arg = arg.nextArg;
}
如您所见,当文本等于 "repeat
时,它错误地将 CycleMethod
设置为 REFLECT
。
错误报告已提交:JDK-8222222 (GitHub #437)。 修复版本:openjfx13.
Javafx 线性渐变重复似乎反映颜色而不是重复。
我编写了一个简单的应用程序来展示在自定义节点(StackPane)上使用线性渐变和重复在我的应用程序中创建条纹图案时看到的内容。在我的应用程序中,它作为叠加层添加到 XYChart 中,并且它们的高度各不相同。使用 Rectangle 效果不佳,这就是为什么我使用 Stackpane 并在其上设置样式而不是以编程方式创建 LinearGradient 的原因。 颜色列表是动态的,在应用程序中大小不同。 问题是线性渐变翻转列表并在每次重复时反映颜色的方式,而不是仅仅重复。
这个 link 描述了一个类似的问题,但只是添加无穷无尽的停顿似乎是我问题的一个混乱的解决方案,最好添加一次颜色并重复。
linear gradient repeat on css for javafx
java.util.List;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.StackPane;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage primaryStage) {
try {
BorderPane root = new BorderPane();
List<Color> colors = Arrays.asList( Color.RED,Color.BLUE,Color.YELLOW,Color.GREEN);
StackPane stackPane = new StackPane();
stackPane.setStyle(getLinearGradientStyle(colors));
root.setCenter(stackPane);
Scene scene = new Scene(root, 400, 400);
primaryStage.setScene(scene);
primaryStage.show();
}
catch (Exception e) {
e.printStackTrace();
}
}
private String getLinearGradientStyle(List<Color> colors) {
StringBuilder stringBuilder = new StringBuilder("-fx-background-color: linear-gradient(from 0px 0px to 10px 10px, repeat,");
for (int i = 0; i < colors.size(); i++) {
stringBuilder.append("rgb(")
.append((int) (colors.get(i).getRed() * 255)).append(",")
.append((int) (colors.get(i).getGreen() * 255)).append(",")
.append((int) (colors.get(i).getBlue() * 255))
.append(")")
.append(" ").append(getPercentage(i+1, colors.size()+1) );
if (i < colors.size() - 1) {
stringBuilder.append(",");
}
}
stringBuilder.append(");");
System.out.println("Main.getLinearGradientStyle():"+stringBuilder);
return stringBuilder.toString();
}
private String getPercentage(float i, int size) {
return (((1.0f / size) * 100 )*i)+ "%";
}
public static void main(String[] args) {
launch(args);
}
这是一个使用重复线性渐变的 CSS3 示例:
https://tympanus.net/codrops/css_reference/repeating-linear-gradient/
向下滚动到以下文本:将创建条纹背景,其中每个线性渐变都是三条纹渐变,无限重复(此示例)
我的示例使用了我需要的对角线图案,但上面的示例显示了我希望看到的纯色重复颜色,没有正常的反射 css。
感谢您的帮助
这看起来像是一个错误。如果您 运行 以下示例(将 CSS 移动到文件中):
Main.java
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.Region;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage primaryStage) {
Region region = new Region();
region.backgroundProperty().addListener((obs, ov, nv) ->
System.out.println(nv.getFills().get(0).getFill()));
Scene scene = new Scene(region, 500, 300);
scene.getStylesheets().add("Main.css");
primaryStage.setScene(scene);
primaryStage.show();
}
}
Main.css
.root {
-fx-background-color: linear-gradient(from 0px 0px to 10px 10px, repeat, red 20%, blue 40%, yellow 60%, green 80%);
}
您将看到以下打印出来的内容:
linear-gradient(from 0.0px 0.0px to 10.0px 10.0px, reflect, 0xff0000ff 0.0%, 0xff0000ff 20.0%, 0x0000ffff 40.0%, 0xffff00ff 60.0%, 0x008000ff 80.0%, 0x008000ff 100.0%)
如您所见,尽管在 CSS 中使用了 "repeat",但创建的 LinearGradient
使用了 "reflect".
您自己可能无法解决此错误,但如果您不介意在代码中设置背景(甚至可能是 FXML),那么以下应该可以满足您的要求:
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.layout.Background;
import javafx.scene.layout.BackgroundFill;
import javafx.scene.layout.Region;
import javafx.scene.paint.Color;
import javafx.scene.paint.CycleMethod;
import javafx.scene.paint.LinearGradient;
import javafx.scene.paint.Stop;
import javafx.stage.Stage;
public class Main extends Application {
@Override
public void start(Stage primaryStage) {
LinearGradient gradient = new LinearGradient(0, 0, 10, 10, false, CycleMethod.REPEAT,
new Stop(0.2, Color.RED),
new Stop(0.4, Color.BLUE),
new Stop(0.6, Color.YELLOW),
new Stop(0.8, Color.GREEN)
);
Region region = new Region();
region.setBackground(new Background(new BackgroundFill(gradient, null, null)));
Scene scene = new Scene(region, 500, 300);
primaryStage.setScene(scene);
primaryStage.show();
}
}
您可以将 LinearGradient
的创建移动到采用任意数量 Color
的方法中,就像您当前正在做的那样。
如果您有兴趣,我相信该错误位于 javafx.css.CssParser
行 1872
附近(在 JavaFX 12 中):
CycleMethod cycleMethod = CycleMethod.NO_CYCLE;
if ("reflect".equalsIgnoreCase(arg.token.getText())) {
cycleMethod = CycleMethod.REFLECT;
prev = arg;
arg = arg.nextArg;
} else if ("repeat".equalsIgnoreCase(arg.token.getText())) {
cycleMethod = CycleMethod.REFLECT;
prev = arg;
arg = arg.nextArg;
}
如您所见,当文本等于 "repeat
时,它错误地将 CycleMethod
设置为 REFLECT
。
错误报告已提交:JDK-8222222 (GitHub #437)。 修复版本:openjfx13.