4. Clearing the Screen and Functions for Shapes

4. Clearing the Screen and Functions for Shapes
ZD YouTube FLV Player

h


Clearing the Screen and Functions for Shapes

Here is a quick overview of how to clear the screen and other functions you have for shapes.

Why Do We Need To Clear the Screen?

So what exactly do we mean by clearing the screen.  Well if we did the following code:

win << Circle(Point(5, 5), 2);
win.output_buffer();

Naturally we will draw a circle to the screen.  But if we do the following code right afterward:

win << Rect(Point(2, 2), 3, 3);
win.output_buffer();

It will then draw a Rectangle to the screen.  However, the circle we initially outputted will still be on there as well. If we want to only have the Rectangle on the screen by itself, we have to clear the circle that we drew before hand. To do that, we need to make sure the buffer is empty prior to inserting the Rect object.

Clearing the Screen

To clear the buffer we use the following function: win.clear_buffer();

This will clear all of the objects off of the buffer so we can begin inserting new ones to draw.  So now if we do the following code:

win << Circle(Point(5, 5), 2);
win.output_buffer();

win.clear_buffer();
win << Rect(Point(2, 2), 3, 3);
win.output_buffer();

First the circle is added to the buffer and drawn to the screen.  Then everything will be cleared from the buffer with win.clear_buffer().  This does NOT clear the screen, this just clears the drawing buffer.  Then the Rect is added to the empty drawing buffer.  And then finally, the Rect is outputted to the screen by itself with the second call of win.output_buffer();


More Functions for Shapes

Every shape in the Instinct Graphics Package can use the following functions:

  • obj.move(double dx, double dy);
  • obj.get_color();
  • obj.is_filled_in();
  • obj.set_visibility(double percent);
  • obj.get_visibility();

You can also use these functions below which were covered in Tutorial 2. Basic Commands and Drawing Shapes.

  • obj.set_color(Color color);
  • obj.set_filled_in(bool is_filled);

Function: obj.move(double dx, double dy);

Definition: Moves the shape dx units in the x direction and dy units in the y direction.

  • circ.move(5, 4);
  • message.move(-2, 3);
  • triangle.move(0, -1);

So for the first example, we moved the Circle circ 5 units in the x direction and 4 units in the y direction.

Function: obj.get_color();

Definition: Returns the color of the shape.

  • if(circ.get_color() == YELLOW)
  • Color rect_color = rect.get_color();

So for the first example, the if statement is true if the color of circ is YELLOW.

Function: obj.is_filled_in();

Definition: Returns whether or not the shape is filled in with color.  True means it is filled in, false means it is outlined with color.

  • rect.is_filled_in();
  • if(circ.is_filled_in())

The first example returns true if the Rect rect is filled in, and false if it is just outline with color.

Function: obj.set_visibility(double percent);

Definition: Sets the visibility of the shape to the percent amount passed in.  A percent value of 1.0 means the shape is fully visible (100% visible). A percent value of 0.5 means the shape is half visible (50% visible). A percent value of 0.0 means the shape is fully invisible (0% visible).

  • circ.set_visibility(0.75);
  • message.set_visibility(0.10);

The first example sets the visibility of circ to 75%.  So the Circle circ is parcially see-through but mostly visible.

Function: obj.get_visibility();

Definition: Returns the visibility amount of the shape.

  • if(circ.get_visibility() == 0.0)
  • rect.set_visibility(rect.get_visiblity() - 0.1);

The first example, the if statement is true if the visibility of circ is 0.0 (invisible).


Functions Specific to each Shape

Each shape also has its own set of specific functions that it’s shape type.  In other words, there are functions specific to Rects and functions specific to Circles, etc.  So let’s look at all of those:

Point

The Point shape has these specific functions it can call as well:

  • pt.get_x();
  • pt.get_y();
  • pt.set_x(double new_x);
  • pt.set_y(double new_y);

Circle

The Circle shape has these specific functions it can call as well:

  • circ.get_center();
  • circ.get_radius();
  • circ.set_radius(double new_radius);
  • circ.set_center(Point new_center);

Line

The Line shape has these specific functions it can call as well:

  • line.get_start();
  • line.get_end();
  • line.set_start(Point new_start);
  • line.set_end(Point new_end);

Triangle

The Triangle shape has these specific functions it can call:

  • tri.get_p1();
  • tri.get_p2();
  • tri.get_p3();
  • tri.set_p1(Point new_p1);
  • tri.set_p2(Point new_p2);
  • tri.set_p3(Point new_p3);

Message

The Message shape has these specific functions it can call:

  • message.get_text();
  • message.get_lower_left();
  • message.set_text(string text);
    message.set_text(int text);
    message.set_text(double text);