如何在JavaFX中将线性渐变(颜色)应用于节点?
例
import javafx.application.Application; import javafx.scene.Group; import javafx.scene.Scene; import javafx.scene.paint.Color; import javafx.scene.paint.CycleMethod; import javafx.scene.paint.LinearGradient; import javafx.scene.paint.Stop; import javafx.stage.Stage; import javafx.scene.shape.Circle; import javafx.scene.shape.Ellipse; import javafx.scene.shape.Polygon; import javafx.scene.shape.Rectangle; public class LinearGradientExample extends Application { public void start(Stage stage) { //画一个圆 Circle circle = new Circle(75.0f, 65.0f, 40.0f ); //画一个矩形 Rectangle rect = new Rectangle(150, 30, 100, 65); //画一个椭圆 Ellipse ellipse = new Ellipse(330, 60, 60, 35); //绘制多边形 Polygon poly = new Polygon(410, 60, 430, 30, 470, 30, 490, 60, 470, 100, 430, 100 ); //设置线性渐变 Stop[] stops = new Stop[] { new Stop(0, Color.DARKSLATEBLUE), new Stop(1, Color.DARKRED) }; LinearGradient gradient = new LinearGradient(0, 0, 1, 0, true, CycleMethod.NO_CYCLE, stops); //设置图案 circle.setFill(gradient); circle.setStrokeWidth(3); circle.setStroke(Color.CADETBLUE); rect.setFill(gradient); rect.setStrokeWidth(3); rect.setStroke(Color.CADETBLUE); ellipse.setFill(gradient); ellipse.setStrokeWidth(3); ellipse.setStroke(Color.CADETBLUE); poly.setFill(gradient); poly.setStrokeWidth(3); poly.setStroke(Color.CADETBLUE); //设置舞台 Group root = new Group(circle, ellipse, rect, poly); Scene scene = new Scene(root, 600, 150); stage.setTitle("Linear Gradient"); stage.setScene(scene); stage.show(); } public static void main(String args[]){ launch(args); } }
输出结果