Monday, August 13, 2007

Repast Space: Windows Dynamics

So we have a window that can float over the Object2DGrid and display the objects inside its area.

But this only makes sense if there's a possibility of moving the window over the grid. We'll do that using events - from the keyboard to start with.

Events comprehend a programming paradigm commonly used in interactive programs. Repast provides ways to use event handlers through the registration of adapters, in this case, keyboard adapters.

As an example, we show how to handle events from the directional keys in order to navigate through the space with a display window:

ModelBuilder.java


// The default move values.
int MOVE_Y = 10, MOVE_X = 10;

...

private void moveViewWindow() {
display.setViewWindow(viewX, viewY, width, height);
DisplaySurface surf = model.getDisplaySurface();
surf.updateDisplayDirect();
}

public void buildDisplays() {

DisplaySurface surf = model.getDisplaySurface();

surf.addKeyListener(new KeyAdapter() {
public void keyPressed(KeyEvent evt) {
switch (evt.getKeyCode()) {
case KeyEvent.VK_DOWN:
if (viewY < space.getY() - MOVE_Y)
viewY += MOVE_Y;
break;

case KeyEvent.VK_UP:
if (viewY > MOVE_Y - 1)
viewY -= MOVE_Y;
break;

case KeyEvent.VK_LEFT:
if (viewX > MOVE_X - 1)
viewX -= MOVE_X;
break;

case KeyEvent.VK_RIGHT:
if (viewX < space.getX() - MOVE_X)
viewX += MOVE_X;
break;

default:

}
moveViewWindow();
}
});

display =
(ShapefileSpaceDisplay)space.getWindowDisplay();
width = display.getWidth();
height = display.getHeight();

surf.addDisplayableProbeable(display, "Window");
surf.display();
}

This will handle the keyboard events, triggered by the directional arrows, thus moving the display window over the space.

Every time the user moves the window, the area covered by it will change and so will the objects to be drawn.

2 comments:

Anonymous said...

Most online based companies have sound experiences in handling international search engine operations.

But, when you aim for developing a site that has relevance and the
information you are writing about.

Have a look at my blog ... search optimization google

Anonymous said...

Brawly's Gym is kept in pitch-black darkness to test the bravery of the Trainers. In 18 months this combination of toys and a game, this handy site will determine whether or not to work from home or work outside the home, your work is never done.

my website - star wars video game