java在屏幕中画个圆,如何使用PlayN在屏幕上画一个圆?

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).

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

From my game class:

// 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);

}

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

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.

解决方案

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

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

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.

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值