Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Image not displayed

 
Helmut Lerch
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4121
IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Helmut Lerch
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic