在J2Se Application上创建javax.microedition.lcdui.Image

时间:2010-07-28 17:51:01

标签: image java-me paint java

我为J2Me设计了一个组件,这是paint方法:

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);
}
...
}

我想在J2Se应用程序上绘制这个组件,我试图将组件绘制到J2Me Image上并将int []提取到InputStream中,并在J2Se平台上创建一个新图像,使用此对象:

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);
    }
}

我已经尝试过Object,但是注释行抛出异常:

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)

怎么会出现这个错误?

谢谢,
 亚当。

1 个答案:

答案 0 :(得分:0)

那么,

这是一个漫长的过程,潜入J2Me MIDP和CLDC的源代码,并使用一个名为Microemulator的软件包,这里有一些代码可以让其他人开始:

这将启动一个模拟器,然后启用一些J2Me功能。

    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");
    }
}

接下来,我们可以使用其他一些很好的对象:

    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);
    }
}

这是如何做到这一点的重点。

亚当。