Java Reference
In-Depth Information
Main thread
Event thread
Drawing
completed
Call to repaint()
repaint added to
event queue
{
Drawing is
modified
repaint is taken out
of the event queue
}
The panel is repainted
using the modified data
Data used
to repaint
Figure 20.2 The main thread, on the left, modifies the drawing while the repaint event
proceeds to the head of the event queue. When the repainting is finally performed, the
modified drawing is shown
the flipLine instructions run in the main thread, while the repaint instruction
is performed in the event thread. The main thread just adds repaint to the event
queue to indicate that the GUI should be repainted at some time.
In this case, the order of the code lines in the listing does no longer reflect the
order of their execution. It might take some time for the event thread to process the
request to repaint. By that time, the main thread has already flipped the first line.
The display then shows part of the old and part of the new picture. The temporal
order in which things happen is listed below and is also shown in Figure 20.2:
1. All lines are horizontal.
2. The repaint command is added to the event queue.
3. The first line is flipped to vertical.
4. The repaint command reaches the head of the queue and the panel is re-
painted to show the current drawing with the first line already vertical.
5. The other two lines are flipped but the display is not updated.
20.4.2
A solution by buffering
One way of solving the problem is to use a buffer for the drawing. This means that
we have two abstract drawings, one of which is displayed and not changed and the
other one that is not displayed and modified. When the modifications of the second
one are completed, the drawings change their role. To this end the reference to the
drawing is changed in the panel. Class UpdatePanel provides a method for this
purpose ( setDrawing ). Then the new drawing is displayed by calling the repaint
method of the panel. The content of the currently displayed drawing is copied to
Search WWH ::




Custom Search