Issue
I have a graphic context:
gc.strokeText(String.format("%d",test), 0, 0);
I add this to the stage like this:
p.getChildren().addAll(canvas);
I get this result:
href="https://i.stack.imgur.com/P1Ip8.png" rel="nofollow noreferrer">
What can i do, or is there a way to prevent the canvas to start behind title bar? (I can do this by setting the start position x,y, but that seems a oncomfortabel solution)
Is there a way to set the canvas just below the title bar, no matter how high this title bar is.
Thanks
Solution
The scene and the canvas it contains are positioned immediately below the title bar. The problem is the way you have positioned the text in the canvas.
The text is positioned in the canvas according to the coordinates provided and the current values of the GraphicsContext
's textAlign
property (which determines how the x
coordinate is used) and textBaseline
property (which determines how the y
coordinate is used). See the "Text Attributes" section of the attributes table in the documentation.
The default textBaseline
property is VPos.BASELINE
, which means the baseline of the text will be positioned with the provided y-coordinate, which is 0
in your case. Thus the text baseline will be at 0
(the top of the canvas), and most of the text (everything except any descenders) will be above the top of the canvas and not be visible.
To position the top of the text at the y-coordinate 0
, set the textBaseline
to TOP
:
gc.setTextBaseline(VPos.TOP);
gc.strokeText(String.format("%d",test), 0, 0);
Answered By - James_D
Answer Checked By - David Marino (JavaFixing Volunteer)