Search code examples
java-melwuitlwuit-form

PNG Image is badly stained when set as LWUIT Form's background image


I want to make a png Image as the background image of a LWUIT Form. The problem is that the image is altered : there are stains in the image after setting it as the Form's background image. Here are codes :

public class DetailPhotoClient extends Form implements ActionListener {

    private Command options, delete, back, annuler, ok;
    private GaleriePhotos backForm;
    private FileConnection fcFile;
    private Image sourceImage, fullImage;
    private InputStream is;
    private PopupMenu popup;

    public DetailPhotoClient(GaleriePhotos prevForm, String absolutePathphotoName)
    {
        super();
        back = new Command("Retour");
        options = new Command("Options");
        this.addCommand(back);
        this.addCommand(options);
        this.addCommandListener(this);

        delete = new Command("Supprimer");
        annuler = new Command("Annuler");
        ok = new Command("Ok");

        backForm = prevForm;

        try {
            fcFile = (FileConnection) Connector.open(absolutePathphotoName, Connector.READ);
            is = fcFile.openInputStream();
            sourceImage = Image.createImage(is);
            fullImage = createThumbnail(sourceImage);
            setBgImage(fullImage);
            is.close();
            fcFile.close();
        } catch (IOException ex) {
            handleException();
        } catch (OutOfMemoryError oom) {
            handleOOM();
        }
    }
    private Image createThumbnail(Image image) {
        Image thumb = image.scaled(this.getPreferredW(), this.getPreferredH());
        return thumb;
    }
    ...
}

I noticed that when I open the photo manually , that is from the phone memory's photo folder , then the photo is not altered !

So how to make the image not altered when setting it as the Form's background image ?


Solution

  • LWUIT uses scaling for background images by default. It will always scale the images unless you explicitly ask the style for different behavior e.g. tiling, aligning etc. try:

    myForm.getStyle().setBackgroundType(Style.BACKGROUND_IMAGE_ALIGNED_CENTER);