<p>What would be the best method for getting a custom element (that is using J2ME native Graphics) painted on LWUIT elements?</p>

<p>The custom element is an implementation from mapping library, that paints it's content (for example Google map) to Graphics object. How would it be possible to paint the result directly on LWUIT elements (at the moment I am trying to paint it on a Component). </p>

<p>Is the only way to write a wrapper in LWUIT package, that would expose the internal implementation of it?</p>

<p><strong>Edit:</strong></p>

<p><strong><em>John:</em></strong> your solution looks like a lot of engineering :P What I ended up using is following wrapper:</p>

<pre><code>package com.sun.lwuit;

public class ImageWrapper {
  private final Image image;

  public ImageWrapper(final Image lwuitBuffer) {
    this.image = lwuitBuffer;
  }

  public javax.microedition.lcdui.Graphics getGraphics() {
    return image.getGraphics().getGraphics();
  }
}
</code></pre>

<p>Now I can get the 'native' Graphics element from LWUIT. Paint on it - effectively painting on LWUIT image. And I can use the image to paint on a component.</p>

<p>And it still looks like a hack :)</p>

<p>But the real problem is 50kB of code overhead, even after obfuscation. But this is a issue for another post :)</p>

<p>/JaanusSiim</p>

[{'answer_id': 48945, 'author': 'John Meagher', 'author_id': 3535, 'author_profile': 'https://Stackoverflow.com/users/3535', 'pm_score': 0, 'selected': False, 'text': '<p>Based on the javadoc for LWUIT and J2ME and guessing that the custom J2ME class is a <a href="http://java.sun.com/javame/reference/apis/jsr118/javax/microedition/lcdui/Canvas.html" rel="nofollow noreferrer">Canvas</a> it looks like you would have to:</p>\n\n<ul>\n<li>Subclass <a href="https://lwuit.dev.java.net/javadocs/com/sun/lwuit/Component.html" rel="nofollow noreferrer">LWUIT\'s Component class</a> wrapping the custom J2ME component</li>\n<li>Override the paint() method of the LWUIT Component</li>\n<li>Subclass the <a href="http://java.sun.com/javame/reference/apis/jsr118/javax/microedition/lcdui/Graphics.html" rel="nofollow noreferrer">J2ME Graphics class</a> wrapping the LWUIT Graphics class and pass all the method calls through</li>\n<li>Pass in the wrapped J2ME Graphics implementation to the custom J2ME component\'s paint method</li>\n</ul>\n\n<p>That third step is an ugly one.  Check on the <a href="https://lwuit.dev.java.net/servlets/SummarizeList?listName=users" rel="nofollow noreferrer">LWUIT mailing list</a> to see if anyone has dome this before.  From the published APIs I don\'t see another way to do it.   </p>\n\n<p>Edit: The hack added in the question looks better than my hack for an Image.  What I have <em>may</em> be better for a general case, but I don\'t know either LWUIT or J2ME well enough to really say that.  </p>\n'}, {'answer_id': 86279, 'author': 'Honza', 'author_id': 8621, 'author_profile': 'https://Stackoverflow.com/users/8621', 'pm_score': 2, 'selected': False, 'text': '<p>I do not think any hacking is necessary. You can subclass the LWTUI Component class and then you can pain whatever you want on to the graphic context of the component. You do not get the native lcdui.Graphics object but an object with a same interface that is easy to use.</p>\n\n<p>If you really need to pass a lcdui.Graphics to some underlying library to display its output then I would suggest this:</p>\n\n<p>Somewhere in your component code (do only when the component contents really need to be changed):</p>\n\n<pre><code>private Image buffer = null; // keep this\n\nint[] bufferArray = new int[desiredWidth * desiredHeight];\njavax.microedition.lcdui.Image bufferImage = \n    Image.createEmptyImage(desiredWidth, desiredHeight);\nthirPartyComponent.paint(bufferImage.getGraphics());\nbufferImage.getRGB(bufferArray,0,1,0,0,desiredWidth, desiredHeight);\nbufferImage = null; //no longer needed\nbuffer = Image.createImage(bufferArray, desiredWidth, desiredHeight);\n</code></pre>\n\n<p>In the component paint(g) method:</p>\n\n<pre><code>g.drawImage(0,0, buffer);\n</code></pre>\n\n<p>By doing the hack you did you are losing portablity and also sice you are exposing implementation private object you might also break other things.</p>\n\n<p>Hope this helps.</p>\n'}]
['https://Stackoverflow.com/questions/23372', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/706/']