• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 152
  • Last Modified:

How to display images outside an applet?

Hello, I'm making a little JAVA-program that should be able to show images. Displaying images in applets have never been a problem. But this program isn't an applet.

The problem is in my paint(Graphics g) method when I try to use g.drawImage(Image,int,int,ImageObserver). Because I cannot use 'this' as an ImageObserver that I do when showing an image in an applet. I have tried let the class implement ImageObserver and then use 'this' but it didn't work either.

My program now excists of 2 classes:
GUI (extends JFrame)
GraphicArea (extends Canvas)

And it's in the GraphicArea class that I have the paint method. And I create a GraphicArea object in the GUI class (I want to have a small graphic-field in the frame).

So, how do I use images in non-applet java programs? Or what can I use as an ImageObserver?
0
pettabo
Asked:
pettabo
  • 2
  • 2
1 Solution
 
CEHJCommented:
>>GraphicArea (extends Canvas)

If your GraphicArea extends java.awt.Canvas then it already implements ImageObserver, but you could pass null as your ImageObserver anyway if you want.
0
 
pettaboAuthor Commented:
Thank you, now I understand. But now I have a new question. What is the best way to load images to an Image object?
I used to do like this:
image = getImage(getCodeBase(),"pic.gif");
But this doesn't work now when it isn't an applet.
I have tried to play around with createImage() (from Component) but without any luck.

So, what is the best way to load the images into a non-applet program?
0
 
CEHJCommented:
Use Toolkit.getDefaultToolkit().createImage("x.jpg");
0
 
pettaboAuthor Commented:
Thank you very much, now it's working perfectly.
0
 
CleanupPingCommented:
pettabo:
This old question needs to be finalized -- accept an answer, split points, or get a refund.  For information on your options, please click here-> http:/help/closing.jsp#1 
EXPERTS:
Post your closing recommendations!  No comment means you don't care.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now