Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Why if override paint (and call super.paint) do components of JWindow not show up?  RSS feed

 
Dan Bizman
Ranch Hand
Posts: 387
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've overriden JWindow's paint in my subclass to draw something in the background and then I call super.paint( g ) (I've also tried super.paintComponents( g ) ) and they never show up. My image paints but the components added to the JWindow's content pane do not. What am I doing wrong?

 
Craig Wood
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We are actually breaking in to the painting chain that renders your gui. To leave the system undisturbed we call super.thisPaintMethod(g); inside the method (better not to mix them up, ie, calling super.paintComponent inside paint). If this is all you do then it is as if you never broke in/weren't there. The call to super has the method you are in/overriding do its default rendering (just like it always did before your overrode the method).
So, if you draw an image in paint and then call super.paint(g); the default painting behavior will fill the background of the component and then pass the graphics context on to the child components to use to render themselves who then pass it on.... Your image has been covered up.
Try this: Make a JPanel class, override the paintComponent method, call super.paintComponent, draw your image and then set this component as the content pane in your JWindow.
 
Dan Bizman
Ranch Hand
Posts: 387
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Craig Wood:

So, if you draw an image in paint and then call super.paint(g); the default painting behavior will fill the background of the component and then pass the graphics context on to the child components to use to render themselves who then pass it on.... Your image has been covered up.


No, see that's what I might have expected, but that's NOT what's happening. The image is NOT being covered up -- it's the only thing that shows!

When I paint the image on JWindow and then call super.paint(g) all I see is the image I painted. Nothing else shows up. It makes no sense to me.
 
Dan Bizman
Ranch Hand
Posts: 387
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
OK, discovered a few other weird things:

1. If I use pack() then it shows the components on top of the images - why is this?
2. My window starts as invisible and then slowly shows itself, yet when the components are painted in what happens is this:

a. The component (here a JLabel) shows up instantly at 255 alpha (fully vivisble)
b. The background image slowly reveals itself, COVERING the label
c. When layering's turned off and it becomes fully visible, the label reaapears

Argh! This makes no sense!
 
Craig Wood
Ranch Hand
Posts: 1535
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When layering's turned off
You must be doing something else besides drawing.
Here's a demonstration of your appraoch to drawing a background image in a
JWindow and another approach.

 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!