wood burning stoves 2.0*
The moose likes Swing / AWT / SWT and the fly likes Why doesn't the image appear with this code. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Why doesn Watch "Why doesn New topic
Author

Why doesn't the image appear with this code.

Adam Miszczak
Greenhorn

Joined: Sep 24, 2012
Posts: 24
Hello!

I decided to to a bit of training with 2D and wanted to draw an image on screen. I followed a tutorial and wrote the code, however the image does not appear.
It does not throw up an error or anything, it just displays the window and the image does not appear.

image class:



screen class:



Thank you!!!
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

Things could have changed since the last time I dealt with this sort of thing, but BMP never used to be a supported format for images in Java.
Adam Miszczak
Greenhorn

Joined: Sep 24, 2012
Posts: 24
Oh my. I feel so stupid :P Yep, it BMP is not supported, thanks for the swift response.
Kemal Sokolovic
Bartender

Joined: Jun 19, 2010
Posts: 825
    
    5

Take a look at ImageInputStream and ImageIO.createImageInputStream(File file). By using those properly, you can display BMP image.


The quieter you are, the more you are able to hear.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38107
    
  22
Sounds like something which would fit better into our GUIs forum. Moving.
Danny Wreczycki
Greenhorn

Joined: Sep 23, 2012
Posts: 23
    
    1
Adam Miszczak wrote:Oh my. I feel so stupid :P Yep, it BMP is not supported, thanks for the swift response.


Instead of paint method overide paintComponent method. Also I'm not pretty sure but i think BMP is supported today, You can use BufferedImage instead of awt.Image if You like and use ImageIO.read for loading image into memory. In my application developed entirely in swing I have no problems with reading BMP images to be honest.

@edit
Ah and always remeber to dispose Graphics or Graphics2D when not used.

Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4523
    
    5

Danny Wreczycki wrote:Ah and always remeber to dispose Graphics or Graphics2D when not used.

Buy only those you have created in your code, via Graphics#create(). Do not call dispose() on the passed-in parameter.


luck, db
There are no new questions, but there may be new answers.
 
 
subject: Why doesn't the image appear with this code.
 
Similar Threads
Output image file and displaying it on screen
Images in downloaded files
How to get image (icon) from a JButton
Rotating an image
Background Image not showing