libShiva

Example: Basic shapes

There are multiple basic shapes which can be described by specifying their properties like width or height. Creating this objects is very similiar as creating a window. We can either specify their properties in constructor or call their appropriate member functions.

All the shapes should be constructed after the application has been initialized and a window created, otherwise the behavior of the application is undefined.

Whenever you change a propery of a shape by calling its member function, the shape will be updated and the changes will be seen on the screen immediately at next redraw.

Note that each of the basic shapes has multiple constructors, each initializing the shape via a different kind of parameters. In this examples, though, we only show one possible constructor for each shape. Refer to the class documentation to get a list of all the possibilities.


Line

To create a line, we specify its start and end point by passing the X and Y coordinates for each to its constructor:

  LineRef ln = new Line(100,100,200,200);

This line will start at point (100,100) and end at point (200,200). If we want to change the end or start point we call the setStart() and setEnd() member functions of the line object:

  ln->setStart(200,200);
  ln->setEnd(100,100);

To make the line visible, we have to specify its stroke. For now, lets just use a simple black stroke:

  ln->setStroke(0,0,0);

Finally, to make it actually appear in the window, we have to add it as a child to it. The window will thus act as a parent to the line object in the display hierarchy.

  wnd->addChild(ln);


Rectangle

To create a simple rectangle we specify its location and size:

  RectangleRef rc = new Rectangle(10,10,100,100);

This rectangle will be positioned at coordinate (10,10) and will have size of 100x100 pixels. If we want to change this parameters, to move the rectangle and resize it we should call the setLocation() and setSize() member functions.

  rc->setLocation(20,20);
  rc->setSize(200,200);

Rectangles can also have rounded corners. The radius of each corner is set via SetRadius() member function. You can also specify which of the corners will actually get rounded by specifying a radius mask. A boolean value for each corner is passed in the order: top-left, top-right, bottom-right, bottom-left.

  rc->setRadius(10);
  rc->setRadiusMask(true,true,false,false);

To make the rectangle visible, we have to set it some fill or stroke. For now, lets just use a black fill:

  rc->setFill(0,0,0);

We add our rectangle as a child to the window much like we added the line:

  wnd->addChild(rc);


Ellipse

We create an ellipse by specifying its center point and radius in X and Y direction:

  EllipseRef el = new Ellipse(200,200,100,100);

To change its properties we call the setCenter() and setRadius() member function:

  el->setCenter(100,100);
  el->setRadius(50,50);

We set the fill of an ellipse and add it as a child to the window the same way as the rectangle.