Can JPanel's paint() use different Graphics2D objects?
Dave Elwood
Ranch Hand
Joined: Dec 27, 2002
Posts: 64
posted
0
I've got NormPanel here :
after I have called DblBuffClass.init() then DblBuffClass is ready to show it's own shapes
is it possible at all for paint() to optionally go over to another class and
have it populate the Graphics2D object with different things and then
have paint() add a few more things?
This thing now doesn't work
Michael Dunn
Rancher
Joined: Jun 09, 2003
Posts: 4041
posted
0
for Swing components you should be using paintComponent(..) instead of paint(..)
not sure exactly what you're trying to do, but doing stuff inside paintComponent()
is generally not good, due to the number of times the method is called, unless that's what you want.
to see how often it is called, run this, then drag the frame wider or smaller and watch the counter go.
Dave Elwood
Ranch Hand
Joined: Dec 27, 2002
Posts: 64
posted
0
not sure exactly what you're trying to do
I'm trying to invoke double-buffering on a part-time basis on a JPanel.
I've succeeded in invoking additional painting on a part-time basis by doing this in my NormPanel :
if blPaintBoom is false then I haven't invoked my extra little animation
otherwise I invoke blastPainter.paintBoom(g), where I add some growing concentric circles (an explosion) with a loop
and repaint() routine and when I'm done I put blPaintBoom back to false
using paint() instead of paintComponent() along with the use of an update() override was suggested in a description of double buffering.
doing this hasn't caused any bad things to happen.
I'd like this snippet of code in paintBoom to employ double-buffering in order to
smooth out my animation. However getting it back into NormPanel.paintComponent() is
difficult since I'm not sure of the true nature of Graphics2D, Graphics and Image objects.
I'm afraid the code you showed me doesn't employ double-buffering at all.
Randall Twede
Ranch Hand
Joined: Oct 21, 2000
Posts: 3905
posted
1
for what it's worth, and without reading your code.
you can have more than one Graphics in your paintComponent()
you can cast the one the method was given to a Graphics2D or create a new Graphics2D
as for double buffering, i am no expert, but i think swing does some of that now for you.
I never took notes in college. That's how I got a 4.0 the first 2 years, and a 3.5 the second two years.
Rob Camick
Ranch Hand
Joined: Jun 13, 2009
Posts: 1652
posted
2
Don't override the update() method. That is done in AWT painting, NOT Swing painting.
Dave Elwood
Ranch Hand
Joined: Dec 27, 2002
Posts: 64
posted
0
Thanks Rob Camick, Trying to imagine the difference between AWT and Swing is hard for me. I'm slow.
To Randall : >>you can have more than one Graphics in your paintComponent()
Do you mean I can do this :
and both will show up?
I'll try that out. Thanks for the tip.
Rob Camick
Ranch Hand
Joined: Jun 13, 2009
Posts: 1652
posted
0
I'm not sure what you are trying to do. Swing is double buffered by default so there is no need to worry about double buffering.
You can't just create a Graphics objects and do painting with it. Generally you just use the Graphics objects that is passed to a paintCompnent() method. However if you want to change a lot of the properties of the Graphics object you can use the Graphics.create() method to get a copy of the original Graphics, just make sure you dispose of the new Graphics objects when you are finished with it.
Michael Dunn
Rancher
Joined: Jun 09, 2003
Posts: 4041
posted
0
if you're just trying to create a 'boom' effect, something like this might be all you need to do.
run it, then click the 'boom' button.
Dave Elwood
Ranch Hand
Joined: Dec 27, 2002
Posts: 64
posted
0
That's interesting. I like the idea of the anon inner class for the timer.