Java FX Rounded Rectangle Program
The below-given code generates a Rectangle, as the name implies. The coordinates of the position and the height and width are passed as arguments. Set the rounded edges with the setArcHeight() and setArcWidth() functions. The setFill() method will be used to set the fill for the rectangle. The rectangle will be generated within a scene, which will then be hosted within a stage. The function setTitle() is used to give the stage a title. The circle is then tied to a Group that has been formed. The group is involved in the scene. The show() function is then used to display the final findings.
Code:
import javafx.application.Application;
import javafx.scene.Group;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.scene.shape.Rectangle;
public class RoundedRectangle extends Application {
@Override
public void start(Stage stage) {
//for Drawing a Rectangle shape
Rectangle rectangle = new Rectangle();
//For setting the rectangle properties
rectangle.setX(150.0f);
rectangle.setY(75.0f);
rectangle.setWidth(300.0f);
rectangle.setHeight(150.0f);
//setting the arc height and arc width
rectangle.setArcWidth(30.0);
rectangle.setArcHeight(20.0);
//Creating Group object
Group root = new Group(rectangle);
//Creating scene object
Scene scene = new Scene(root, 600, 300);
//Setting title to the Stage
stage.setTitle("Drawing a Rectangle using JavaFX");
//Adding scene to the stage
stage.setScene(scene);
//Displaying the contents of the stage
stage.show();
}
public static void main(String args[]){
launch(args);
}
}
Output:

Note:
- Unless you adjust the height and width of the arc to +ve values (0) using their corresponding setter methods setArcHeight() and setArcWidth(), JavaFX constructs a rectangle with sharp edges by default ().
- Save the above code as RoundedRectangle.java.
-
compile and execute the above-stored java file from the command prompt using these commands:
javac RoundedRectangle.java
java RoundedRectangle
Frequently Asked Questions
How does JavaFX compare to Flash and Flex?
Flex is quite amazing. However, nowadays, people have begun to implement JavaFX. I'm a little perplexed. JavaFX appears to be more focused on low-level graphics processes and animations—less emphasis on designing standard UIs, such as Flex. On the other hand, JavaFX supports Swing components and data binding, making it appear more like Flex.
What Type Of Support Is Available For JavaFX?
Java SE Support is available for JavaFX. JavaFX is one of the Java SE technologies that Oracle business clients can use. Application developers are urged to visit the JavaFX forum or log in to JIRA to report bugs and suggest new functionality.
Is JavaFX open source?
At this moment, the source code for the JavaFX UI Controls has been submitted to the OpenJFX open-source project; further JavaFX components are planned to follow in phases. The source is accessible under the GPL v2 with Classpath Exception licence, as are other OpenJDK projects.
Conclusion
This article extensively discussed JavaFX Rounded Rectangle 2-D shape and various JavaFX methods that are quite useful in making 2-D shapes. We hope this blog has helped you enhance your knowledge regarding JavaFX 2-D Shapes.
To learn more, check out our JavaFX Arc, JavaFX Circle, and JavaFX Parallel Transition articles. Practice makes a man perfect. To practice and improve yourself for the interviews, you can check out Top 100 SQL problems, Interview experience, Coding interview questions, and the Ultimate guide path for interviews. Do upvote our blog to help other ninjas grow. Happy Coding!
Thank You Image