• Post Reply Bookmark Topic Watch Topic
  • New Topic

Specifying when graphics must be painted (and updated)

 
Francois Meyer
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.
 
Rob Camick
Ranch Hand
Posts: 2699
10
  • 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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!