Win a copy of Re-engineering Legacy Software this week in the Refactoring forum
or Docker in Action in the Cloud/Virtualization forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Swing doubt. not able to display pic on swing frame

 
RajivAwadhesh kumar
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



when i run this program , the frame is displayed but without image.
Please suggest if i am doing some mistake.

Thanks in advance
 
Randall Twede
Ranch Hand
Posts: 4363
2
Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
for starters, paintComponent() should have this as it's first line:
super.paintComponent();
 
Campbell Ritchie
Sheriff
Posts: 48453
56
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And shouldn’t paintComponent have protected access?
 
RajivAwadhesh kumar
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried this example from HeadFirst Java..

Am i going wrong anywhere?
 
Campbell Ritchie
Sheriff
Posts: 48453
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Which page in Head First? I haven’t got my copy to hand now. Why have you got a logger there? Is the exercise about logging or about drawing images?
Try System.out.println(image); as line 20, and System.out.println(b); as line 22. If you get null for the first you have got problems, maybe because the image file isn’t where you think it is. Check the contents of your folder. You might have to move the .png file to a different directory. What happens if you “flatten” the file structure by deleting the package name and putting everything into one folder?
If you get false, look at this. I am not sure what to do about that.
 
Rob Spoor
Sheriff
Pie
Posts: 20495
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You shouldn't load the image every time you repaint the panel. That may cause a lot of I/O. It's better to store the Image as an instance field that you set in the constructor. Also, make sure that the image exists.
 
RajivAwadhesh kumar
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator



I hope this is the correct way of loading the image using the image as an instance field
and setting it in the constructor.
I have the image in the desktop as well as in my project directory in eclipse.
 
RajivAwadhesh kumar
Greenhorn
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This example is there on page 365 of headfirst java. Its a swing chapter and example is about how to create a gui and display your own pic on it
I just added a few loggers for my convenience to check where
the problem is .


I am not getting the image object as null but the method drawImage returns false.
I put the pic in the same folder as the java soyrce file , still the problem persists.

 
Darryl Burke
Bartender
Posts: 5125
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
RajivAwadhesh kumar wrote:I hope this is the correct way of loading the image using the image as an instance field
and setting it in the constructor.


Generally correct, but you don't need to construct an ImageIcon just to obtain its image.

Non-animated images are best loaded using ImageIO#read(...) which blocks until the image is fully loaded and returns a BufferedImage.

Animated images can be loaded by Toolkit#getImage(...) (which is what ImageIcon uses). As this is a non-blocking method, you may need to make use of a MediaTracker.

... the method drawImage returns false.

Evidently because the image hasn't yet been fully loaded. Or maybe the path is wrong, or you're running the code from an IDE without rebuilding the project so the image file hasn't been copied form teh source tree to the build tree.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic