aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Displaying a png on screen using AWT Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Displaying a png on screen using AWT" Watch "Displaying a png on screen using AWT" New topic
Author

Displaying a png on screen using AWT

Matt Shultz
Greenhorn

Joined: Oct 24, 2009
Posts: 9
Hi

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: 42289
    
  64
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.


Ping & DNS - my free Android networking tools app
Matt Shultz
Greenhorn

Joined: Oct 24, 2009
Posts: 9
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
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!





Can anyone help???
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19722
    
  20

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()?


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Matt Shultz
Greenhorn

Joined: Oct 24, 2009
Posts: 9
Hi

Thanks for your reply I am obviously very confused here as I wasn't aware bimg was drawing itself on itself, which line does this?

I have tried making your suggested changes but it still doesn't work just a blank screen as shown in the screenshot.



[Thumbnail for Screen shot 2010-04-19 at 19.47.09.png]

Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19722
    
  20

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.

How have you added the map to the Panel?
Matt Shultz
Greenhorn

Joined: Oct 24, 2009
Posts: 9
I tried this,
new code listing:



Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19722
    
  20

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
That has sorted it , thanks you sooooo much .
I can't begin to explain how grateful I am
You are a star!
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19722
    
  20

You're welcome.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Displaying a png on screen using AWT