สี่เหลี่ยมผืนผ้าคือรูปหลายเหลี่ยมปิดที่มีขอบสี่ด้าน มุมระหว่างสองขอบใดๆ จะเป็นมุมฉาก และด้านตรงข้ามจะขนานกัน กำหนดโดยความสูงและความกว้าง ความยาวของด้านแนวตั้งและแนวนอนตามลำดับ
ใน JavaFX สี่เหลี่ยมผืนผ้าจะแสดงโดย javafx.scene.shape.Rectangle ระดับ. คลาสนี้มีคุณสมบัติสี่ประการคือ -
-
ส่วนสูง − คุณสมบัตินี้แสดงถึงพิกัด x ของจุดศูนย์กลางของวงกลม คุณสามารถตั้งค่าเป็นคุณสมบัตินี้โดยใช้ setHeight() วิธีการ
-
ความกว้าง − คุณสมบัตินี้แสดงถึงพิกัด y ของจุดศูนย์กลางของวงกลม คุณสามารถตั้งค่าเป็นคุณสมบัตินี้โดยใช้ setWidth() วิธีการ
-
x − รัศมีของวงกลมเป็นพิกเซล คุณสามารถตั้งค่าคุณสมบัตินี้โดยใช้ setRadius() วิธีการ
-
ย − รัศมีของวงกลมเป็นพิกเซล คุณสามารถตั้งค่าคุณสมบัตินี้โดยใช้ setRadius() วิธีการ
ในการสร้างสี่เหลี่ยมผืนผ้า คุณต้อง -
-
ยกตัวอย่างคลาส Rectangle
-
ตั้งค่าคุณสมบัติที่จำเป็นโดยใช้เมธอด setter หรือข้ามไปเป็นอาร์กิวเมนต์ของ Constructor
-
เพิ่มโหนดที่สร้าง (รูปร่าง) ให้กับวัตถุกลุ่ม
ตัวอย่าง
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import javafx.scene.shape.Rectangle;
public class DrawinRectangle extends Application {
public void start(Stage stage) {
//Drawing a Rectangle
Rectangle shape = new Rectangle();
//Setting the properties of the rectangle
shape.setX(150.0f);
shape.setY(75.0f);
shape.setWidth(300.0f);
shape.setHeight(150.0f);
//Setting other properties
shape.setFill(Color.DARKCYAN);
shape.setStrokeWidth(8.0);
shape.setStroke(Color.DARKSLATEGREY);
//Setting the Scene
Group root = new Group(shape);
Scene scene = new Scene(root, 595, 300, Color.BEIGE);
stage.setTitle("Drawing Rectangle");
stage.setScene(scene);
stage.show();
}
public static void main(String args[]){
launch(args);
}
} ผลลัพธ์

สี่เหลี่ยมผืนผ้าโค้งมน
นอกจากคุณสมบัติที่กล่าวมาแล้ว คลาส Rectangle ยังมีคุณสมบัติอีกสองอย่างคือ −
-
ความกว้างส่วนโค้ง − คุณสมบัตินี้แสดงถึงเส้นผ่านศูนย์กลางของส่วนโค้งที่มุมทั้ง 4 คุณสามารถตั้งค่าโดยใช้ setArcWidth() วิธีการ
-
ความสูงส่วนโค้ง − คุณสมบัตินี้แสดงถึงความสูงของส่วนโค้งที่มุมทั้ง 4 คุณสามารถตั้งค่าโดยใช้ setArcHeight() วิธีการ
โดยการตั้งค่าเหล่านี้ คุณสามารถวาดรูปสี่เหลี่ยมผืนผ้าที่มีขอบมน/โค้งได้ -
ตัวอย่าง
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.stage.Stage;
import javafx.scene.shape.Rectangle;
public class DrawingRoundedRectangle extends Application {
public void start(Stage stage) {
//Drawing a Rectangle
Rectangle shape = new Rectangle();
//Setting the properties of the rectangle
shape.setX(150.0f);
shape.setY(75.0f);
shape.setWidth(300.0f);
shape.setHeight(150.0f);
shape.setArcHeight(30.0);
shape.setArcWidth(30.0);
//Setting other properties
shape.setFill(Color.DARKCYAN);
shape.setStrokeWidth(8.0);
shape.setStroke(Color.DARKSLATEGREY);
//Setting the Scene
Group root = new Group(shape);
Scene scene = new Scene(root, 595, 300, Color.BEIGE);
stage.setTitle("Drawing Rectangle");
stage.setScene(scene);
stage.show();
}
public static void main(String args[]){
launch(args);
}
} ผลลัพธ์
