Forums Register Login

Specifying when graphics must be painted (and updated)

+Pie Number of slices to send: Send
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.
1
+Pie Number of slices to send: Send
 

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.
Live a little! The night is young! And we have umbrellas in our drinks! This umbrella has a tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 418 times.
Similar Threads
Lesson: Performing Custom Painting (The Java Tutorials)
Code explination
Layout of Swing program question
How does repaint work in combination with paintComponent (Graphics)?
image blanks out... why?
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 16, 2024 08:56:53.