Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Draw bitmap in Canvas

 
Jonathas Carrijo
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 8521
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Try this. Note that Balloon.jpg is in the same directory with ImageFrame.class.
 
O Nulano
Greenhorn
Posts: 2
Eclipse IDE Java Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I had the same problem (images not loading) and I found this: Official Java Tutorial on loading Images
 
Campbell Ritchie
Sheriff
Pie
Posts: 50251
79
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2
Eclipse IDE Java Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 50251
79
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 20668
65
Chrome Eclipse IDE Java Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Did you notice the thread was labelled urgent?

Emphasis on "was"
 
Campbell Ritchie
Sheriff
Pie
Posts: 50251
79
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, emphasis on“
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic