File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Java in General and the fly likes Draw bitmap in Canvas Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Draw bitmap in Canvas" Watch "Draw bitmap in Canvas" New topic
Author

Draw bitmap in Canvas

Jonathas Carrijo
Greenhorn

Joined: Jul 25, 2002
Posts: 25
I have to show a bmp image in a Canvas, inside a frame. I tried to use Toolkit's getImage(String <filename> , but it didn't work... Actually I copied the code from an old post here at this forum. Here it is:

It compiles and run fine, but the image simply is not painted, and it actually exists (i get a true printed in the console). Why doesn't it work? How can it be done (without using an applet) ?
Thanks in advance...
JUBA d-_-b
Amanda Weber
Greenhorn

Joined: Nov 20, 2002
Posts: 28
The drawImage method of the graphics class returns a boolean if the image displayed. Try using it in a while loop:
while (!g.drawImage(......))
repaint;
Jonathas Carrijo
Greenhorn

Joined: Jul 25, 2002
Posts: 25
I changed the paint() method as follows:

And all I got is hundreds of thousands of:
drawImage: false
drawImage: false
drawImage: false
drawImage: false
drawImage: false
Why??? What can be done? I really need this working! Please help me...
Thanks,
Juba d-_-b
Amanda Weber
Greenhorn

Joined: Nov 20, 2002
Posts: 28
You are trying to draw the image before it is fully loaded. Use the MediaTracker to get when the image will be loaded, then draw it. Sun's documentation on java.awt.MediaTracker explains it very well.
Jonathas Carrijo
Greenhorn

Joined: Jul 25, 2002
Posts: 25
I added this to my code. Everything compiled and ran fine, but still didn't show my image...


I got as output:
Error loading: true
which means that there was an error loading the image... I tried to use the getImage() method which takes an URL as parameter, like this:

instead of the tk.getImage("c:\\juba\\used.bmp");
Didn`t work yet... I think I'm gonna migrate to Swing... or even to C# (hehehe)
Am I attacking it the right way? I mean, is the problem actually in the path specification?
Anybody help, I need it deployed till tomorrow...
Thanks much!
Juba d-_-b
[ November 21, 2002: Message edited by: Jonathas Carrijo ]
Jonathas Carrijo
Greenhorn

Joined: Jul 25, 2002
Posts: 25
Please guys... I really need help on this!
Haven't anybody ever had a happy experience loading images?? Please share with me
Thanks a lot
Cindy Glass
"The Hood"
Sheriff

Joined: Sep 29, 2000
Posts: 8521
Try this. Note that Balloon.jpg is in the same directory with ImageFrame.class.


"JavaRanch, where the deer and the Certified play" - David O'Meara
O Nulano
Greenhorn

Joined: Oct 27, 2012
Posts: 2

I had the same problem (images not loading) and I found this: Official Java Tutorial on loading Images
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39869
    
  28
Welcome to the Ranch

Did you notice the thread was labelled urgent? I hope ten years wasn’t too long to wait That tutorial might not even have been available in 2002!
O Nulano
Greenhorn

Joined: Oct 27, 2012
Posts: 2

Campbell Ritchie wrote:Welcome to the Ranch

Did you notice the thread was labelled urgent? I hope ten years wasn’t too long to wait That tutorial might not even have been available in 2002!


It was urgent for me too. I couln't find anything of value except this, and I thought it wold be valuable information for other people who find this forum as well.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39869
    
  28
Yes, it is valuable information. Thank you, It is just that we sometimes wonder whether people realise they are reopening such old threads.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19762
    
  20

Campbell Ritchie wrote:Did you notice the thread was labelled urgent?

Emphasis on "was"


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39869
    
  28
No, emphasis on“
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Draw bitmap in Canvas