LinearGradient gradient = new LinearGradient(
0, 0, W, 0,
false,
CycleMethod.NO_CYCLE,
new Stop(0, Color.LAWNGREEN),
new Stop(.5, Color.YELLOW),
new Stop(1, Color.ORANGERED)
);
return new Rectangle(W * visiblePortion, H, gradient);
使用剪辑
这里,我们将剪辑应用于一个具有渐变的矩形,因此仅部分矩形可见。
Rectangle rect = new Rectangle(W, H);
rect.setStyle("-fx-fill: linear-gradient(to right, lawngreen, yellow, orangered);");
Rectangle clip = new Rectangle(W * visiblePortion, H);
rect.setClip(clip);
LinearGradient gradient = new LinearGradient(
0, 0, 1, 0,
true,
CycleMethod.NO_CYCLE,
new Stop(0, Color.GREEN),
new Stop(1, Color.GREEN.interpolate(Color.RED, visiblePortion))
);
return new Rectangle(W * visiblePortion, H, gradient);
使用进度条
这些图片看起来很像进度条。如果合适的话,您可以研究如何样式化进度条。我不会在这里提供渐变样式进度条的解决方案。如果您需要这个,请提一个新问题。
示例应用程序
import javafx.application.Application;
import javafx.geometry.*;
import javafx.geometry.Insets;
import javafx.scene.*;
import javafx.scene.control.Label;
import javafx.scene.layout.VBox;
import javafx.scene.paint.*;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;
import java.util.function.Function;
public class GradientDemo extends Application {
private static final double W = 500;
private static final double H = 10;
private static final double STEP = 0.2;
@Override
public void start(Stage stage) {
final VBox layout = new VBox(10);
layout.setAlignment(Pos.TOP_LEFT);
layout.setPadding(new Insets(10));
addGradients(layout, "fixedWidthTriColorGradient", this::fixedWidthTriColorGradient);
addGradients(layout, "clippedTriColorGradient", this::clippedTriColorGradient);
addGradients(layout, "interpolatedTwoColorGradient", this::interpolatedTwoColorGradient);
stage.setScene(new Scene(layout));
stage.show();
}
private void addGradients(
VBox layout,
String gradientName,
Function<Double, Node> gradientFactory
) {
Label label = new Label(gradientName);
label.setPadding(new Insets(5, 0, 0, 0));
layout.getChildren().addAll(
label
);
for (double f = STEP; f <= 1.0; f += STEP) {
layout.getChildren().addAll(gradientFactory.apply(f));
}
}
public Rectangle fixedWidthTriColorGradient(double visiblePortion) {
LinearGradient gradient = new LinearGradient(
0, 0, W, 0,
false,
CycleMethod.NO_CYCLE,
new Stop(0, Color.LAWNGREEN),
new Stop(.5, Color.YELLOW),
new Stop(1, Color.ORANGERED)
);
return new Rectangle(W * visiblePortion, H, gradient);
}
public Rectangle clippedTriColorGradient(double visiblePortion) {
Rectangle rect = new Rectangle(W, H);
rect.setStyle("-fx-fill: linear-gradient(to right, lawngreen, yellow, orangered);");
Rectangle clip = new Rectangle(W * visiblePortion, H);
rect.setClip(clip);
return rect;
}
public Rectangle interpolatedTwoColorGradient(double visiblePortion) {
LinearGradient gradient = new LinearGradient(
0, 0, 1, 0,
true,
CycleMethod.NO_CYCLE,
new Stop(0, Color.GREEN),
new Stop(1, Color.GREEN.interpolate(Color.RED, visiblePortion))
);
return new Rectangle(W * visiblePortion, H, gradient);
}
public static void main(String[] args) {
launch(args);
}
}
LinearGradient
(编程或CSS)来完成这个任务。你可以尝试使用剪辑(查看Node.clip
属性)。如果将具有渐变的节点保持原始大小,然后将其剪切到一半的宽度,那么我认为你只会看到50%的渐变效果。但也许有更好的方法。这可能也是一个_XY问题_。如果你解释一下你试图做什么,也许就可以提出替代方案了。 - Slaw