I hope someone may help me and clear up my confusion here as I seem to be going round in circles. I am writing an application using Java ME, so would I be right in assuming I can't use any Swing components? I have been trying to take an image saved on my computer and get it to display in a canvas on screen using AWT and I just can't get it to work. The errors I have been getting are ranging from null pointer exceptions when trying to create a graphics object (something I don't fully understand how to do) to the program running in the IDE (which is Eclipse 3.5.2) but just displaying a blank canvas.
You're right that no version of JME supports Swing. But not all flavors of JME support AWT, either. For example, CLDC and MIDP don't - you need at least the CDC Personal (Basis) Profile for AWT to be available.
I have changed the code to the following and now the frame displays, the g.toString() method displays a Graphics "string" in the console but the picture still doesn't display on the screen!
I think I am going to cry!
Matt Shultz wrote:I wasn't aware bimg was drawing itself on itself, which line does this?
The bimg of the above code block is the same as the bimg of the below code block. You create a graphics object for this image, then pass this to map.paint. That paint draws bimg onto that graphics object - which is the graphics object of bimg itself.
I have tried making your suggested changes but it still doesn't work just a blank screen as shown in the screenshot.
A bit of debugging showed me that the preferred size of the Canvas is 0x0 pixels. It is added but it has no size.
Change your constructor to this:
This extra line sets the preferred size to be that of the image to display. This preferred size is then used by the Panel's layout manager (a FlowLayout) to determine its preferred size.