ILOG
Welcome, Guest | Sign In


Blogs | Forums | Worldwide sites | Contact us

title element1
Product Info
Features
Tutorial
Datasheet
Supported platforms
Technical Forum
Solutions
Customers
News & Events
Press releases
Trial & Purchase
Demos
Evals
Contact info
Tutorial  

2D Graphics

In step 4, we will use a timer to animate a graphic object. The timer will call a defined function at each period, and this function will move a previously created white ellipse. This step will introduce important concepts, such as the user argument usage and the bounding box.

When running, this step looks like this, with the white ellipse moving and bouncing:
 
Download Step 4 executable (ZIP, 338 KB).

Changes to Step 3:
 
 IlvPos x = rand() % 1000;
 IlvPos y = rand() % 1000;
 IlvFilledEllipse* mobile = new IlvFilledEllipse(display, IlvRect(x,y,30,30));
 mobile->setForeground(display->getColor("White"));
 manager->addObject(mobile, IlvFalse, 4);
In the main section, create a filled ellipse, change it to white, and add it to the manager in Layer 4. The ellipse will move above all the other objects.
 
 manager->setObjectName(mobile, "MovingEllipse");
Give a name to the ellipse. This will be very useful later for easy access to this specific object, when you will have to make it move.
 
 IlvTimer* timer = new IlvTimer(display, 0, 50, AnimateEllipse, manager);
 timer->run();
Now create the timer by providing the display, the period (in this example, the period is 50 ms), the function to call at each period, and an user argument. Here, the function AnimateEllipse is called every 50 ms.
 
 manager->addAccelerator(stopTimer, IlvKeyDown, 's', 0, timer);
 manager->addAccelerator(runTimer, IlvKeyDown, 'r', 0, timer);
To make the application more interactive, you can add two accelerators to stop and run the timer.

Now let's examine the AnimateEllipse function called by the timer:
 
static void AnimateEllipse(IlvTimer* timer, IlvAny userArg)
{
 static dx = 10;
 static dy = 10;

 IlvManager* manager = (IlvManager*)userArg;
 IlvFilledEllipse* mobile = (IlvFilledEllipse*)manager->getObject("MovingEllipse");
 if (!mobile)
  return;

The moving object can be retrieved using the manager getObject method. If the name is wrong, the failure case is tested.
 
 IlvRect bbox;
 mobile->boundingBox(bbox);
To get the object position and size, use the , which requires an IlvRect instance. It is important to know the position of the object to make it bounce against the window boundaries.
 
 if ((bbox.x() < 0) || (bbox.x() > 1000))
  dx = -dx;
 if ((bbox.y() < 0) || (bbox.y() > 1000))
  dy = -dy;

 manager->translateObject(mobile, dx, dy);

The X, Y, width and height values of th ellipse are retrieved with the boundingBox method. They are tested to make the moving object bounce if necessary, and the object is finally translated
 

Conclusion
You have seen how to use timers and animate objects. Some important concepts are shown here: the user arguments, the bounding box and the animation capabilities.

See complete code.

Download source code.

Learn More
    Features  
    Charts  
    Maps  
    Graph Layout  
    Controls  
    Data Access  
    Gantt Chart  
     
ILOG JViews Diagrammer
(includes Graph Layout)
ILOG JViews Maps
ILOG JViews Gantt
ILOG JViews Charts
ILOG JViews Telecom Graphic Objects
ILOG JViews Maps for Defense
Customer Spotlight
   
     
 
 
element3