views:

70

answers:

1

Hey, I have designed a component for J2Me, and here is the paint method:

import javax.microedition.lcdui.Graphics;  
import javax.microedition.lcdui.Image;  
class Component {
...
public void paint(Graphics g) {
    if (background != null)
        g.drawImage(image, bounds.getLocation().x, bounds.getLocation().y, 0);
}
...
}

I want to paint this component on a J2Se application, I tried to paint the component onto a J2Me Image and extracted the int[] into an InputStream, and create a new image on the J2Se platform, with this object:

public class ComponentStreamer {
    private Component component;
    private Image j2Me_Image;

    public void setComponent(Component component) {
        this.component = component;
    }

    public InputStream getInputStream() throws IOException {
        if(component==null)
            return null;
        //THIS LINE THROWS THE EXCEPTION
        j2Me_Image=Image.createImage(component.getSize().width, component.getSize().height); 
        component.paint(j2Me_Image.getGraphics());
            return getImageInputStream(j2Me_Image);
    }
}

I've tried the Object, but the commented line throws an exception:

Exception in thread "AWT-EventQueue-0" java.lang.UnsatisfiedLinkError: javax.microedition.lcdui.ImmutableImage.decodeImage([BII)V
    at javax.microedition.lcdui.ImmutableImage.decodeImage(Native Method)
    at javax.microedition.lcdui.ImmutableImage.getImageFromStream(Image.java:999)
    at javax.microedition.lcdui.ImmutableImage.<init>(Image.java:955)
    at javax.microedition.lcdui.Image.createImage(Image.java:554)

How can over come this error?

Thanks,
Adam.

A: 

Well,

That was a very long process of diving into the sources of the J2Me MIDP and CLDC, and the use of a package called Microemulator, here is some code to get anyone else started:

this starts an emulator, which then enables some of the J2Me features.

    private void setUpEmulator() {
    try {
        // overrideJ2MeImagePackageLock();
        Headless app = new Headless();
        DeviceEntry defaultDevice = new DeviceEntry("Default device", null, DeviceImpl.DEFAULT_LOCATION, true, false);
        Field field = app.getClass().getDeclaredField("emulator");
        field.setAccessible(true);
        Common emulator = (Common) field.get(app);
        emulator.initParams(new ArrayList<String>(), defaultDevice, J2SEDevice.class);
    } catch (Exception e) {
        e.printStackTrace();
        System.out.println("Un-handled Exception");
    }
}

Next we have few other nice objects to work with:

    public class J2MeImageLayer extends ScalableLayer {
    private static final long serialVersionUID = -4606125807092612043L;

    public J2MeImageLayer() {
        componentViewer.super();
    }
    @Override
    public void repaint() {
        J2SEMutableImage mutableImage = new J2SEMutableImage(page.getSize().width, page.getSize().height);
        page.paint(mutableImage.getGraphics());
        Graphics g = getImage().getGraphics();
        g.drawImage(mutableImage.getImage(), 0, 0, DCP_Simulator.this);
    }
    public void addComponent(Component component) {
        page.add(component);
    }
    public void setComponent(final Component component) {
        page.removeAllElements();
        final Container componentParent;
        if ((componentParent = component.getParent()) != null)
            component.setRemovedAction(new interfaces.Action() {
                @Override
                public void action() {
                    componentParent.add(component);
                }
            });
        page.add(component);
    }
}

and this is the highlight on how to do that.

Adam.

TacB0sS