How To Draw Text In Java
This is our fifth tutorial in JavaFX, in this tutorial we are going to learn How to Draw Text in
JavaFX, Text is also a kind of shape 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 Text by creating the object of the Text class in the JavaFX. and you
need to give the x, y position and also the text itself like this.
Text text = new Text ( 100 , 50 , "Welcome to codeloop.org" ) ; |
Also you can set the font size and type of text using this code.
Font font = new Font ( "Serif" , 25 ) ; |
So now this is the complete code for How to Draw Text in JavaFX
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 | import javafx . application . Application ; import javafx . scene . Group ; import javafx . scene . Scene ; import javafx . scene . effect . DropShadow ; import javafx . scene . effect . Reflection ; import javafx . scene . paint . Color ; import javafx . scene . text . Font ; import javafx . scene . text . Text ; import javafx . stage . Stage ; public class DrawingText extends Application { public static void main ( String [ ] args ) { launch ( args ) ; } @Override public void start ( Stage stage ) { stage . setTitle ( "JavaFx 10 Drawing Text" ) ; Group root = new Group ( ) ; Scene scene = new Scene ( root , 400 , 400 , Color . RED ) ; Text text = new Text ( 100 , 50 , "Welcome to codeloop.org" ) ; Font font = new Font ( "Serif" , 25 ) ; text . setFont ( font ) ; text . setFill ( Color . WHITE ) ; // Shadows In JavaFx DropShadow shadow = new DropShadow ( ) ; shadow . setOffsetX ( 8 ) ; shadow . setOffsetY ( 8 ) ; shadow . setColor ( Color . rgb ( 20 , 70 , 70 , 0.8 ) ) ; text . setEffect ( shadow ) ; // Adding Reflection To JavaFx Text Reflection reflection = new Reflection ( ) ; reflection . setFraction ( 7 ) ; reflection . setTopOffset ( 5 ) ; text . setEffect ( reflection ) ; root . getChildren ( ) . add ( text ) ; stage . setScene ( scene ) ; stage . show ( ) ; } } |
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.
Group root = new Group ( ) ; |
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 400 width and 400 height for the
window, also a red color for the window.
Scene scene = new Scene ( root , 400 , 400 , Color . RED ) ; |
Using this code we have created our Text in JavaFX. also you can see that we have given the
font for our text.
Text text = new Text ( 100 , 50 , "Welcome to codeloop.org" ) ; Font font = new Font ( "Serif" , 25 ) ; text . setFont ( font ) ; text . setFill ( Color . WHITE ) ; |
This code is used for creating shadow for our text, and you can simply create the object
of DropShadow, after that you need to set the offset for the shadow, you can also set color
for the shadow.
DropShadow shadow = new DropShadow ( ) ; shadow . setOffsetX ( 8 ) ; shadow . setOffsetY ( 8 ) ; shadow . setColor ( Color . rgb ( 20 , 70 , 70 , 0.8 ) ) ; text . setEffect ( shadow ) ; |
In here we have codes for reflections to our text.
Reflection reflection = new Reflection ( ) ; reflection . setFraction ( 7 ) ; reflection . setTopOffset ( 5 ) ; text . setEffect ( reflection ) ; |
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 text in the
Group container.
root . getChildren ( ) . add ( text ) ; |
At the end you need to set your scene object to the stage of the window, and show the window.
stage . setScene ( scene ) ; stage . show ( ) ; |
Run the complete code and this will be the result.
Thank you for visiting my website! If you enjoyed this article, please consider supporting my works on Patreon.
How To Draw Text In Java
Source: https://codeloop.org/how-to-draw-text-in-javafx/
Posted by: mezadogese.blogspot.com
0 Response to "How To Draw Text In Java"
Post a Comment