是否可以使用 CSS 或以编程方式在 JavaFX 中创建这样的背景,但没有图像文件是动态的。
这是我要创建的图像:
您可以自己在Canvas上绘制图像,然后使用snapshot方法将其转换为 Image:
Background createBackground() {
double width = 200;
double height = 200;
Canvas canvas = new Canvas(width, height);
GraphicsContext g = canvas.getGraphicsContext2D();
double[] x;
double[] y;
g.setFill(Color.RED);
// Left triangle
x = new double[] { 0, 0, width / 2 };
y = new double[] { 0, height, height / 2 };
g.fillPolygon(x, y, x.length);
// Right triangle
x = new double[] { width, width, width / 2 };
g.fillPolygon(x, y, x.length);
g.setFill(Color.DARKGREEN);
// Top triangle
x = new double[] { 0, width, width / 2 };
y = new double[] { 0, 0, height / 2 };
g.fillPolygon(x, y, x.length);
// Bottom triangle
y = new double[] { height, height, height / 2 };
g.fillPolygon(x, y, x.length);
Image image = canvas.snapshot(null, null);
return new Background(
new BackgroundImage(image, null, null, null, null));
}