代码之家  ›  专栏  ›  技术社区  ›  TacB0sS

在J2SE应用程序上创建javax.microedition.lcdui.image

  •  0
  • TacB0sS  · 技术社区  · 15 年前

    我为J2ME设计了一个组件,下面是绘制方法:

    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映像上,并将int[]提取到输入流中,然后在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);
        }
    }
    

    我尝试过这个对象,但是注释行抛出了一个异常:

    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 回复  |  直到 15 年前
        1
  •  0
  •   TacB0sS    15 年前

    好,

    这是一个非常长的过程,深入到J2ME MIDP和CLDC的源代码中,并使用一个名为micromulator的包,下面是一些代码来让其他人开始:

    这将启动一个模拟器,然后启用一些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);
        }
    }
    

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

    亚当。

    推荐文章