如何使用PlayN在屏幕上画一个圆? [英] How can I draw a circle to the screen with PlayN?

查看:132
本文介绍了如何使用PlayN在屏幕上画一个圆?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这几乎是最基本的.我不想使用图像文件.相反,我想以编程方式画一个圆并将其涂到表面上(正如他们在pygame中所说的那样).

This is about as basic as it gets. I don't want to use an image file. Rather, I want to programmatically draw a circle and blit it to a surface (as they say in pygame).

我尝试在此处遵循使用CanvasLayers"示例:

I tried to follow the "Using CanvasLayers" example here:

https://developers.google.com/playn/devguide/rendering

在我的游戏课上:

// Surface
SurfaceLayer surface;

// Background
int width = 640;
int height = 480;
//ImageLayer bgLayer;
CanvasImage bgImage;
Canvas canvas;

// Circle
CanvasImage circleImage;
//ImageLayer circleLayer;
int circleRadius = 20;
int circleX = 0;
int circleY = 0;


@Override
public void init() {

    // create a surface
    surface = graphics().createSurfaceLayer(width, height);
    graphics().rootLayer().add(surface);

    // create a solid background
    // http://code.google.com/p/playn101/source/browse/core/src/main/java/playn101/core/J.java#81
    bgImage = graphics().createImage(width, height);
    canvas = bgImage.canvas();
    canvas.setFillColor(0xff87ceeb);
    canvas.fillRect(0, 0, width, height);
    //bgLayer = graphics().createImageLayer(bgImage);
    //graphics().rootLayer().add(bgLayer);

    // create a circle
    circleImage = graphics().createImage(circleRadius,  circleRadius);
    canvas = circleImage.canvas();
    canvas.setFillColor(0xff0000eb);
    canvas.fillCircle(circleX, circleY, circleRadius);
    //circleLayer = graphics().createImageLayer(circleImage);
    //graphics().rootLayer().add(circleLayer);
}

@Override
public void paint(float alpha) {
    // the background automatically paints itself, so no need to do anything
    // here!
    surface.clear(0);
    surface.drawImage(bgImage, 0, 0);
    surface.drawImage(circleImage, 100, 100);
}

但是我在Java中看到一个空白窗口,并且Eclipse抱怨:

But I get a blank window in Java and Eclipse complains:

该类型的方法drawImage(CanvasImage,int,int)未定义 SurfaceLayer

The method drawImage(CanvasImage, int, int) is undefined for the type SurfaceLayer

但是,这就是链接示例中使用它的方式.

That, however, is the way it is used in the example at the link.

推荐答案

如果您提供的代码甚至可以编译,那么您正在使用PlayN的某个非常旧的版本.

If the code you provided even compiles, then you are using some very old version of PlayN.

更新到PlayN 1.1.1并修复所导致的编译错误,您的代码将正常工作.

Update to PlayN 1.1.1 and fix the compilation errors that result, and your code will work fine.

以下是您的代码已更新,可与PlayN 1.1.1一起使用:

The following is your code updated to work with PlayN 1.1.1:

  private SurfaceLayer surface;
  private CanvasImage bgImage;
  private CanvasImage circleImage;

  @Override
  public void init() {
    // create a surface
    int width = graphics().width(), height = graphics().height();
    surface = graphics().createSurfaceLayer(width, height);
    graphics().rootLayer().add(surface);

    // create a solid background
    bgImage = graphics().createImage(width, height);
    Canvas canvas = bgImage.canvas();
    canvas.setFillColor(0xff87ceeb);
    canvas.fillRect(0, 0, width, height);

    // create a circle
    int circleRadius = 20;
    int circleX = 0;
    int circleY = 0;
    circleImage = graphics().createImage(circleRadius,  circleRadius);
    canvas = circleImage.canvas();
    canvas.setFillColor(0xff0000eb);
    canvas.fillCircle(circleX, circleY, circleRadius);
  }

  @Override
  public void paint(float alpha) {
    Surface s = surface.surface();
    s.clear();
    s.drawImage(bgImage, 0, 0);
    s.drawImage(circleImage, 100, 100);
  }

如果您确实打算使用这种方法制作游戏,则应使用

If you really intend to make a game using this approach, you should use ImmediateLayer not SurfaceLayer. ImmediateLayer will issue your drawImage, etc. calls directly against the framebuffer. SurfaceLayer will make an off-screen framebuffer and render everything into that and then copy that off-screen framebuffer to the main framebuffer every frame (in addition to the double buffering naturally performed by OpenGL, etc.), resulting in a needless copy of your entire screen.

这篇关于如何使用PlayN在屏幕上画一个圆?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆