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.
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.