Java GUI – Drawing Rectangle in JavaFX

This is our fourth tutorial in Java GUI, in this tutorial we learn about Drawing Rectangle in

JavaFX. in the previous article we have learned that how you can draw line in JavaFX, so if

you are interested you can read this article , Create Line in JavaFX

 

 

 

 

 

Also you can check Python GUI Development Tutorials in the below link.

1: PyQt5 GUI Development Tutorials

2: TKinter GUI Development Tutorials

3: Pyside2 GUI Development Tutorials 

4: Kivy GUI Development Tutorials

5: wxPython GUI Development Tutorials

 

 

 

 

You can draw Rectangle by creating the object of the Rectangle class in the JavaFX. and you

need to give the x, y, width and height of the Rectangle.

 

 

 

 

You can also create JavaFX Rectangle like this.

 

 

 

 

Also in this tutorial we are going to draw Quad, so you can use this code for drawing

Quad in JavaFX. for drawing of the quad you need to give start and end positions for x and y 

and also control x and y.

 

 

 

 

 

 

So now this is the complete code for Java GUI – Drawing Rectangle in JavaFX

 

 

 

 

 

Every JavaFX application should have a container, a container is like layout. as we

have created a Group container in the above code. but you can use different containers

from JavaFX.

 

 

 

 

Also for every JavaFX application we need to create a Scene object. in the scene we need

to add our container with the width and height of the window, if you want to colorize your

window, you can do it in here. you can see that we have given 500 width and 400 height for the

window, also a red color for the window.

 

 

 

 

This code is for creating Rectangle. we have given the color for the stroke and also for the fill.

for every rectangle you need to to give the width and height, also X and Y position.

 

 

 

 

If you want to rotate your JavaFX Rectangle, you can use this code.

 

 

 

 

This is for drawing of the JavaFX QuadCurve, basically we have created the object of QuadCurve.

 

 

 

 

When you create a widget in JavaFX, for example button,label,combobox or some other

widgets, you need to add that to your container, as we have added our this myrect in the

Group container.

 

 

 

 

 

At the end you need to set your scene object to the stage of the window, and show the window.

 

 

 

 

Run the complete code and this will be the result.

Java GUI - Drawing Rectangle in JavaFX
Java GUI – Drawing Rectangle in JavaFX

Subscribe and Get Free Video Courses & Articles in your Email

 

Share via
Copy link
Powered by Social Snap
×