Display and display model

Discussion in 'Java' started by Marc Twain, Jan 13, 2004.

  1. Marc Twain

    Marc Twain Guest

    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
    Marc Twain, Jan 13, 2004
    #1
    1. Advertising

  2. "Marc Twain" <> wrote in message
    news:...
    > 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.iviz.com/
    Matt Humphrey, Jan 13, 2004
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Manivannan

    a Sample and hold circuit model

    Manivannan, Sep 27, 2004, in forum: VHDL
    Replies:
    1
    Views:
    3,243
    cdbular
    Mar 5, 2008
  2. Replies:
    7
    Views:
    5,737
    Weng Tianxiang
    Mar 10, 2006
  3. Replies:
    0
    Views:
    1,126
  4. Replies:
    1
    Views:
    471
    Andreas Wollschlaeger
    Oct 6, 2006
  5. Evrim
    Replies:
    1
    Views:
    431
Loading...

Share This Page