gpt4 book ai didi

java - 如何在 Piccolo2D 中使用多个图层?

转载 作者:行者123 更新时间:2023-12-01 13:41:04 25 4
gpt4 key购买 nike

我想要一些图片位于另一张图片之上,并且想要利用 PCameraaddLayer()方法。

这可能吗?

以下代码抛出NullPointerException。这是怎么回事?

package test.piccolo;

import java.awt.Color;

import edu.umd.cs.piccolo.PCamera;
import edu.umd.cs.piccolo.PLayer;
import edu.umd.cs.piccolo.nodes.PPath;
import edu.umd.cs.piccolox.PFrame;

public class Try_Cameras_01 {
@SuppressWarnings("serial")
public static void main(String[] args) {
new PFrame() {



private PLayer layer1 = new PLayer();

private PLayer layer2 = new PLayer();

private PLayer layer3 = new PLayer();

private PCamera camera = new PCamera();
{
camera.addLayer(layer1);
camera.addLayer(layer2);
camera.addLayer(layer3);
}


@Override
public void initialize() {

getCanvas().setCamera(camera);

PPath redRectangle = PPath.createRectangle(0, 0, 100, 100);
redRectangle.setStrokePaint(Color.black);
redRectangle.setPaint(Color.red);

PPath greenRectangle = PPath.createRectangle(20, 20, 100, 100);
greenRectangle.setStrokePaint(Color.black);
greenRectangle.setPaint(Color.green);

PPath blueRectangle = PPath.createRectangle(40, 40, 100, 100);
blueRectangle.setStrokePaint(Color.black);
blueRectangle.setPaint(Color.blue);

layer1.addChild(redRectangle);
layer2.addChild(greenRectangle);
layer3.addChild(blueRectangle);


}
};
}
}

最佳答案

问题是,当您设置新相机时,它没有关联的根目录。结果,PCanvas.getRoot() 返回 null,并且在其中一个绘制方法中存在 NPE。以下是基本的 Piccolo2D 运行时结构:

enter image description here

了解更多信息Piccolo2D Patterns

在您的情况下,您缺少从 PCameraPRoot 的链接。这是一个简单的修复:

private PCamera camera = new PCamera(); {
PRoot root = new PRoot();
root.addChild(camera);
camera.addLayer(layer1);
camera.addLayer(layer2);
camera.addLayer(layer3);
}

结果是:

enter image description here

此处提供了来自 PUtil.createBasicScenegraph() 的副本,用于创建基本相机。

public static PCamera createBasicScenegraph() {
final PRoot root = new PRoot();
final PLayer layer = new PLayer();
final PCamera camera = new PCamera();

root.addChild(camera);
root.addChild(layer);
camera.addLayer(layer);

return camera;
}

关于java - 如何在 Piccolo2D 中使用多个图层?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20773197/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com