Win a copy of The Business Blockchain this week in the Cloud forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Getting Images into JApplets

 
colin shuker
Ranch Hand
Posts: 750
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
How would I get an image into my JApplet.
Supposing the MyApplet file and the images folder are in the same directory.
And the images folder contains the file MyImage.jpg

I've looked at the sun tutorial, but it doesn't make sense, and I just
want a simple solution that will work.

Can anyone help.
Thanks
 
Ulf Dittmer
Rancher
Posts: 42969
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
One way to do this is described here. As that article mentions, it may be necessary to use a MediaTracker, the baiscs of which are explained here.
 
colin shuker
Ranch Hand
Posts: 750
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I couldn't get it to work.
It's not clear enough, I tried using the ImageIcon to load image,
as it show at the bottom of this page...
using image icons
but it didn't work.

Can anyone help me load a single image into a JApplet?

Thanks
[ June 10, 2006: Message edited by: colin shuker ]
 
Ulf Dittmer
Rancher
Posts: 42969
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
but it didn't work.

That's not a very helpful description of a problem you're facing. The approach in general does work; what exactly are you doing that doesn't work? Show us the shortest piece of code that does not do what you expect it to do, and tell us what the differences are.
 
colin shuker
Ranch Hand
Posts: 750
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
basically I have a class that extends JApplet, then this class in the same file, underneath:



I'm struggling to figure out how to do this, and it gets annoying
after a while.
If you can help without confusing me, that would be excellent.

Thanks for any help
 
Ulf Dittmer
Rancher
Posts: 42969
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This page suggests using the ImageIcon.paintIcon call.
 
colin shuker
Ranch Hand
Posts: 750
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks very much, I tried the paintIcon idea, and it didn't work,
But then i tried Using the ClassLoader idea, with paintIcon as below:




And this works fine!
However I tried using .getImage() on the ImageIcon, to get the Image.
So that I could then use g.drawImage(...);
But that didn't seem to work, kept getting a NullPointerException.
As you probably guessed I'm writing a chess program, and the reason
I wanted to use g.drawImage(...) was because some of the pieces need
to be resized, which can be done with g.drawImage(...), but I'm not
sure with paintIcon.
Anyway, I just thought, I can just resize them first in photoshop or something, so the paintIcon method should hopefully work fine.

Basically I have a chess application with GUI(based in a JFrame) that I
wanted to applet'ise'. But if I just bung the JFrame into the JApplet,
then it looks like an extra window has been added on top of applet window.
I need the contents of my JFrame to go in the JApplet, so I'm effectively
swapping the JFrame for a JApplet.

Thanks for your help, I will probably run into more problems, so I'll let you know. Thanks
 
Ulf Dittmer
Rancher
Posts: 42969
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Just a wild guess, but I think you may need to use the ImageObserver/MediaTracker mechanism no matter how you load the image, especially once you run the applet over a network. But I guess the ClassLoader approach might somehow sidestep the problem by actually loading the image before returning.
 
colin shuker
Ranch Hand
Posts: 750
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, It works fine using just the ImageIcons and paintIcon,
except that for the first time I loaded page, not all the images
were loaded, not quite sure why. But now it seems to load them all
each time.
Isn't there a SwingUtilities method, invokeAndWait, or invokeLater,
that needs to be used here, I'm not sure?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic