Layout algorithms in Piccolo2D

64 views
Skip to first unread message

ahbejarano

unread,
Nov 27, 2009, 4:21:32 AM11/27/09
to Piccolo2D Users
I have not found any reference to different layout algorithms in
Piccolo2D. So here are some questions.

Does Piccolo2D include layout algortihms (tree, radial, etc.)
(Something equivalent as JGraph) ?

If it does not support it, are there any open source extensions to
Piccolo2D for doing that ?

Thanks in advance.

Travis

unread,
Nov 28, 2009, 11:39:01 PM11/28/09
to Piccolo2D Users
That's a good question. I'm fairly new to Piccolo2D, but I think not.
However, the position method on PNode is a good place to start.

Nigel

unread,
Nov 29, 2009, 6:56:58 AM11/29/09
to Piccolo2D Users

I work a lot with Swing so I created a solution very much like Swing's
layout managers for use with Piccolo.

I did the following

1) Created a PLayoutManager interface, mine contained the following
methods

public void removeLayoutComponent(PNode child);
public void addLayoutComponent(PNode child, PLayoutConstraints const
raints);
public void layoutContainer(PNode target);

2) Extended PNode (I called mine a LayoutNode) so can set a layout
manager on it

setLayout(PLayoutManager layout)

then, and so you can add a child with constraints, I added these 2
addChild methods.

public void addChild(final PNode child, PLayoutConstraints
constraints)
public void addChild(final int index, final PNode child,
PLayoutConstraints constraints)

The above 2 methods just call the equivalent super's addChild methods
to add the child to the node and then call addLayoutComponent on the
layout manager to indicate this child will be positioned by the layout
manager.

Similarly override
public PNode removeChild(final int index)
to call the layout manager's removeLayoutComponent method after the
super's emoveChild method.

then override layoutChildren, mine looks like this

protected void layoutChildren() {
if (layout !=null) {
layout.layoutContainer(this);
}
}


3) Write a class that implements your desired layout and pass an
instance of it to the node whose children you want to layout with the
setLayout() method. The node with the layout manager will now
automatically position its children.


I did think while I was implementing my layout manager that maybe
the layout manager interface I added ; and
PNode's setLayout method I added ; and
PNode's extra addChild methods ; and
PNode's layoutChildren I overrode (to use the layout manager if
set)
could / should all be part of piccolo, then all you would have to do
to use a layout would be to implement one and call setLayout on the
PNode.


Nigel

Samuel Robert Reid

unread,
Nov 30, 2009, 3:13:22 PM11/30/09
to piccolo...@googlegroups.com
My colleagues and I also saw a need for a Swing-like layout solution for Piccolo2D, so we wrote an adapter that actually uses a swing layout manager to arrange Piccolo nodes.  I've attached the entire implementation and example usage in SwingLayoutNode.java.  Let's discuss whether this should be included in the upcoming Piccolo 1.3 release.

Sample using Swing's FlowLayout:
        SwingLayoutNode flowLayoutNode = new SwingLayoutNode( new FlowLayout() );
        flowLayoutNode.addChild( new PText( "1+1" ) );
        flowLayoutNode.addChild( new PText( "2+2" ) );

Sample using Swing's BorderLayout:
        BorderLayout borderLayout = new BorderLayout();
        borderLayout.setHgap( 10 );
        borderLayout.setVgap( 5 );
        SwingLayoutNode borderLayoutNode = new SwingLayoutNode( borderLayout );
        borderLayoutNode.addChild( new PText( "North" ), BorderLayout.NORTH );
        borderLayoutNode.setAnchor( Anchor.CENTER );
        borderLayoutNode.addChild( new PText( "South" ), BorderLayout.SOUTH );
        borderLayoutNode.setAnchor( Anchor.WEST );
        borderLayoutNode.addChild( new PText( "East" ), BorderLayout.EAST );
        borderLayoutNode.addChild( new PText( "West" ), BorderLayout.WEST );
        borderLayoutNode.addChild( new PText( "CENTER" ), BorderLayout.CENTER );

I'm also attaching a screenshot of the sample main() for your convenience.

Thanks,
Sam Reid

Nigel wrote:
--

You received this message because you are subscribed to the Google Groups "Piccolo2D Users" group.
To post to this group, send email to piccolo...@googlegroups.com.
To unsubscribe from this group, send email to piccolo2d-use...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/piccolo2d-users?hl=en.


  
SwingLayoutNode.java
SwingLayoutNode.png

Michael Heuer

unread,
Dec 1, 2009, 2:01:28 PM12/1/09
to piccolo...@googlegroups.com
Samuel Robert Reid <Re...@colorado.edu> wrote:

> My colleagues and I also saw a need for a Swing-like layout solution for
> Piccolo2D, so we wrote an adapter that actually uses a swing layout manager
> to arrange Piccolo nodes.  I've attached the entire implementation and
> example usage in SwingLayoutNode.java.  Let's discuss whether this should be
> included in the upcoming Piccolo 1.3 release.

I think this is cool, and a good fit for piccolox.swing in extras.
Could you create an issue for this? With the proper license header,
unit tests, checkstyle fixes, and refactoring of the example into a
separate class, this could go into 1.3.

michael

Michael Heuer

unread,
Dec 1, 2009, 2:04:30 PM12/1/09
to piccolo...@googlegroups.com
Nigel <nigel....@f2s.com> wrote:

> I work a lot with Swing so I created a solution very much like Swing's
> layout managers for use with Piccolo.
>
> ...
>
> I did think while I was implementing my layout manager that maybe
>    the layout manager interface I added ; and
>    PNode's setLayout method I added ; and
>    PNode's extra addChild methods ; and
>    PNode's layoutChildren I overrode (to use the layout manager if
> set)
> could / should all be part of piccolo, then all you would have to do
> to use a layout would be to implement one and call setLayout on the
> PNode.

This is also pretty cool. I'm not convinced we should make changes to
core though. Please create a new issue, or comment on/post
attachments to Sam's issue on this thread.

michael

Jan Kotek

unread,
Dec 18, 2009, 5:04:52 AM12/18/09
to piccolo...@googlegroups.com
Hi,

I have an algorithm for label placement (to not overlap with other
nodes). I will post it in a few weeks.
More details at: http://kotek.net/blog/label_layout_algorithm

Jan

Reply all
Reply to author
Forward
0 new messages