Win a copy of Real-World Software Development: A Project-Driven Guide to Fundamentals in Java this week in the Agile and Other Processes forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Liutauras Vilda
  • Knute Snortum
  • Bear Bibeault
master stewards:
  • Devaka Cooray
  • Jeanne Boyarsky
  • Junilu Lacar
garden masters:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • salvin francis
gardeners:
  • Tim Holloway
  • Piet Souris
  • Frits Walraven

Draw bitmap in Canvas

 
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
 
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
 
"The Hood"
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.
 
Greenhorn
Posts: 2
Eclipse IDE Opera Java
  • 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
 
Marshal
Posts: 68014
258
  • 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 Opera Java
  • 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
Marshal
Posts: 68014
258
  • 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.
 
Sheriff
Posts: 21865
105
Eclipse IDE Spring VI Editor Chrome Java Ubuntu 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
Marshal
Posts: 68014
258
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, emphasis on“
 
Joel Salatin has signs on his property that say "Trespassers will be Impressed!" Impressive tiny ad:
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!