个性化阅读
专注于IT技术分析

JavaFX矩形

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

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

物产

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

范例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);
}

}

输出:

JavaFX矩形输出

圆角矩形

我们可以通过调用实例设置方法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);
}

}

输出:

JavaFX矩形圆角输出
赞(0)
未经允许不得转载:srcmini » JavaFX矩形

相关推荐

评论 抢沙发

评论前必须登录!