如何在Piccolo2D中使用多个图层? [英] How to use multiple layers in Piccolo2D?
问题描述
我想要有一张照片在另一张上面,并且想要利用。
在您的情况下,您缺少从 PCamera 的
code>。这是一个简单的修复:
私人PCamera相机=新PCamera(); {
PRoot root = new PRoot();
root.addChild(camera);
camera.addLayer(layer1);
camera.addLayer(layer2);
camera.addLayer(layer3);
}
结果如下:
仅供参考这是从 PUtil.createBasicScenegraph()
创建一个基本相机的副本。
public static PCamera createBasicScenegraph(){
final PRoot root = new PRoot();
最终PLayer图层=新PLayer();
最终PCamera相机=新PCamera();
root.addChild(camera);
root.addChild(layer);
camera.addLayer(layer);
返回相机;
}
I want t have some picture above another one and want to utilize PCamera
's addLayer()
method.
Is this possible?
The following code throws NullPointerException
. What's wrong with it?
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);
}
};
}
}
The problem is that when you set up a new camera it has no associated root. As a result, PCanvas.getRoot()
returns null
and there an NPE
in one of the painting methods. Here is a basic Piccolo2D runtime structure:
Read more in Piccolo2D Patterns.
In your case you're missing a link to PRoot
from a PCamera
. Here is a simple fix:
private PCamera camera = new PCamera(); {
PRoot root = new PRoot();
root.addChild(camera);
camera.addLayer(layer1);
camera.addLayer(layer2);
camera.addLayer(layer3);
}
That results in:
For reference here is a copy from PUtil.createBasicScenegraph()
that creates a basic camera.
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;
}
这篇关于如何在Piccolo2D中使用多个图层?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!