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

public class LayoutPainter

  1. Object
  2. LayoutPainter

Paints laid out items on a Graphics, the way DocumentView paints its pages.

Anything that shows text laid out by DocumentLayout – a slide’s text box, a thumbnail, a print preview – paints it with this class, so it looks exactly as it does in the word processor: each word is drawn where the layout measured it, underlines and strike lines follow the measured text, and pictures are decoded once and kept.

void paintFirstPage(Graphics g, Document doc, int x, int y) {
    DocumentLayout layout = new DocumentLayout(doc, new ScreenMeasurer());
    LayoutPainter painter = new LayoutPainter(layout.getMeasurer());
    painter.paint(g, layout.getPage(0).getItems(), x, y, 2f);
}

Constructors

public LayoutPainter(Measurer measurer)A painter for items laid out with a measurer.

Methods

public Measurer getMeasurer()The measurer items are positioned with.
public Font font(TextStyle style, int px)The platform font text of a style is drawn in, at a pixel size.
public void paint(Graphics g, List<LayoutItem> items, int left, int top, float scale)Paints items.
public void paintItem(Graphics g, LayoutItem it, int left, int top, float scale, boolean lastOnLine)Paints one item.
public void paintText(Graphics g, LayoutItem it, int x, int baseline, float scale, boolean lastOnLine)Paints a text item: its words, underline, strike line and revision marks.
public void paintPicture(Graphics g, Picture p, int x, int y, int w, int h)Paints a picture scaled into a box, or a frame when it cannot be decoded.
public Image image(Picture p)The decoded image of a picture, decoded once; null when it is linked or not an image the platform reads.
public int offset(LayoutItem it, int chars, float scale)Where the first chars characters of an item end, in pixels from its left edge, by the measurer the item was laid out with.
public void drawWords(Graphics g, LayoutItem it, int x, int y, float scale)Draws an item a word at a time, each where the layout put it, in the graphics’ current font and color.

Inherited methods

Constructor details

LayoutPainter

public LayoutPainter(Measurer measurer)
A painter for items laid out with a measurer.

Parameters

measurer Measurer
the measurer the items were laid out with; word positions are computed with it

Method details

getMeasurer

public Measurer getMeasurer()
The measurer items are positioned with.

Returns

the measurer

font

public Font font(TextStyle style, int px)
The platform font text of a style is drawn in, at a pixel size.

Parameters

style TextStyle
the style
px int
the size in pixels

Returns

the font

paint

public void paint(Graphics g, List<LayoutItem> items, int left, int top, float scale)
Paints items.

Parameters

g Graphics
the graphics
items List<LayoutItem>
the items, in points relative to their page
left int
where the page’s left edge is, in pixels
top int
where the page’s top edge is, in pixels
scale float
pixels per point

paintItem

public void paintItem(Graphics g, LayoutItem it, int left, int top, float scale, boolean lastOnLine)
Paints one item.

Parameters

g Graphics
the graphics
it LayoutItem
the item
left int
the page’s left edge in pixels
top int
the page’s top edge in pixels
scale float
pixels per point
lastOnLine boolean
true when it is the last item of its line, whose underline stops at its last glyph instead of running through its trailing spaces

paintText

public void paintText(Graphics g, LayoutItem it, int x, int baseline, float scale, boolean lastOnLine)
Paints a text item: its words, underline, strike line and revision marks.

Parameters

g Graphics
the graphics
it LayoutItem
a LayoutItem.TEXT item
x int
its left edge in pixels
baseline int
its baseline in pixels
scale float
pixels per point
lastOnLine boolean
true when it ends its line

paintPicture

public void paintPicture(Graphics g, Picture p, int x, int y, int w, int h)
Paints a picture scaled into a box, or a frame when it cannot be decoded.

Parameters

g Graphics
the graphics
p Picture
the picture
x int
the box’s left edge
y int
its top edge
w int
its width
h int
its height

image

public Image image(Picture p)
The decoded image of a picture, decoded once; null when it is linked or not an image the platform reads.

Parameters

p Picture
the picture

Returns

the image or null

offset

public int offset(LayoutItem it, int chars, float scale)
Where the first chars characters of an item end, in pixels from its left edge, by the measurer the item was laid out with.

Parameters

it LayoutItem
a text item
chars int
the number of characters
scale float
pixels per point

Returns

the offset in pixels

drawWords

public void drawWords(Graphics g, LayoutItem it, int x, int y, float scale)
Draws an item a word at a time, each where the layout put it, in the graphics’ current font and color. The font drawn at a whole pixel size is never exactly the one measured, and drawn in one piece the difference would add up along the run; this way it stays inside the space after each word.

Parameters

g Graphics
the graphics
it LayoutItem
a text item
x int
its left edge in pixels
y int
the top of its glyphs in pixels
scale float
pixels per point