What is a component's baseline in Java
Asked Answered
D

1

5

Very simple question:

What is a component's baseline in Java?

The documentation does not provide an answer as to what the "baseline" is, just describes its use by LayoutManagers. Yes, the answer is probably straightforward, but I don't want to play the guessing game.

Decorative answered 21/6, 2016 at 17:13 Comment(0)
B
8

From JavaDocs of FontMetrics

When an application asks to place a character at the position (x, y), the character is placed so that its reference point (shown as the dot in the accompanying image) is put at that position. The reference point specifies a horizontal line called the baseline of the character. In normal printing, the baselines of characters should align.

More formally a component's baseline is an imaginary line on which text is placed in a component. In general it is the distance in pixels between top-left of the component and Text's baseline. So in order to get this baseline one needs to pass height and width of the component. It is not necessary for every component to have baseline and for those components this method returns -1.

This method is used during component layout, so it can not use actual dimensions at that point because component is still being resized/repositioned. Hence it needs width and height to be passed.

For your reference as @Frakcool mentioned:

Line under "Find What:" is baseline.

Basham answered 21/6, 2016 at 17:22 Comment(3)
So in terms of components, why is a width and a height needed as arguments for getBaseline()? What's different about the width and height arguments than the component's size?Decorative
Also check the image under "Vertical align": docs.oracle.com/javase/tutorial/uiswing/layout/… it should give you an idea. @Basham I guess it complements your explanationArndt
Okay so I understand what a baseline is now. Final question is why does the component's size need to be passed as arguments if the size if readily available with Component.getSize()?Decorative

© 2022 - 2024 — McMap. All rights reserved.