JavaFX Rectangle and Ellipse Tutorial with Examples
1. JavaFX Rectangle
JavaFX Rectangle is the class that helps draw a rectangle, the same as class Line and Circle, it is extends from Shape class.
RectangleDemo.java
package org.o7planning.javafx.rectangle;
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 RectangleDemo extends Application {
@Override
public void start(Stage primaryStage) {
Rectangle rectangle = new Rectangle();
rectangle.setX(20);
rectangle.setY(50);
rectangle.setWidth(200);
rectangle.setHeight(150);
rectangle.setFill(Color.BISQUE);
Group root = new Group();
root.getChildren().add(rectangle);
Scene scene = new Scene(root, 400, 250, Color.WHITE);
primaryStage.setTitle("JavaFX Rectangle (o7planning.org)");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
Application.launch(args);
}
}
2. Rounded corner rectangle
RectangleRoundedCornerDemo.java
package org.o7planning.javafx.rectangle;
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 RectangleRoundedCornerDemo extends Application {
@Override
public void start(Stage primaryStage) {
Rectangle rectangle = new Rectangle();
rectangle.setX(80);
rectangle.setY(50);
rectangle.setWidth(200);
rectangle.setHeight(150);
rectangle.setFill(Color.CADETBLUE);
rectangle.setArcHeight(105);
rectangle.setArcWidth(50);
Group root = new Group();
root.getChildren().add(rectangle);
Scene scene = new Scene(root, 400, 250, Color.WHITE);
primaryStage.setTitle("JavaFX Rectangle (o7planning.org)");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
Application.launch(args);
}
}
3. JavaFX Ellipse
EllipseDemo.java
package org.o7planning.javafx.ellipse;
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.scene.shape.Ellipse;
import javafx.stage.Stage;
public class EllipseDemo extends Application {
@Override
public void start(Stage primaryStage) {
Group root = new Group();
Ellipse ellipse = new Ellipse();
ellipse.setCenterX(180.0f);
ellipse.setCenterY(150.0f);
// Radius X
ellipse.setRadiusX(120.0f);
// Radius Y
ellipse.setRadiusY(80.0f);
// Fill color.
ellipse.setFill(Color.CORNFLOWERBLUE);
root.getChildren().add(ellipse);
Scene scene = new Scene(root, 400, 250);
primaryStage.setTitle("JavaFX Ellipse (o7planning.org)");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
Application.launch(args);
}
}
4. Effects
Rotate:
EllipseDemo2.java
package org.o7planning.javafx.ellipse;
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.scene.paint.Color;
import javafx.scene.shape.Ellipse;
import javafx.stage.Stage;
public class EllipseDemo2 extends Application {
@Override
public void start(Stage primaryStage) {
Group root = new Group();
Ellipse ellipse = new Ellipse();
ellipse.setCenterX(180.0f);
ellipse.setCenterY(150.0f);
// Radius X
ellipse.setRadiusX(120.0f);
// Radius Y
ellipse.setRadiusY(80.0f);
// Fill color.
ellipse.setFill(Color.CORNFLOWERBLUE);
// Rotate 30 degrees
ellipse.setRotate(30);
root.getChildren().add(ellipse);
Scene scene = new Scene(root, 400, 250);
primaryStage.setTitle("JavaFX Ellipse (o7planning.org)");
primaryStage.setScene(scene);
primaryStage.show();
}
public static void main(String[] args) {
Application.launch(args);
}
}
JavaFX Tutorials
- Open a new Window in JavaFX
- JavaFX ChoiceDialog Tutorial with Examples
- JavaFX Alert Dialogs Tutorial with Examples
- JavaFX TextInputDialog Tutorial with Examples
- Install e(fx)clipse for Eclipse (JavaFX Tooling)
- Install JavaFX Scene Builder for Eclipse
- JavaFX Tutorial for Beginners - Hello JavaFX
- JavaFX FlowPane Layout Tutorial with Examples
- JavaFX TilePane Layout Tutorial with Examples
- JavaFX HBox, VBox Layout Tutorial with Examples
- JavaFX BorderPane Layout Tutorial with Examples
- JavaFX AnchorPane Layout Tutorial with Examples
- JavaFX TitledPane Tutorial with Examples
- JavaFX Accordion Tutorial with Examples
- JavaFX ListView Tutorial with Examples
- JavaFX Group Tutorial with Examples
- JavaFX ComboBox Tutorial with Examples
- JavaFX Transformations Tutorial with Examples
- JavaFX Effects Tutorial with Examples
- JavaFX GridPane Layout Tutorial with Examples
- JavaFX StackPane Layout Tutorial with Examples
- JavaFX ScrollPane Tutorial with Examples
- JavaFX WebView and WebEngine Tutorial with Examples
- JavaFX HTMLEditor Tutorial with Examples
- JavaFX TableView Tutorial with Examples
- JavaFX TreeView Tutorial with Examples
- JavaFX TreeTableView Tutorial with Examples
- JavaFX Menu Tutorial with Examples
- JavaFX ContextMenu Tutorial with Examples
- JavaFX Image and ImageView Tutorial with Examples
- JavaFX Label Tutorial with Examples
- JavaFX Hyperlink Tutorial with Examples
- JavaFX Button Tutorial with Examples
- JavaFX ToggleButton Tutorial with Examples
- JavaFX RadioButton Tutorial with Examples
- JavaFX MenuButton and SplitMenuButton Tutorial with Examples
- JavaFX TextField Tutorial with Examples
- JavaFX PasswordField Tutorial with Examples
- JavaFX TextArea Tutorial with Examples
- JavaFX Slider Tutorial with Examples
- JavaFX Spinner Tutorial with Examples
- JavaFX ProgressBar and ProgressIndicator Tutorial with Examples
- JavaFX ChoiceBox Tutorial with Examples
- JavaFX Tooltip Tutorial with Examples
- JavaFX DatePicker Tutorial with Examples
- JavaFX ColorPicker Tutorial with Examples
- JavaFX FileChooser and DirectoryChooser Tutorial with Examples
- JavaFX PieChart Tutorial with Examples
- JavaFX AreaChart and StackedAreaChart Tutorial with Examples
- JavaFX BarChart and StackedBarChart Tutorial with Examples
- JavaFX Line Tutorial with Examples
- JavaFX Rectangle and Ellipse Tutorial with Examples
Show More