首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >PLayer在Piccolo2d中的扩展

PLayer在Piccolo2d中的扩展
EN

Stack Overflow用户
提问于 2014-07-21 19:45:39
回答 1查看 141关注 0票数 1

我有下一个问题。在我的项目中,当从层中添加或删除节点时,我需要将消息委托给第三方库。为了实现这一点,我以下一种方式扩展了PLayer:

代码语言:javascript
复制
public class DelegateLayer extends PLayer {
    private Delegate delegate = null;
    private boolean delegationNeeded = false;
    public DelegateLayer() {
        super();
    }
    @Override
    public void removeChildren(final Collection children) {
        for (Object child : children) {
            removeChild((PNode)child);
        }
    }
    @Override
    public void addChildren(final Collection children) {
        for (Object child : children) {
            addChild((PNode) child);
        }
    }

    @Override
    public void addChild(final PNode child) {
        if (delegationNeeded) {
            Preconditions.checkNotNull(delegate, "DelegateLayer: Delegate is not initialized");
            delegate.delegateNodeAdded((CloudNode)child);
        }
        super.addChild(child);
    }

    @Override
    public PNode removeChild(final PNode child) {
        if (delegationNeeded) {
            Preconditions.checkNotNull(delegate, "DelegateLayer: Delegate is not initialized");
            delegate.delegateNodeRemoved((CloudNode)child);
        }
        return super.removeChild(child);
    }

    public void setDelegationNeeded(boolean needed) {
        this.delegationNeeded = needed;
    }

    public void setDelegate(ClusterUpdateDelegate delegate) {
        this.delegate = delegate;
    }
}

我还将这个节点层添加到画布的摄像机中:

代码语言:javascript
复制
DelegateLayer nodeLayer = new DelegateLayer();
camera.addLayer(0, nodeLayer);

但是,在我将节点放置到层并应用转换(在点上对节点进行中心化)之后,什么都不会发生。但是一旦我告诉PLayer,我开始使用camera.getLayer(0),一切都很好。

有谁能解释一下是怎么回事吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-07-21 23:08:06

您可能缺少一个将新创建的层添加到PRoot中的调用。下面是在Piccolo2D模式中描述的运行时结构的快照:

在这个简短的演示中,当您注释掉canvas.getCamera().getRoot().addChild(layer);时,动画就停止工作了:

代码语言:javascript
复制
import java.awt.Color;
import java.awt.Dimension;
import java.awt.geom.Point2D;
import javax.swing.JFrame;
import javax.swing.SwingUtilities;

import edu.umd.cs.piccolo.PCanvas;
import edu.umd.cs.piccolo.PLayer;
import edu.umd.cs.piccolo.PNode;
import edu.umd.cs.piccolo.event.PBasicInputEventHandler;
import edu.umd.cs.piccolo.event.PInputEvent;
import edu.umd.cs.piccolo.nodes.PPath;
import edu.umd.cs.piccolo.util.PBounds;

public class Test {
    public static void main(String[] args) {
        SwingUtilities.invokeLater(new Runnable() {   
            public void run() {   
                JFrame frame = new JFrame("Test");
                frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                frame.setLocationByPlatform(true);

                final PCanvas canvas = new PCanvas() {
                    @Override
                    public Dimension getPreferredSize() {
                        return new Dimension(600, 400);
                    }
                };

                PLayer layer = new PLayer() {
                    @Override
                    public void addChild(final PNode child) {
                        System.out.println("notify: addChild");
                        super.addChild(child);
                    }
                };

                canvas.getCamera().addLayer(0, layer);
                canvas.getCamera().getRoot().addChild(layer);

                final PPath node = PPath.createRectangle(0, 0, 100, 100);
                node.setPaint(Color.RED);
                canvas.getLayer().addChild(node);

                canvas.addInputEventListener(new PBasicInputEventHandler() {
                    @Override
                    public void mouseClicked(PInputEvent event) {
                        Point2D p = event.getCamera().localToView(
                                event.getCanvasPosition());
                        PBounds bounds = node.getBounds();
                        final double dx = p.getX() - bounds.getCenterX();
                        final double dy = p.getY() - bounds.getCenterY();
                        node.animateToBounds(node.getBounds().x + dx, bounds.y
                                + dy, bounds.width, bounds.height, 300);
                    }
                });

                frame.add(canvas);            
                frame.pack();
                frame.setVisible(true);
            }
        });
    }    
}

另外,您还可以监听PNode.PROPERTY_CHILDREN属性,而无需添加自定义层:

代码语言:javascript
复制
canvas.getLayer().addPropertyChangeListener(PNode.PROPERTY_CHILDREN, new PropertyChangeListener() {
    @Override
    public void propertyChange(PropertyChangeEvent e) {
        System.out.println("notify");
    }
});

但是,在这种方法中,您没有添加/删除哪个子节点的信息。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24873417

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档