JavaFX Rectangle类

一般来说,矩形可以定义为由四个边组成的几何图形,其中对边总是相等的,并且相邻的两个边之间的夹角为90度。有四个相等边的矩形称为正方形。

JavaFX 库允许开发人员通过实例化javafx.scene.shape.Rectangle类来创建矩形。

1 Rectangle类的属性 

Rectangle 类包含下面描述的各种属性。

属性 描述 setter方法
ArcHeight 矩形四个角圆弧的垂直直径 setArcHeight(Double height)
ArcWidth 矩形四个角处圆弧的水平直径 setArcWidth(Double Width)
Height 定义矩形的高度 setHeight(Double height)
Width 定义矩形的宽度 setWidth(Double width)
X 左上角的 X 坐标 setX(Double X-value)
Y 左上角的 Y 坐标 setY(Double( Y-value)

2 Rectangle的示例1

package com.yiidian;

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;

public class Shape_Example extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception {
        // TODO Auto-generated method stub
        primaryStage.setTitle("一点教程网:Rectangle案例1");
        Group group = new Group(); //creating Group
        Rectangle rect=new Rectangle(); //instantiating Rectangle
        rect.setX(20); //setting the X coordinate of upper left //corner of rectangle
        rect.setY(20); //setting the Y coordinate of upper left //corner of rectangle
        rect.setWidth(100); //setting the width of rectangle
        rect.setHeight(100); // setting the height of rectangle
        group.getChildren().addAll(rect); //adding rectangle to the //group
        Scene scene = new Scene(group,200,300, Color.GRAY);
        primaryStage.setScene(scene);
        primaryStage.show();
    }
    public static void main(String[] args) {
        launch(args);
    }

}

输出结果为:

3 Rectangle的示例2

package com.yiidian;

import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.scene.shape.Rectangle;
import javafx.stage.Stage;

public class Shape_Example extends Application {

    @Override
    public void start(Stage primaryStage) throws Exception {
        primaryStage.setTitle("一点教程网:Rectangle案例2");
        Group group = new Group();
        Rectangle rect=new Rectangle();
        rect.setX(20);
        rect.setY(20);
        rect.setWidth(100);
        rect.setHeight(100);
        rect.setArcHeight(35);
        rect.setArcWidth(35);
        rect.setFill(Color.RED);
        group.getChildren().addAll(rect);
        Scene scene = new Scene(group,200,300,Color.GRAY);
        primaryStage.setScene(scene);
        primaryStage.show();
    }
    public static void main(String[] args) {
        launch(args);
    }

}

输出结果为:

热门文章

优秀文章