📜  JavaFX 矩形

📅  最后修改于: 2020-10-14 01:11:47             🧑  作者: Mango

JavaFX矩形

通常,可以将矩形定义为几何图形,该几何图形由四个侧面组成,其中四个相对侧面始终相等,并且两个相邻侧面之间的角度为90度。具有四个相等边的矩形称为正方形。

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

物产

Property Description Setter Method
ArcHeight Vertical diameter of the arc at the four corners of rectangle setArcHeight(Double height)
ArcWidth Horizontal diameter of the arc at the four corners of the rectangle setArcWidth(Double Width)
Height Defines the height of the rectangle setHeight(Double height)
Width Defines the width of the rectangle setWidth(Double width)
X<

范例1:

package application;
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 Example");
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);
}

}

输出:

圆角矩形

我们可以通过调用实例设置方法setArcHeight()和setArcWidth()来使矩形的角变圆。它设置矩形角的圆弧的高度和宽度。下面的示例实现圆角矩形。

例:

package application;
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 Example");
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);
}

}

输出:


/td>

X coordinate of the upper left corner setX(Double X-value)
Y Y coordinate of the upper left corner setY(Double( Y-value)