File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Swing / AWT / SWT and the fly likes Can JPanel's paint() use different Graphics2D objects? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Swing / AWT / SWT
Bookmark "Can JPanel Watch "Can JPanel New topic
Author

Can JPanel's paint() use different Graphics2D objects?

Dave Elwood
Ranch Hand

Joined: Dec 27, 2002
Posts: 84
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
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
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: 84
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: 4339
    
    2

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.

SCJP
Visit my download page
Rob Camick
Ranch Hand

Joined: Jun 13, 2009
Posts: 2090
    
    7
Don't override the update() method. That is done in AWT painting, NOT Swing painting.
Dave Elwood
Ranch Hand

Joined: Dec 27, 2002
Posts: 84
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: 2090
    
    7
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
Ranch Hand

Joined: Jun 09, 2003
Posts: 4632
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: 84
That's interesting. I like the idea of the anon inner class for the timer.
Darryl Burke
Bartender

Joined: May 03, 2008
Posts: 4523
    
    5

The anonymous inner class is an ActionListener passed to the Timer's constructor.

The Timer is just a javax.swing.Timer --not an anonymous inner class.


luck, db
There are no new questions, but there may be new answers.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Can JPanel's paint() use different Graphics2D objects?
 
Similar Threads
using a bean to put slider on panel
MouseDragg
A Paint Application with Combo Boxes and Buttons
percentage based background scheme for a JPanel
Wanted: Java2D example of zooming and panning with scroll bars