java shape 形_java基础:10.4 Java FX之形状

JavaFX 提供了多种形状类,用于绘制文本、直线、圆、矩形、椭圆、孤、多边形以及折线。

Shape 类是一个抽象基类,定义了所有形状的共同属性。这些属性有fill、stroke,strokeWidth。

fill 属性指定一个填充形状内部区域的颜色。

Stroke 属性指定用于绘制形状边缘的颜色。

strokeWidth 属性指定形状边缘的宽度。

Shape的子类:

9d03ac7d356f55fbcf627dc6bb9e870e.png

1、Text

07adf2134201006d7d9c904579b39966.png

Pane pane = new Pane();

Text text1 = new Text(20,20,"Java is interesting");

text1.setFont(Font.font("Courier",FontWeight.BOLD,FontPosture.ITALIC,15));

text1.setFill(Color.RED);

text1.setUnderline(true); // underline

text1.setStrikethrough(true); // strikethrough

pane.getChildren().add(text1);

Scene scene1 = new Scene(pane, 500, 500); // create a scene

primaryStage.setTitle("ShowBorderdPane");

primaryStage.setScene(scene1); // place the scene in the stage

primaryStage.show();

2、Line

1条直线通过4 个参数(startX、startY、endX 以及endY) 连接两个点.

0c241e37d57d520745d27cffbd690a93.png

//line

Line line1 = new Line(70,70,120,120);

Line line2 = new Line(70,120,120,70);

// line1.endXProperty().bind(pane.widthProperty().divide(2));

// line1.endYProperty().bind(pane.heightProperty().divide(2));

line1.setStrokeWidth(5);

line2.setStrokeWidth(5);

line1.setStroke(Color.MEDIUMAQUAMARINE);

line2.setStroke(Color.MEDIUMAQUAMARINE);

pane.getChildren().addAll(line1,line2);

3、Rectangle

— 个矩形通过参数x、y、width、height、arcWidth 以及arcHeight 定义。矩形的左上角点处于(x,y), 参数aw(arcWidth) 表示圆角处弧的水平直径,ah(arcHeight)表示圆角处弧的垂直直径。

8d83b299740ee4b91e504a4e062ac2af.png

Rectangle r1 = new Rectangle(10,130,40,50);

r1.setStroke(Color.TAN);

r1.setFill(Color.WHITE); // r1.setFill(null);

pane.getChildren().add(r1);

4、Circle   Ellipse

Circle circle = new Circle(90,160,15);

Ellipse ellipse = new Ellipse(160,160,20,10);

还有一些子类,例如Arc 一段弧,Polygon 多边形,Polyline 不会自动闭合的多点连线。

总程序如下:

public class MyJavaFX extends Application {

@Override // Override the start method in the Application class

public void start(Stage primaryStage) { //set a primary stage

Pane pane = new Pane();

// text

Text text1 = new Text(50,50,"Java is interesting");

text1.setFont(Font.font("Courier",FontWeight.BOLD,FontPosture.ITALIC,15));

text1.setFill(Color.RED);

text1.setUnderline(true); // underline

text1.setStrikethrough(true); // strikethrough

//line

Line line1 = new Line(70,70,120,120);

Line line2 = new Line(70,120,120,70);

// line1.endXProperty().bind(pane.widthProperty().divide(2));

// line1.endYProperty().bind(pane.heightProperty().divide(2));

line1.setStrokeWidth(5);

line2.setStrokeWidth(5);

line1.setStroke(Color.MEDIUMAQUAMARINE);

line2.setStroke(Color.MEDIUMAQUAMARINE);

// rectangle

Rectangle r1 = new Rectangle(10,130,40,50);

r1.setStroke(Color.TAN);

r1.setFill(Color.WHITE); // r1.setFill(null);

// circle

Circle circle = new Circle(90,160,15);

Ellipse ellipse = new Ellipse(160,160,20,10);

pane.getChildren().addAll(text1,line1,line2,r1,circle,ellipse);

Scene scene1 = new Scene(pane, 500, 500); // create a scene

primaryStage.setTitle("ShowBorderdPane");

primaryStage.setScene(scene1); // place the scene in the stage

primaryStage.show();

}

}

84af8080e90ad6b1b7d939b6fe5a2eed.png

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值