This week's book giveaway is in the Agile and other Processes forum. We're giving away four copies of The Mikado Method and have Ola Ellnestam and Daniel Brolund on-line! See this thread for details.
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.
here is my code as it stands at the moment:
Panel that holds the canvas:
Canvas to display picture:
Thanks in advance
Ulf Dittmer
Marshal
Joined: Mar 22, 2005
Posts: 35241
7
posted
0
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.
My compiler compliance level is set to 1.3 and the currently installed compiler is 1.6.
Also other AWT components are working elsewhere in the program
i.e.:
Matt Shultz
Greenhorn
Joined: Oct 24, 2009
Posts: 9
posted
0
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!
1) call super.paint(g) as the first line of UIMap.paint
2) you need to add the map variable to the panel. At the moment, paintScrn() only paints the map variable to a buffered image. That image is not shown in the user interface at the moment.
You are also aware that bimg will be drawing itself onto itself when you call paintScrn()?
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.
Matt Shultz
Greenhorn
Joined: Oct 24, 2009
Posts: 9
posted
0
That has sorted it , thanks you sooooo much .
I can't begin to explain how grateful I am You are a star!