无法让 Canvas/CanvasLayer 在 Playn 中工作

发布于 2024-12-23 02:04:14 字数 1023 浏览 1 评论 0原文

就像标题所说,我无法让 Canvas/CanvasLayer 在 PlayN 中工作。我的示例代码再简单不过了。 ImageLayer 和默认背景加载良好(该代码已在此处注释掉),但画布实际上始终不可见。

public void init() {
    // create and add background image layer
    //Image bgImage = assetManager().getImage("images/bg.png");
    //ImageLayer bgLayer = graphics().createImageLayer(bgImage);
    //graphics().rootLayer().add(bgLayer);      
    graphics().setSize(500, 400);

    CanvasLayer testLayer = graphics().createCanvasLayer(200,200);
    Canvas testCanvas = testLayer.canvas();

    graphics().rootLayer().add(testLayer);
    //testCanvas.clear();

    testCanvas.setStrokeColor(0x000000);
    testCanvas.setStrokeWidth(2);
    testCanvas.setFillColor(0xff0000);
    testCanvas.drawText("hello", 50, 50);
    testCanvas.drawLine(0, 0, 300,300);
    testCanvas.strokeRect(1, 1, 46, 46);

    //I even tried adding this.
    testLayer.setAlpha(1);
    testLayer.setVisible(true);
    testLayer.setScale(1);
}

我正在使用 PlayN 1.0.3 和 Maven。请注意,这是针对纯 java 编译的。

蒂亚·丹尼尔

Like the title says, I can't get Canvas/CanvasLayer working in PlayN. My sample code couldn't be simpler. The ImageLayer and default background loads fine (that code is commented out here), but the canvas is practically invisible always.

public void init() {
    // create and add background image layer
    //Image bgImage = assetManager().getImage("images/bg.png");
    //ImageLayer bgLayer = graphics().createImageLayer(bgImage);
    //graphics().rootLayer().add(bgLayer);      
    graphics().setSize(500, 400);

    CanvasLayer testLayer = graphics().createCanvasLayer(200,200);
    Canvas testCanvas = testLayer.canvas();

    graphics().rootLayer().add(testLayer);
    //testCanvas.clear();

    testCanvas.setStrokeColor(0x000000);
    testCanvas.setStrokeWidth(2);
    testCanvas.setFillColor(0xff0000);
    testCanvas.drawText("hello", 50, 50);
    testCanvas.drawLine(0, 0, 300,300);
    testCanvas.strokeRect(1, 1, 46, 46);

    //I even tried adding this.
    testLayer.setAlpha(1);
    testLayer.setVisible(true);
    testLayer.setScale(1);
}

I'm using PlayN 1.0.3 with Maven. Note this is for the pure java compilation.

TIA Daniel

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

浅暮の光 2024-12-30 02:04:14

您的颜色代码是错误的,您还必须指定 alpha

0x000000 实际上意味着 00 alpha, 00 red, 00 gren

试试这个:

testCanvas.setStrokeColor(Color.rgb(0, 0, 0) ));

或者如果您想要 50% 透明度:

testCanvas.setStrokeColor(Color.argb(0x80, 0, 0, 0));

您可能还想在添加 CanvasLayer 之前添加背景

    SurfaceLayer bgLayer = graphics().createSurfaceLayer(500, 400);
    bgLayer.surface().setFillColor(Color.rgb(0,0,0));
    bgLayer.surface().fillRect(0, 0, bgLayer.surface().width(),
            bgLayer.surface().height());
    graphics().rootLayer().add(bgLayer);

Your color codes are wrong, you must specify alpha as well

0x000000 actually means 00 alpha, 00 red, 00 gren

try this instead:

testCanvas.setStrokeColor(Color.rgb(0, 0, 0));

or if you want 50% transparency:

testCanvas.setStrokeColor(Color.argb(0x80, 0, 0, 0));

you might also want to add a background as well, before you add your CanvasLayer:

    SurfaceLayer bgLayer = graphics().createSurfaceLayer(500, 400);
    bgLayer.surface().setFillColor(Color.rgb(0,0,0));
    bgLayer.surface().fillRect(0, 0, bgLayer.surface().width(),
            bgLayer.surface().height());
    graphics().rootLayer().add(bgLayer);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文