• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Ranch Hand
Posts: 4716
9
Scala Java
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Rancher
Posts: 3324
32
  • Likes 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Don't override the update() method. That is done in AWT painting, NOT Swing painting.
 
Dave Elwood
Ranch Hand
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Rancher
Posts: 3324
32
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 4632
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 84
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That's interesting. I like the idea of the anon inner class for the timer.
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic