How to display images outside an applet?

Posted on 2003-02-23
Medium Priority
Last Modified: 2010-03-31
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?
Question by:pettabo
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
  • 2
LVL 86

Expert Comment

ID: 8004692
>>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.

Author Comment

ID: 8004961
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?
LVL 86

Accepted Solution

CEHJ earned 200 total points
ID: 8005023
Use Toolkit.getDefaultToolkit().createImage("x.jpg");

Author Comment

ID: 8005089
Thank you very much, now it's working perfectly.

Expert Comment

ID: 9059129
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 
Post your closing recommendations!  No comment means you don't care.

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This video teaches viewers about errors in exception handling.
Suggested Courses
Course of the Month15 days, 8 hours left to enroll

743 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question