aspose file tools*
The moose likes Swing / AWT / SWT and the fly likes image blanks out... why? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Java 8 in Action this week in the Java 8 forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "image blanks out... why?" Watch "image blanks out... why?" New topic
Author

image blanks out... why?

M George
Greenhorn

Joined: May 29, 2007
Posts: 6
hi all... I am trying to understand how java graphics works.
the problem is NOT showing an image... but WHY it must extend a class!
the following compiles, and displays, then blanks out... why?

Brian Cole
Author
Ranch Hand

Joined: Sep 20, 2005
Posts: 862
Originally posted by M George:
hi all... I am trying to understand how java graphics works.
the problem is NOT showing an image... but WHY it must extend a class!
the following compiles, and displays, then blanks out... why?



It's generally not a good idea to call jFrame.getGraphics(). It doesn't help you much anyway because, though it may draw your image once, your image will not be redrawn when the app needs to be refreshed.

The way it's supposed to work is that the paint() method will automatically get called whenever the app needs to be refreshed. In fact, your frame's existing paint() method is probably painting a blank background, which would clobber your image even had it managed to get drawn that one time.

Here's one way to fix your code so it works:


And here's an easier way to do the same thing:


bitguru blog
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
this is your problem
Graphics graphics = jFrame.getGraphics() ;

this line draws it once
graphics.drawImage( buffImg , 0, 0, dimi.width , dimi.height , jFrame ) ;

then, if a repaint, it's gone.

you're trying to set a background image?

if so, much better to use a JPanel and override its paintComponent to do your drawing.
you can then set the panel as the frame's contentPane.

go back to the swing forum main page, just above the topics list is an FAQ link.
in the FAQ is an example for a background image.
M George
Greenhorn

Joined: May 29, 2007
Posts: 6
All, Thanks for the quick replies!

Brian:
I tried both ways, and the first one seems to paint inconsistently when resizing. This does not seem to happen when paint is overridden in a class extending Component. Do you know why this happens?

Michael:
My apologies for the forum duplication,
I did not realize how it may impact others.
Frankly, I did not expect such fast responses, if any!
I sincerely thought I had to duplicate the questions to get an answer.

For your response: Thank you for the specific direction.
The FAQ example you mentioned essentially matches solution #1 above.
Why does the paint always work when a Component is extended?
Or to phrase it differently, why does the inner class fail to paint consistently?
[ February 29, 2008: Message edited by: M George ]
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
there's no difference between these two



perhaps the problem you now see, is in paint() vs paintComponent()
M George
Greenhorn

Joined: May 29, 2007
Posts: 6
Thanks for your reply!

When I apply paintComponent to the inner class in Brians example, my image does not display at all. It does compile, and it does not throw errors when running. It does not print System.out debug lines. I am guessing this is because JFrame is attempting to call paint but not paintComponent?

Are you also getting the behavior that paint behaves differently (when resizing) when implemented through an inner class? If not, are you adding something different to the code?
Michael Dunn
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
the your JFrame compiled using paintComponent, you must not have included
the line super.paintComponent(g), otherwise you would get a compile error.

paintComponent() in a JFrame is just another method, not called by anything.

paintComponent() is a method of JComponent, which in not in the ancestry of JFrame.
JFrame's ancestry is awt.Component (which uses paint()).

here's a simple demo of both ways mentioned earlier for paintComponent
run it and see if there's any difference

 
It is sorta covered in the JavaRanch Style Guide.
 
subject: image blanks out... why?
 
Similar Threads
Reading tif files on x64 system
image thumbnail question
JComponent not opaque
How to add image inside jPanel ??
Background images for the Frame