Ellipse类是JavaFX库的一部分。ellipse类在提供中心以及X和Y半径的基础上创建椭圆。椭圆类扩展了形状类。
该类的构造函数是:
- 椭圆() :创建椭圆的空实例
- 椭圆(双X,双Y) :创建具有给定x和y半径的椭圆
- 椭圆(双x,双y,双x,双y) :创建具有给定中心和半径的椭圆
常用方法:
方法 | 解释 |
---|---|
getCenterX() | 返回椭圆中心的X坐标 |
getCenterY() | 返回椭圆中心的Y坐标 |
getRadiusX() | 返回X半径的值(沿长轴) |
getRadiusY() | 返回Y半径的值(沿短轴) |
setCenterX(双v) | 设置椭圆中心的X坐标 |
setCenterY(双v) | 设置椭圆中心的Y坐标 |
setRadiusX(双v) | 返回X半径的值(沿长轴) |
setRadiusY(双v) | 返回Y半径的值(沿短轴) |
设置填充(c色) | 设置椭圆的填充 |
以下程序将说明椭圆类的使用:
- Java程序通过将圆心和半径的坐标作为参数传递给构造函数来创建椭圆:
该程序创建一个椭圆,名称为Ellipse(圆心和半径的坐标作为参数传递)。椭圆将在场景中创建,而场景又将在舞台中托管。函数setTitle()用于为舞台提供标题。然后创建一个组,并附着椭圆。这群人依附于现场。最后,调用show()方法来显示最终结果。
// Java program to create ellipse by passing the
// coordinates of the center and radius as arguments in constructor
import
javafx.application.Application;
import
javafx.scene.Scene;
import
javafx.scene.control.Button;
import
javafx.scene.layout.*;
import
javafx.event.ActionEvent;
import
javafx.scene.shape.Ellipse;
import
javafx.scene.control.*;
import
javafx.stage.Stage;
import
javafx.scene.Group;
public
class
ellipse_0
extends
Application {
// launch the application
public
void
start(Stage stage)
{
// set title for the stage
stage.setTitle(
"creating ellipse"
);
// create a ellipse
Ellipse ellipse =
new
Ellipse(
200
.0f,
120
.0f,
150
.0f,
80
.f);
// create a Group
Group group =
new
Group(ellipse);
// create a scene
Scene scene =
new
Scene(group,
500
,
300
);
// set the scene
stage.setScene(scene);
stage.show();
}
public
static
void
main(String args[])
{
// launch the application
launch(args);
}
}
输出:
- Java程序,通过使用函数setCenterX()、setCenterY()等传递圆心和半径的坐标来创建椭圆:
这个程序创建一个椭圆,以椭圆的名称表示。中心和半径的坐标将使用函数setCenterX()、setCenterY()、setRadiusX()和setRadiusY()进行设置。椭圆将在场景中创建,而场景又将在舞台中托管。函数setTitle()用于为舞台提供标题。然后创建一个组,并附着椭圆。这群人依附于现场。最后,调用show()方法来显示最终结果。
// Java program to create ellipse by passing the
// coordinates of the center and radius using
// functions setCenterX(), setCenterY() etc.
import
javafx.application.Application;
import
javafx.scene.Scene;
import
javafx.scene.control.Button;
import
javafx.scene.layout.*;
import
javafx.event.ActionEvent;
import
javafx.scene.shape.Ellipse;
import
javafx.scene.control.*;
import
javafx.stage.Stage;
import
javafx.scene.Group;
public
class
ellipse_1
extends
Application {
// launch the application
public
void
start(Stage stage)
{
// set title for the stage
stage.setTitle(
"creating ellipse"
);
// create a ellipse
Ellipse ellipse =
new
Ellipse();
// set center
ellipse.setCenterX(
150
.0f);
ellipse.setCenterY(
120
.0f);
// set radius
ellipse.setRadiusX(
130
.0f);
ellipse.setRadiusY(
100
.0f);
// create a Group
Group group =
new
Group(ellipse);
// create a scene
Scene scene =
new
Scene(group,
500
,
300
);
// set the scene
stage.setScene(scene);
stage.show();
}
public
static
void
main(String args[])
{
// launch the application
launch(args);
}
}
输出:
- Java程序,通过使用函数setCenterX()、setCenterY()传递圆心和半径的坐标来创建椭圆,并使用函数setFill()设置填充:
这个程序创建一个椭圆,以椭圆的名称表示。中心和半径的坐标将使用函数setCenterX()、setCenterY()、setRadiusX()和setRadiusY()进行设置。函数setFill()将用于设置椭圆的填充。椭圆将在场景中创建,而场景又将在舞台中托管。函数setTitle()用于为舞台提供标题。然后创建一个组,并附着椭圆。这群人依附于现场。最后,调用show()方法来显示最终结果。
// Java program to create ellipse by passing the
// coordinates of the center and radius using
// functions setCenterX(), setCenterY(), and
// set a fill using setFill() function
import
javafx.application.Application;
import
javafx.scene.Scene;
import
javafx.scene.control.Button;
import
javafx.scene.layout.*;
import
javafx.event.ActionEvent;
import
javafx.scene.shape.Ellipse;
import
javafx.scene.control.*;
import
javafx.scene.paint.Color;
import
javafx.stage.Stage;
import
javafx.scene.Group;
public
class
ellipse_2
extends
Application {
// launch the application
public
void
start(Stage stage)
{
// set title for the stage
stage.setTitle(
"creating ellipse"
);
// create a ellipse
Ellipse ellipse =
new
Ellipse();
// set center
ellipse.setCenterX(
150
.0f);
ellipse.setCenterY(
120
.0f);
// set radius
ellipse.setRadiusX(
130
.0f);
ellipse.setRadiusY(
100
.0f);
// set fill
ellipse.setFill(Color.BLUE);
// create a Group
Group group =
new
Group(ellipse);
// create a scene
Scene scene =
new
Scene(group,
500
,
300
);
// set the scene
stage.setScene(scene);
stage.show();
}
public
static
void
main(String args[])
{
// launch the application
launch(args);
}
}
输出:
注: 以上程序可能无法在联机IDE中运行,请使用脱机编译器。 参考: https://docs.oracle.com/javase/8/javafx/api/javafx/scene/shape/Ellipse.html