Gábor Bakos
Gábor Bakos

Reputation: 9100

PlayN: clear part of an Image

I would like to overwrite/clear part of an image, using transparent colour in PlayN. Is this possible somehow? (PlayN 1.3.1)

Here is a sample code I did:

  @Override
  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);
    SurfaceLayer surfaceLayer = graphics().createSurfaceLayer(640, 480);
    CanvasImage image = graphics().createImage(640, 480);
    surfaceLayer.surface().setFillColor(0xff000000);
    surfaceLayer.surface().fillRect(100, 100, 200, 200);
    surfaceLayer.surface().setFillColor(0x00000000);
    surfaceLayer.surface().fillRect(150, 150, 50, 50);
    graphics().rootLayer().add(surfaceLayer);
    image.canvas().setFillColor(0xff000000);
    image.canvas().fillCircle(300, 300, 100);
    image.canvas().setFillColor(0x00000000);
    image.canvas().fillCircle(300, 300, 50);
    graphics().rootLayer().add(graphics().createImageLayer(image));
  }

So I have tried with SurfaceLayer, and also with ImageLayer none of them were helping (I guess this way it is not possible to overwrite parts). I wanted to have two intersecting rectangles, and circles, but I only see one of each.

Upvotes: 0

Views: 195

Answers (1)

Keldon Alleyne
Keldon Alleyne

Reputation: 2130

Drawing or filling with a transparent color just results in nothing being drawn. The only operation you have in PlayN to clear pixel data is Canvas.clear(), which clears the entire image. You could also make use of GWT's access to canvas pixel operations and set those pixels manually >> https://stackoverflow.com/a/10492578/1117740

For an easy solution ...

... to clear a rectangle you could just make a copy of your image, then clear you image and draw back the sections you wish to retain.

Obviously you couldn't do that with a circle so easily, though you could use Bresenham's circle drawing algorithm and copy back the segments of each scan line you wish to retain.

Upvotes: 2

Related Questions