解释 JavaFX 中 2D 形状的 Stroke Width 属性
javaobject oriented programmingprogramming更新于 2025/10/11 23:40:17
Stroke width 属性指定形状边界线的宽度。您可以使用 javafx.scene.shape.Shape 类的 setWidth() 方法设置宽度。此方法接受双精度值作为参数并绘制指定宽度的边界。
如果您没有将任何值作为参数传递给此方法,则默认情况下它将宽度视为 1.0。
示例
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.scene.shape.Polygon;
import javafx.scene.text.Font;
import javafx.scene.text.FontPosture;
import javafx.scene.text.FontWeight;
import javafx.scene.text.Text;
import javafx.stage.Stage;
public class StrokeWidthExample extends Application {
public void start(Stage stage) {
Font font = Font.font("verdana", FontWeight.BOLD, FontPosture.REGULAR, 12);
Text label1 = new Text("Original Image");
label1.setFont(font);
label1.setX(250.0);
label1.setY(125.0);
Polygon rhombus1 = new Polygon(300.0, 0.0, 250.0, 50.0, 300.0, 100.0, 350.0, 50.0);
Text label2 = new Text("Stroke Width: none");
label2.setFont(font);
label2.setX(25.0);
label2.setY(275.0);
Polygon rhombus2 = new Polygon(100.0, 150.0, 50.0, 200.0, 100.0, 250.0, 150.0, 200.0);
rhombus2.setStroke(Color.BLUE);
Text label3 = new Text("Stroke Width: 3");
label3.setFont(font);
label3.setX(250.0);
label3.setY(275.0);
Polygon rhombus3 = new Polygon(300.0, 150.0, 250.0, 200.0, 300.0, 250.0, 350.0, 200.0);
rhombus3.setStroke(Color.BLUE);
rhombus3.setStrokeWidth(3.0);
Text label4 = new Text("Stroke Width: 10");
label4.setFont(font);
label4.setX(430.0);
label4.setY(275.0);
Polygon rhombus4 = new Polygon(490.0, 150.0, 440, 200.0, 490.0, 250.0, 540.0, 200.0);
rhombus4.setStroke(Color.BLUE);
rhombus4.setStrokeWidth(10.0);
//创建一个 Group 对象
Group root = new Group(label1, label2, label3, label4, rhombus1, rhombus2, rhombus3, rhombus4);
//创建一个 scene 对象
Scene scene = new Scene(root, 595, 310);
//为 Stage 设置标题
stage.setTitle("Stroke Width");
//将 scene 添加到 stage
stage.setScene(scene);
//显示舞台内容
stage.show();
}
public static void main(String args[]){
launch(args);
}
}
输出

相关文章
有用资源
java 参考教程 - 该教程包含有关 java 的更多信息:https://www.cainiaomax.com/java/

