• 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

Question about new Color

 
Ranch Hand
Posts: 367
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all..... Maybe this isn't an awt question after all but anyway

In my JPanel paintComponent i have a lot of rectangles drawn. Like g2.draw( new Rectangle( 1 , 1 , 100 , 100 ) for example. Graphics is cast to a Graphics2D object.

I have a question on new Rectangle and new Color that I use alot. Does this take a lot of memory? I first thought that when paintComponent was done the classes where out of scope and gabage collected but I think my program runs slow. Is it ok to do setColor( new Color( 0 , 0 , 0 ) and new Rectangle or is there a better way of doing this?

// Mathias
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
It's OK. If your paint methods draws a lot of Rectangles, you might consider some way to avoid creating the objects repeatedly. But if we're talking about less than a hundred or so, I wouldn't give it a second thought. Same with Color objects -- although the color your example shows is black, and I'd simply use the constant Color.BLACK instead.
 
Mathias Nilsson
Ranch Hand
Posts: 367
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks!

Yes it's not black color just an example. If I have 1000 rectangels should I put them in a vector or Arraylist or what is the best approch?

// Mathias
 
reply
    Bookmark Topic Watch Topic
  • New Topic