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

public class ArtModel

  1. Object
  2. ArtModel

An editable, undoable ArtDocument with a selection.

Every change goes through this class so it can be undone: the operations below cover the common edits, and edit wraps any other change to the nodes. Undo keeps whole snapshots of the tree, so any change can be undone, however it was made. Nodes are found by id: every node gets one when the model takes the document or a new node, and the selection is kept as ids so it survives undo.

Snapshots share pixel data with the document, so never draw into a RasterNode’s raster in place: give the node a copy (setImage(getImage().copy())) after the checkpoint and draw into that.

ArtModel model = new ArtModel(new ArtDocument(200, 120));
ShapeNode layer = ShapeNode.rect(10, 10, 80, 50, 6);
layer.fill(new SolidPaint(0xff1e88e5));
model.add(layer);
model.setOpacity(layer, 0.5f);
model.undo();   // back to fully opaque

Constructors

public ArtModel()Creates a model over an empty 800 by 600 document.
public ArtModel(ArtDocument document)Creates a model over a document.

Methods

public ArtDocument getDocument()The document.
public void setDocument(ArtDocument doc)Replaces the document and clears the undo history and the selection.
public void addArtListener(ArtListener l)Adds a listener.
public void removeArtListener(ArtListener l)Removes a listener.
public void checkpoint()Saves the current tree so the next change can be undone.
public void beginGroup()Starts a group of changes that undo as one.
public void endGroup()Ends a group started by beginGroup.
public void edit(Runnable change)Makes an arbitrary change undoable: saves the tree, runs the change and notifies the listeners.
public void changed()Tells the listeners the nodes changed, after a change made between checkpoint and now, such as each step of a drag.
public boolean canUndo()True when there is something to undo.
public boolean canRedo()True when there is something to redo.
public boolean undo()Undoes the last change; returns false when there is none.
public boolean redo()Redoes the last undone change; returns false when there is none.
public void setUndoLimit(int limit)How many changes are kept for undo.
public List<ArtNode> getSelection()The selected nodes, in the order they were selected.
public ArtNode getSelected()The first selected node, or null.
public void select(ArtNode n, boolean add)Selects a node.
public void clearSelection()Clears the selection.
public boolean isSelected(ArtNode n)True when the node is selected.
public ArtNode findById(String id)The node with an id, or null.
public GroupNode parentOf(ArtNode n)The group holding a node, or null for the root.
public Matrix toDocument(ArtNode n)The transform from a node’s own space to the document, its parents’ transforms multiplied in.
public void insert(GroupNode parent, int index, ArtNode n)Inserts a node.
public void add(ArtNode n)Adds a node on top of the document, or of the selected node’s group, and selects it.
public void remove(ArtNode n)Removes a node.
public void removeSelection()Removes the selected nodes.
public void move(ArtNode n, GroupNode parent, int index)Moves a node to another group or position.
public void reorder(ArtNode n, boolean up)Moves a node one step up (toward the top) or down among its siblings.
public GroupNode group(List<ArtNode> nodes)Puts the nodes into a new group in place of the topmost of them, and selects it.
public void ungroup(GroupNode g)Replaces a group by its children, which keep their place visually, and selects them.
public void setOpacity(ArtNode n, float opacity)Sets a node’s opacity.
public void setVisible(ArtNode n, boolean visible)Shows or hides a node.
public void setLocked(ArtNode n, boolean locked)Locks or unlocks a node.
public void setName(ArtNode n, String name)Renames a node.
public void setBlendMode(ArtNode n, BlendMode mode)Sets a node’s blend mode.
public void setTransform(ArtNode n, Matrix t)Sets a node’s transform.
public void translateSelection(double dx, double dy)Moves the selected nodes by an offset in document units.
public void setFill(ShapeNode s, ArtPaint paint)Replaces a shape’s fills with one paint.
public void setStroke(ShapeNode s, StrokeStyle stroke)Replaces a shape’s strokes with one stroke.
public void addEffect(ArtNode n, Effect e)Adds an effect to a node.
public void removeEffect(ArtNode n, int index)Removes an effect from a node.
public void setEffectParameter(ArtNode n, int index, String param, Object value)Sets a parameter of one of a node’s effects.
public void setText(TextNode t, String text)Replaces a text node’s characters, keeping the first run’s style.

Inherited methods

Constructor details

ArtModel

public ArtModel()
Creates a model over an empty 800 by 600 document.

ArtModel

public ArtModel(ArtDocument document)
Creates a model over a document. The model edits that document in place.

Parameters

document ArtDocument
the document

Method details

getDocument

public ArtDocument getDocument()
The document.

setDocument

public void setDocument(ArtDocument doc)
Replaces the document and clears the undo history and the selection.

Parameters

doc ArtDocument
the new document

addArtListener

public void addArtListener(ArtListener l)
Adds a listener.

Parameters

l ArtListener
the listener

removeArtListener

public void removeArtListener(ArtListener l)
Removes a listener.

Parameters

l ArtListener
the listener

checkpoint

public void checkpoint()
Saves the current tree so the next change can be undone. Operations call this themselves; call it before changing nodes directly, or use edit.

beginGroup

public void beginGroup()
Starts a group of changes that undo as one.

endGroup

public void endGroup()
Ends a group started by beginGroup.

edit

public void edit(Runnable change)
Makes an arbitrary change undoable: saves the tree, runs the change and notifies the listeners.

Parameters

change Runnable
changes nodes of the document

changed

public void changed()
Tells the listeners the nodes changed, after a change made between checkpoint and now, such as each step of a drag.

canUndo

public boolean canUndo()
True when there is something to undo.

canRedo

public boolean canRedo()
True when there is something to redo.

undo

public boolean undo()
Undoes the last change; returns false when there is none.

redo

public boolean redo()
Redoes the last undone change; returns false when there is none.

setUndoLimit

public void setUndoLimit(int limit)
How many changes are kept for undo.

Parameters

limit int
at least 1

getSelection

public List<ArtNode> getSelection()
The selected nodes, in the order they were selected.

getSelected

public ArtNode getSelected()
The first selected node, or null.

select

public void select(ArtNode n, boolean add)
Selects a node.

Parameters

n ArtNode
the node, or null to clear the selection
add boolean
true to add it to the selection, false to replace the selection

clearSelection

public void clearSelection()
Clears the selection.

isSelected

public boolean isSelected(ArtNode n)
True when the node is selected.

Parameters

n ArtNode
the node

findById

public ArtNode findById(String id)
The node with an id, or null.

Parameters

id String
the id

parentOf

public GroupNode parentOf(ArtNode n)
The group holding a node, or null for the root.

Parameters

n ArtNode
the node

toDocument

public Matrix toDocument(ArtNode n)
The transform from a node’s own space to the document, its parents’ transforms multiplied in.

Parameters

n ArtNode
the node

insert

public void insert(GroupNode parent, int index, ArtNode n)
Inserts a node.

Parameters

parent GroupNode
the group, or null for the root
index int
the position among its children, or -1 for the top
n ArtNode
the node

add

public void add(ArtNode n)
Adds a node on top of the document, or of the selected node’s group, and selects it.

Parameters

n ArtNode
the node

remove

public void remove(ArtNode n)
Removes a node.

Parameters

n ArtNode
the node

removeSelection

public void removeSelection()
Removes the selected nodes.

move

public void move(ArtNode n, GroupNode parent, int index)
Moves a node to another group or position.

Parameters

n ArtNode
the node
parent GroupNode
the new group, or null for the root
index int
the position, or -1 for the top

reorder

public void reorder(ArtNode n, boolean up)
Moves a node one step up (toward the top) or down among its siblings.

Parameters

n ArtNode
the node
up boolean
true to bring it forward

group

public GroupNode group(List<ArtNode> nodes)
Puts the nodes into a new group in place of the topmost of them, and selects it.

Parameters

nodes List<ArtNode>
siblings or not; each keeps its place in the document visually

ungroup

public void ungroup(GroupNode g)
Replaces a group by its children, which keep their place visually, and selects them.

Parameters

g GroupNode
the group

setOpacity

public void setOpacity(ArtNode n, float opacity)
Sets a node’s opacity.

Parameters

n ArtNode
the node
opacity float
0 to 1

setVisible

public void setVisible(ArtNode n, boolean visible)
Shows or hides a node.

Parameters

n ArtNode
the node
visible boolean
true to show it

setLocked

public void setLocked(ArtNode n, boolean locked)
Locks or unlocks a node.

Parameters

n ArtNode
the node
locked boolean
true to lock it

setName

public void setName(ArtNode n, String name)
Renames a node.

Parameters

n ArtNode
the node
name String
the name

setBlendMode

public void setBlendMode(ArtNode n, BlendMode mode)
Sets a node’s blend mode.

Parameters

n ArtNode
the node
mode BlendMode
the mode

setTransform

public void setTransform(ArtNode n, Matrix t)
Sets a node’s transform.

Parameters

n ArtNode
the node
t Matrix
the transform, in its parent’s space

translateSelection

public void translateSelection(double dx, double dy)
Moves the selected nodes by an offset in document units.

Parameters

dx double
x offset
dy double
y offset

setFill

public void setFill(ShapeNode s, ArtPaint paint)
Replaces a shape’s fills with one paint.

Parameters

s ShapeNode
the shape
paint ArtPaint
the fill, or null for none

setStroke

public void setStroke(ShapeNode s, StrokeStyle stroke)
Replaces a shape’s strokes with one stroke.

Parameters

s ShapeNode
the shape
stroke StrokeStyle
the stroke, or null for none

addEffect

public void addEffect(ArtNode n, Effect e)
Adds an effect to a node.

Parameters

n ArtNode
the node
e Effect
the effect

removeEffect

public void removeEffect(ArtNode n, int index)
Removes an effect from a node.

Parameters

n ArtNode
the node
index int
the effect’s position

setEffectParameter

public void setEffectParameter(ArtNode n, int index, String param, Object value)
Sets a parameter of one of a node’s effects.

Parameters

n ArtNode
the node
index int
the effect’s position
param String
the parameter
value Object
its value

setText

public void setText(TextNode t, String text)
Replaces a text node’s characters, keeping the first run’s style.

Parameters

t TextNode
the text node
text String
the characters