0

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.

Roman C
  • 49,761
  • 33
  • 66
  • 176
Gábor Bakos
  • 8,982
  • 52
  • 35
  • 52

1 Answers1

2

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.

Community
  • 1
  • 1
Keldon Alleyne
  • 2,103
  • 16
  • 23
  • Thanks, this is a way to go, but I'm afraid it will not help with the GC issues on Android. (Making things transparent again would not require to recreate the whole layer/image and dispose them.) – Gábor Bakos Sep 01 '12 at 14:32