Class LabelLayout
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final intCandidate: centred above the anchor.static final intCandidate: centred below the anchor.static final intCandidate: centred on the anchor.static final intCandidate: left of the anchor, vertically centred.static final intCandidate: right of the anchor, vertically centred. -
Constructor Summary
ConstructorsConstructorDescriptionLabelLayout(int x, int y, int w, int h) Creates a layout confined to a rectangle. -
Method Summary
Modifier and TypeMethodDescriptionstatic StringShortens text with a trailing ellipsis until it fitsmaxWidth.booleanfits(int x, int y, int w, int h) True when the box is inside the bounds and clear of every placed label.int[]getRect(int i) The bounds of theith placed or reserved box, as{x, y, w, h}.booleanplace(int ax, int ay, int w, int h, int offset, int candidates, int[] out) Tries to place awbyhbox near an anchor.booleanplaceAt(int x, int y, int w, int h) Places a box at an exact position if it is free.voidreserve(int x, int y, int w, int h) Reserves an area (a legend, a marker) that labels must avoid.voidsetBounds(int x, int y, int w, int h) Changes the bounds and forgets every placed label.voidsetGap(int gap) The minimum clearance kept between labels, in pixels.intsize()The number of labels placed so far.
-
Field Details
-
ABOVE
public static final int ABOVECandidate: centred above the anchor.- See Also:
-
BELOW
public static final int BELOWCandidate: centred below the anchor.- See Also:
-
RIGHT
public static final int RIGHTCandidate: right of the anchor, vertically centred.- See Also:
-
LEFT
public static final int LEFTCandidate: left of the anchor, vertically centred.- See Also:
-
CENTER
public static final int CENTERCandidate: centred on the anchor.- See Also:
-
-
Constructor Details
-
LabelLayout
public LabelLayout(int x, int y, int w, int h) Creates a layout confined to a rectangle.
Parameters
-
x: bounds left -
y: bounds top -
w: bounds width -
h: bounds height
-
-
-
Method Details
-
setBounds
public void setBounds(int x, int y, int w, int h) Changes the bounds and forgets every placed label. -
setGap
public void setGap(int gap) The minimum clearance kept between labels, in pixels. -
size
public int size()The number of labels placed so far. -
getRect
public int[] getRect(int i) The bounds of the
ith placed or reserved box, as{x, y, w, h}.Parameters
i: an index belowsize()
Returns
a new array holding the box
-
reserve
public void reserve(int x, int y, int w, int h) Reserves an area (a legend, a marker) that labels must avoid. -
place
public boolean place(int ax, int ay, int w, int h, int offset, int candidates, int[] out) Tries to place a
wbyhbox near an anchor.Parameters
-
ax: anchor x -
ay: anchor y -
w: box width -
h: box height -
offset: distance kept from the anchor -
candidates: a mask ofABOVE,BELOW,RIGHT,LEFT,CENTER, tried in that order -
out: receives the chosen top-left{x, y}
Returns
true if placed, false if every candidate collided
-
-
placeAt
public boolean placeAt(int x, int y, int w, int h) Places a box at an exact position if it is free.
Returns
true if the box was free and is now reserved
-
fits
public boolean fits(int x, int y, int w, int h) True when the box is inside the bounds and clear of every placed label. -
ellipsize
Shortens text with a trailing ellipsis until it fits
maxWidth.Parameters
-
f: the font it is drawn in -
text: the text -
maxWidth: the space available
Returns
the text itself if it fits, a shortened copy, or an empty string when not even the ellipsis fits
-
-