Table of contents
1.
Introduction
2.
JavaFX Rectangle
2.1.
Constructor: 
2.2.
Commonly used methods  
3.
Java FX Rounded Rectangle Program
3.1.
Code:
3.1.1.
Output:
3.2.
Note:
4.
Frequently Asked Questions
4.1.
How does JavaFX compare to Flash and Flex?
4.2.
What Type Of Support Is Available For JavaFX?
4.3.
Is JavaFX open source?
5.
Conclusion
Last Updated: Mar 27, 2024

JavaFX Rounded Rectangle

Author Akash Nagpal
0 upvote
Career growth poll
Do you think IIT Guwahati certified course can help you in your career?

Introduction

JavaFX is a Java library that is used to create desktop and rich Internet applications (RIA). JavaFX applications may operate on various platforms, including Web, Mobile, and Desktop. In this blog, we will learn about JavaFX Rounded Rectangle and various JavaFX methods that are useful in making 2-D shapes.

JavaFX Rectangle

The JavaFX library enables developers to generate a rectangle by invoking JavaFX.scene.shape. JavaFX includes the Rectangle class. The Rectangle class generates a rectangle with a given width, height, and position. This Rectangle class in JavaFX has sharp corners by default, but the edges may be smoothed by adding an arc height and width.

Constructor: 

Following is the list of the constructors that can be used to make a Rounded Rectangle 2-D Shape through JavaFX:

  • Rectangle(): returns an empty rectangle object.
  • Rectangle(double w, double h): This function generates a rectangle with the given height and width of the rectangle.
  • Rectangle (double p, double q, double r, double s): It generates a rectangle with a given width, height, and location.
  • Rectangle(double w, double h, Paint f): It draws a rectangle with a specified width and height and fills it with a colour.

Commonly used methods  

The most useful methods in Rounded Rectangle class from JavaFX are as follows: 

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:

Code 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 ArcJavaFX 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 problemsInterview experienceCoding interview questions, and the Ultimate guide path for interviews. Do upvote our blog to help other ninjas grow. Happy Coding!

Thank you image 

Thank You Image

Live masterclass