• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Knute Snortum
  • Bear Bibeault
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Piet Souris
  • Ganesh Patekar
Bartenders:
  • Frits Walraven
  • Carey Brown
  • Tim Holloway

Specifying when graphics must be painted (and updated)

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have a program that should take a random 2D array of Colors, display the random grid, then sort it and display the sorted grid. I don't have a lot of experience with swing GUI. The problem I am facing is that even though it seems to me that I display the grid before sorting it (and then sort it), the painted grid is always the sorted one. I don't understand when the paintCompnent() method is called.



My goal, as I said is:
1) Display random array of colors.
2) Sort the colors.
3) Then display the newly sorted array of colors.

How do I do this? The problem seems to be when the paint methods are called and also how to repaint a panel.
 
Rancher
Posts: 3051
22
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I don't understand when the paintCompnent() method is called.



Painting requests are made through the RepaintManager. The RepaintManager gathers requests, consolidates them and then adds the painting request to the end of the Event Dispatch Thread.

So, I would guess in this case that the sorting of the Array is being done before the actual painting of all the components.

You should not have a painting method that is referring to external data. The data to be painted should be part of the MyPanel class. So you need create a second Array and copy the elements to the array and then sort the Array. The Arrays should be used a parameters to the MyPanel class when you create the panel so the painting method can paint the proper data.
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!