The Essential JViews Framework > Getting Started with JViews Framework > Step 4 - Manipulating Graphic Objects > Moving Graphic Objects

In Sample4, we have added a new button in the appButtons() method which is used to move the IlvIcon objects in a random way:

 Button moveButton = new Button("move");
 moveButton.setBackground(Color.gray);
 moveButton.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent evt) {
        moveObjects();
      }
    });
    buttons.add(moveButton);
  }

The movement of the objects is implemented in the Sample4.moveObjects() method. This method gets an enumeration of objects contained in layer 1 (our new objects), and, for each of these objects, finds a random object in layer 0 and moves these objects of layer 1 to the center of those objects of layer 0 by calling IlvManager.moveObject.

void moveObjects() { 
  IlvGraphic state=null, obj=null; 
  // get objects in layer 1 
  IlvGraphicEnumeration objects, states; 
  for (objects = manager.getObjects(1); objects.hasMoreElements();) { 
    obj = objects.nextElement();
    // get an random object in layer 0 
    states = manager.getObjects(0); 
    int index = (int)((double)manager.getCardinal(0)*Math.random());
    state = states.nextElement();
    for (int i = 1 ; i < index; i++) 
      state = states.nextElement();
    if (state != null) {
      // move the object.
      IlvRect bbox = state.boundingBox(null);
      manager.moveObject(obj, bbox.x+bbox.width/2,
                              bbox.y+bbox.height/2, true);
    }
  }
}