Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript 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
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

Applet, How to get the image displayed PLEASE HELP

 
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Friends,

In my applet program, I have a problem in loading the image.

First I wrote

im1=getImage(getDocumentBase(),"C:/Documents and Settings/Mohan.RBCHRIT7/Desktop/HTML/Exercise1/images/Grocery.gif");
sugarPix = new JButton(new ImageIcon(im1));

When I run this, it gave me NullPointerException.(that means the image is not loaded to im1). Do I need to use drawImage in paint()? I searched google.com and found the code below:

Image redball = Toolkit.getDefaultToolkit().getImage ( "C:/Documents and Settings/Mohan.RBCHRIT7/Desktop/HTML/Exercise1/images/Grocery.gif");
sugarPix = new JButton(new ImageIcon(redball));

This code displayed the image without any problem. But, outside of applets you can use the getImage method from the default toolkit instead, like above.

Question:
How can I get the image in Applet, without using getDefaultToolkit()?
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Moving to the Applet forum for expert attention...
 
Ranch Hand
Posts: 2410
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One thing you can try is to use a MediaTracker to make sure the image loads before you try to use it.
 
Shiva Mohan
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks,I am working on that.
 
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check the getImage(URL url, String name) method in the Applet api to see that the name must be relative to the url. When you fix this you run into file permission problems. Another approach is to move the image file into/onto your class path and let the class loader find it with the getResource method which is introduced in How to Use Icons (see "Loading Images Using getResource" and "Loading Images Into Applets"). You discovered the other way: load the image in another class using Toolkit/MediaTracker, ImageIcon or ImageIO loading methods.
 
Shiva Mohan
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Craig,
Excellent.It works well for me.Thanks for the coding.
 
Consider Paul's rocket mass heater.
    Bookmark Topic Watch Topic
  • New Topic