Client API. The Codename One framework your app is built on: this runs on the device, not in a backend.

public final class LabelLayout

  1. Object
  2. LabelLayout
Places text so no two labels overlap. Every label is offered a list of candidate positions around its anchor; the first that stays inside the bounds and clear of every label already placed wins, and a label with no free position is dropped rather than drawn over another. Callers place the most important labels first.

Fields

public static final int ABOVE = 1Candidate: centred above the anchor.
public static final int BELOW = 2Candidate: centred below the anchor.
public static final int RIGHT = 4Candidate: right of the anchor, vertically centred.
public static final int LEFT = 8Candidate: left of the anchor, vertically centred.
public static final int CENTER = 16Candidate: centred on the anchor.

Constructors

public LabelLayout(int x, int y, int w, int h)Creates a layout confined to a rectangle.

Methods

public void setBounds(int x, int y, int w, int h)Changes the bounds and forgets every placed label.
public void setGap(int gap)The minimum clearance kept between labels, in pixels.
public int size()The number of labels placed so far.
public int[] getRect(int i)The bounds of the ith placed or reserved box, as {x, y, w, h}.
public void reserve(int x, int y, int w, int h)Reserves an area (a legend, a marker) that labels must avoid.
public boolean place(int ax, int ay, int w, int h, int offset, int candidates, int[] out)Tries to place a w by h box near an anchor.
public boolean placeAt(int x, int y, int w, int h)Places a box at an exact position if it is free.
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.
public static String ellipsize(Font f, String text, int maxWidth)Shortens text with a trailing ellipsis until it fits maxWidth.

Inherited methods

Field details

ABOVE

public static final int ABOVE = 1
Candidate: centred above the anchor.

BELOW

public static final int BELOW = 2
Candidate: centred below the anchor.

LEFT

public static final int LEFT = 8
Candidate: left of the anchor, vertically centred.

CENTER

public static final int CENTER = 16
Candidate: centred on the anchor.

Constructor details

LabelLayout

public LabelLayout(int x, int y, int w, int h)
Creates a layout confined to a rectangle.

Parameters

x int
bounds left
y int
bounds top
w int
bounds width
h int
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 int
an index below size()

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 w by h box near an anchor.

Parameters

ax int
anchor x
ay int
anchor y
w int
box width
h int
box height
offset int
distance kept from the anchor
candidates int
a mask of ABOVE, BELOW, RIGHT, LEFT, CENTER, tried in that order
out int[]
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

public static String ellipsize(Font f, String text, int maxWidth)
Shortens text with a trailing ellipsis until it fits maxWidth.

Parameters

f Font
the font it is drawn in
text String
the text
maxWidth int
the space available

Returns

the text itself if it fits, a shortened copy, or an empty string when not even the ellipsis fits