Java Reference
In-Depth Information
analysed and displayed. One might experience the problem that the drawing is in-
completely displayed. We use a simple example to demonstrate this phenomenon.
We define a class
Drawing
as an abstract representation of the drawing. The
drawing shows three parallel lines. Initially the lines are horizontal. Method
flip-
Lines
will change the drawing to three vertical lines. Calling
flipLines
again will
make the lines horizontal again. To simulate a complex modification of a drawing,
the lines are flipped one after the other with a one second pause after each flip.
Class
UpdatePanel
is defined to display a drawing. To this end it has access to an
instance of class
Drawing
.Wedonot list these classes; they can be downloaded
from the topic's home page.
Class
BadUpdateFrame
is the main class of the application. It has a variable
drawing
of class
Drawing
.Italso has an
UpdatePanel
to display this drawing.
Initially the lines are horizontal. Then the
BadUpdateFrame
calls the
flipLines
method of the drawing, which flips the lines one after the other in roughly three
seconds. This cannot be seen in the display because no event is created to trigger
a repaint. Therefore we add a
repaint
command for the panel after the lines have
been flipped. Then
flipLines
and
repaint
are called again to flip the lines to
horizontal and display this. We only list the program
BadUpdateFrame
; the actual
demonstration is placed in method
badDemo
.
File:
its/GraphicsUpdate/BadUpdateFrame.java
package
its.GraphicsUpdate;
1.
2.
3.
import
javax.swing.JFrame;
4.
import
its.SimpleFrame.SimpleFrame;
5.
6.
7.
public class
BadUpdateFrame
extends
SimpleFrame{
8.
9.
private
UpdatePanel uppane;
10.
private
Drawing drawing;
11.
12.
public
BadUpdateFrame(){
13.
drawing =
new
Drawing();
14.
uppane =
new
UpdatePanel(drawing);
15.
getContentPane().add(uppane);
16.
pack();
17.
}
18.
19.
public void
badDemo(){
uppane.repaint();
//
paint the horizontal lines
20.
drawing.flipLines(); //
flip them to vertical
21.
uppane.repaint();
//
paint the vertical lines
22.
drawing.flipLines(); //
flip them back to horizontal
23.
uppane.repaint ();
//
paint the horizontal lines
24.
Search WWH ::
Custom Search