aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes Swing doubt. not able to display pic on swing frame Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Swing doubt. not able to display pic on swing frame" Watch "Swing doubt. not able to display pic on swing frame" New topic
Author

Swing doubt. not able to display pic on swing frame

RajivAwadhesh kumar
Greenhorn

Joined: May 20, 2011
Posts: 24



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

Joined: Oct 21, 2000
Posts: 4347
    
    2

for starters, paintComponent() should have this as it's first line:
super.paintComponent();


SCJP
Visit my download page
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39884
    
  28
And shouldn’t paintComponent have protected access?
RajivAwadhesh kumar
Greenhorn

Joined: May 20, 2011
Posts: 24
I tried this example from HeadFirst Java..

Am i going wrong anywhere?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39884
    
  28
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

Joined: Oct 27, 2005
Posts: 19762
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
RajivAwadhesh kumar
Greenhorn

Joined: May 20, 2011
Posts: 24



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

Joined: May 20, 2011
Posts: 24
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

Joined: May 03, 2008
Posts: 4658
    
    5

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.


luck, db
There are no new questions, but there may be new answers.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Swing doubt. not able to display pic on swing frame