Search code examples
codenameone

Add a repeated text watermark to an image with Codename One


Is it possible to add a repeated text as watermark to an Image using the Codename One API?

For example, giving an Image and a text of one or more words, I'd like to create a new Image like this:

enter image description here


Solution

  • Sure just use a mutable image:

    Image watered = Image.create(sourceImg.getWidth(), sourceImg.getHeight());
    Graphics g = watered.getGraphics();
    g.drawImage(sourceImg, 0, 0);
    g.setAlpha(30);
    g.setColor(0xcccccc);
    g.rotate(Math.PI / 2, sourceImg.getWidth() / 2, sourceImg.getHeight() / 2);
    
    
    // here you can loop and do draw String a lot and just move with string width/height
    
    // or you can use multiple drawImage calls and have a ready made watermark
    // this might actually look better and won't require the alpha/rotation code