Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Workaround for applying a mask to an image in Codename One

Tags:

codenameone

I have a function that accepts an image and rounds it, like so:

    public static Image roundImage(Image img) {
    int width = img.getWidth();
    Image roundMask = Image.createImage(img.getWidth(), img.getHeight(), 0xff000000);
    Graphics gr = roundMask.getGraphics();
    gr.setColor(0xffffff);
    gr.fillArc(0, 0, width, width, 0, 360);
    Object mask = roundMask.createMask();
    img = img.applyMask(mask);
    return img;
}

This works great for images, but if I pass FontImage to it, the function throws an exception: java.lang.RuntimeException: Unsupported Operation. How can I check whether masking is a supported operation? I want to avoid changing the application logic.

like image 892
Hristo Vrigazov Avatar asked Dec 07 '25 08:12

Hristo Vrigazov


1 Answers

FontImage is a requiresDrawImage which is unique. You can convert it to a regular image or even to an encoded image using toImage() or toEncodedImage() respectively.

For this use case a regular image will be better/faster so toImage() should work best.

like image 122
Shai Almog Avatar answered Dec 12 '25 10:12

Shai Almog



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!