Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Display and display model

Reply
Thread Tools

Display and display model

 
 
Marc Twain
Guest
Posts: n/a
 
      01-13-2004
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
 
Reply With Quote
 
 
 
 
Matt Humphrey
Guest
Posts: n/a
 
      01-13-2004

"Marc Twain" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) m...
> 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 http://www.velocityreviews.com/forums/(E-Mail Removed) http://www.iviz.com/


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
a Sample and hold circuit model Manivannan VHDL 1 03-05-2008 06:55 PM
Newbie question - MVC model and Accessing Database and resultset bean? Evrim Java 1 08-08-2007 09:58 PM
Convert Java Model to Java Model without XML erinbot@gmail.com Java 1 10-06-2006 09:00 PM
How to Find Out Your Motherboard Manufacturer and Model Silverstrand Front Page News 0 10-21-2005 01:24 PM
Review: Evercool Aluminum Fans (AL8025B and AL8025LD2 model) Silverstrand Reviews & How-To's 0 06-20-2005 03:41 AM



Advertisments