aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Image not displayed Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Image not displayed" Watch "Image not displayed" New topic
Author

Image not displayed

Helmut Lerch
Ranch Hand

Joined: Feb 11, 2001
Posts: 48
Hi,
what am i missing? Assume the following code for displaying an Image in a frame.
<pre>
import java.awt.*;
public class ImageFrame extends FrameWithClose {
Image img;
public ImageFrame() {
img = Toolkit.getDefaultToolkit().getImage("c:\\img.jpg");
}
public void paint(Graphics g) {
g.drawImage(img, 0, 0, this);
}
public static void main(String [] args) {
ImageFrame f = new ImageFrame();
f.setSize(640, 480);
f.repaint();
f.setVisible(true);
}
}
</pre>
But there is nothing displayed. What I am doing wrong.
Thanks for help
Heli
Nathan Pruett
Bartender

Joined: Oct 18, 2000
Posts: 4121

From what I see two things could be wrong...

  • img.jpg doesn't really exist where you say it does... Java will try to load your image, but if it's not there, a null image reference will be used with no complaints.
  • Your image is small and is being drawn underneath the title bar of the window... you can fix this by either changing the x,y coordinates in your paint method, or by painting the image on a Panel that is then placed in the Frame...


  • -Nate


-Nate
Write once, run anywhere, because there's nowhere to hide! - /. A.C.
Helmut Lerch
Ranch Hand

Joined: Feb 11, 2001
Posts: 48
Thanks for answering.
Originally posted by Nathan Pruett:
From what I see two things could be wrong...

  • img.jpg doesn't really exist where you say it does... Java will try to load your image, but if it's not there, a null image reference will be used with no complaints.


  • -Nate

I thought there will be sure some Exception if this happens.
Again thanks
Heli
 
Consider Paul's rocket mass heater.
 
subject: Image not displayed