Search code examples
playn

How to fill a layer with a radial gradient in playn (1.3)?


I am trying to get started with playn 1.3 (should I use 1.4?) but I am failing early on the point on how to simply fill some sort of layer with a radial gradient. graphics().createRadialGradient tells me that it creates a Pattern, but it just creates a Gradient that I cannot use on setFillPattern. Using createCanvasLayer has been deprecated, so I am trying around with ImageLayer and SurfaceLayer but I cannot find a way to create an actual Pattern from the Gradient or something similar that finally fills a layer with my Gradient. Of course I must just be overlooking something, so I'd be happy to have a hint here because playn really looks promising to me :)

Cheers


Solution

  • Tested in PlayN 1.4:

    CanvasImage canvasImage = graphics().createImage(100, 100);
    Canvas canvas = canvasImage.canvas();
    Gradient radialGradient = graphics().createRadialGradient(50, 50, 50, 
            new int[]{Color.rgb(255, 0, 0), Color.argb(0, 255, 0, 0)}, 
            new float[]{0, 1});
    canvas.setFillGradient(radialGradient);
    canvas.fillRect(0, 0, 100, 100);
    ImageLayer imageLayer = graphics().createImageLayer();
    imageLayer.setImage(canvasImage);
    imageLayer.setDepth(1);
    graphics().rootLayer().addAt(imageLayer, graphics().width() / 2, graphics().height() / 2);