首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >SimpleEllipseExample只是在Piccolo2D中不工作?

SimpleEllipseExample只是在Piccolo2D中不工作?
EN

Stack Overflow用户
提问于 2013-10-09 17:02:30
回答 1查看 182关注 0票数 1

下面的例子应该做些什么呢?我什么也没画。而油漆法从未调用过:

代码语言:javascript
复制
package test.piccolo;

import java.awt.Graphics2D;
import java.awt.geom.Ellipse2D;
import java.awt.geom.Rectangle2D;

import edu.umd.cs.piccolo.PNode;
import edu.umd.cs.piccolo.nodes.PText;
import edu.umd.cs.piccolo.util.PPaintContext;
import edu.umd.cs.piccolox.PFrame;

public class SimpleEllipseNode extends PNode {
    private Ellipse2D ellipse;

    // This nodes uses an internal Ellipse2D to define its shape.
    public Ellipse2D getEllipse() {
        if (ellipse == null)
            ellipse = new Ellipse2D.Double();
        return ellipse;
    }

    // This method is important to override so that the geometry of
    // the ellipse stays consistent with the bounds geometry.
    public boolean setBounds(double x, double y, double width, double height) {
        if (super.setBounds(x, y, width, height)) {
            ellipse.setFrame(x, y, width, height);
            return true;
        }
        return false;
    }

    // Non rectangular subclasses need to override this method so
    // that they will be picked correctly and will receive the
    // correct mouse events.
    public boolean intersects(Rectangle2D aBounds) {
        return getEllipse().intersects(aBounds);
    }

    // Nodes that override the visual representation of their super
    // class need to override a paint method.
    public void paint(PPaintContext aPaintContext) {
        Graphics2D g2 = aPaintContext.getGraphics();
        g2.setPaint(getPaint());
        g2.fill(getEllipse());
    }

    public static void main(String[] args) {

        PFrame frame = new PFrame() {

            @Override
            public void initialize() {

                PNode aNode = new SimpleEllipseNode();
                //PNode aNode = new PText("Hello World!");

                // Add the node to the canvas layer so that it
                // will be displayed on the screen.
                getCanvas().getLayer().addChild(aNode);
            }

        };

    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-10-10 04:28:01

椭圆没有绘制,可能是因为SimpleEllipseNode的边界是空的。将这些行添加到initialize()中:

代码语言:javascript
复制
aNode.setPaint(Color.RED);
aNode.setBounds(0, 0, 100, 100);

它应该画一个红色的椭圆。setBounds()中存在NPE,因为ellipse尚未初始化。这可以通过添加getEllipse();作为setBounds()中的第一行来解决。

不确定重写paint()setBounds()背后的原因是什么,因为通常您可以通过使用组合节点轻松地离开。例如:

代码语言:javascript
复制
import java.awt.Color;
import edu.umd.cs.piccolo.nodes.PPath;
import edu.umd.cs.piccolox.PFrame;

public class SimpleEllipseNode {
    public static void main(final String[] args) {
        PFrame frame = new PFrame() {
            @Override
            public void initialize() {
                final PPath circle = PPath.createEllipse(0, 0, 100, 100);
                circle.setPaint(Color.RED);
                getCanvas().getLayer().addChild(circle);
            }
        };
    }
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19278308

复制
相关文章

相似问题

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