Display and display model

M

Marc Twain

I have purchase a Java book that list a short number of 'display
strategies':

1- maintain a model of the display
2- have the paint method draw the display based only on the content of
the model
3- to change the display , update the model and then call the repaint
method

.... unfortunately the book doesn't provide any example for the above
and I'm wondering what it means. Does anyone know?

TIA,

AJ
 
M

Matt Humphrey

Marc Twain said:
I have purchase a Java book that list a short number of 'display
strategies':

1- maintain a model of the display
2- have the paint method draw the display based only on the content of
the model
3- to change the display , update the model and then call the repaint
method

... unfortunately the book doesn't provide any example for the above
and I'm wondering what it means. Does anyone know?

Let's say your application is to drag a circle around the screen whereever
the mouse get pressed. Drawing a circle is easy and you might think you can
just draw the circle whenever the mouse is moved, but that won't work
because you wouldn't want to draw to the screen when drawing isn't needed,
or on top of the wrong window. In Java, the UI thread manages the display
update process and only it knows when and where it is appropriate to draw.
It tells you (via the paint method) when painting needs to happen. This
means that, in a sense, your application will receive requests to draw the
circle "out of the blue." To make this work you need to keep track of where
the circle is. That information is your model of the display (step 1). In
this example it would simply be an x and y location and your paintComponent
uses that (read only) when updating the screen (step 2).

Similarly, when the mouse is moved, you don't do any painting at all.
Rather, you get the new mouse location from the event and use that to update
the x, y location of the circle (step 3). At some point you call "repaint"
which in fact does NOT repaint the screen. Instead, it tells the UI thread
that it should (when it gets the chance) repaint the screen. Since the mouse
listener code is taking place in the UI thread, there is no way to get a
synchronization / race problem with the part of the UI that repaints the
screen (as you might not be the only window component also requesting
repainting and trying to synchronize a real example with many UI components
is the nightmare the Java designers decided to solve.)

So the display strategy is:
1) Figure out what kind of data and data structure you need to draw the
display,
2) Have your paint method apply that data and do the rendering,
3) Have your action listeners (and other UI / external thread updates)
update the model and invoke repaint.

This technique is primarily for applications that do some of their own
rendering, although it is part of the general Model-View-Controller method
for managing the user interface. For existing components (JButton, JLabel,
etc) steps 1 and 2 are done for you so all you have to do is modify the
model data.)

Cheers,
Matt Humphrey (e-mail address removed) http://www.iviz.com/
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,576
Members
45,054
Latest member
LucyCarper

Latest Threads

Top