Friendly Robot Class You will create a Robot class which will be able to draw a little robot icon at a particular place on the screen. Your robot will alternate drawing from two possible icons to create a small animation. We've provided main.cc that uses your Robot class to draw an animated robot that follows your cursor around. You will be implementing four member functions we've declared for you in the Robot class in robot.h. Robot needs a constructor, and getter and setter functions (aka accessors and mutators) for pixel location. Constructor We've declared the constructor in robot.h: Robot (const std::string& filenamel, const std::string& filename2); You must implement the Robot Class constructor in robot.cc. It takes two string parameters, the filename for the robot icon to show first, and the filename for the robot icon to show second. Your constructor should save these filenames in the filename1_ and filename2_ member variables but should not construct the images yet. Why not load the images in the constructor? It's not good practice to do work in constructors because if there is an error it leaves the program in a bad state. Don't forget: when you implement a member function, you must specify the class name before the function name, so the compiler knows that the function is a member of the class e.g.: int MyClassName::MyFunctionName() { }
main.cc file
#include "robotutils/robotclicklistener.h"
//
// You do not need to edit this file.
//
// Helper function to create robot*.bmp. Feel free to make your own
// icons and use this for inspiration.
/*
void CreateRobotIcon() {
graphics::Image image(31, 31);
// Arms
image.DrawLine(0, 10, 10, 15, 109, 131, 161, 6);
image.DrawLine(30, 10, 10, 15, 109, 131, 161, 6);
// Legs
image.DrawLine(10, 15, 10, 30, 109, 131, 161, 6);
image.DrawLine(20, 15, 20, 30, 109, 131, 161, 6);
// Body
image.DrawRectangle(5, 0, 20, 22, 130, 151, 179);
// Eyes
image.DrawCircle(10, 8, 2, 255, 255, 255);
image.DrawCircle(20, 8, 2, 255, 255, 255);
image.DrawCircle(9, 8, 2, 62, 66, 71);
image.DrawCircle(19, 8, 2, 62, 66, 71);
image.SaveImageBmp("robot.bmp");
}
*/
int main() {
RobotClickListener listener{};
listener.Start();
return 0;
}
robot.cc file
#include "robot.h"
#include <string>
#include "cpputils/graphics/image.h"
// ========================= YOUR CODE HERE =========================
// This implementation file (robot.cc) should hold the
// implementation of member functions declared in the header (robot.h).
//
// Implement the following member functions, declared in robot.h:
// 1. Robot constructor
// 2. SetPosition
// 3. GetX
// 4. GetY
//
// Remember to specify the name of the class with :: in this format:
// <return type> MyClassName::MyFunction() {
// ...
// }
// to tell the compiler that each function belongs to the Robot class.
// ===================================================================
// You don't need to modify these. These are helper functions
// used to load the robot icons and draw them on the screen.
void Robot::Draw(graphics::Image& image) {
// Load the image into the icon if needed.
if (icon1_.GetWidth() <= 0) {
icon1_.Load(filename1_);
}
if (icon2_.GetWidth() <= 0) {
icon2_.Load(filename2_);
}
mod_ = (mod_ + 1) % 2;
if (mod_ == 1) {
DrawIconOnImage(icon1_, image);
} else {
DrawIconOnImage(icon2_, image);
}
}
void Robot::DrawIconOnImage(graphics::Image& icon,
graphics::Image& image) const {
int width = icon.GetWidth();
int height = icon.GetHeight();
for (int i = 0; i < width; i++) {
for (int j = 0; j < height; j++) {
int x = x_ + i - width / 2;
int y = y_ + j - height / 2;
if (y >= 0 && x >= 0 && x < image.GetWidth() && y < image.GetHeight()) {
image.SetColor(x, y, icon.GetColor(i, j));
}
}
}
}
robot.h file
#include <string>
#include "cpputils/graphics/image.h"
class Robot {
public:
// You don't need to change this file, but you will be
// implementing some of these member functions in robot.cc.
Robot(const std::string& filename1, const std::string& filename2);
void SetPosition(int x, int y);
int GetX() const;
int GetY() const;
void Draw(graphics::Image& image);
private:
void DrawIconOnImage(graphics::Image& icon, graphics::Image& image) const;
std::string filename1_;
std::string filename2_;
graphics::Image icon1_;
graphics::Image icon2_;
graphics::Color color_;
int x_ = 0;
int y_ = 0;
int mod_ = 0;
};
![Friendly Robot Class
You will create a Robot class which will be able to draw a little robot icon at a
particular place on the screen.
Your robot will alternate drawing from two possible icons to create a small animation.
We've provided main.cc that uses your Robot class to draw an animated robot that
follows your cursor around.
DHI
You will be implementing four member functions we've declared for you in the Robot
class in robot.h.
Robot needs a constructor, and getter and setter functions (aka accessors and
mutators) for pixel location.
Constructor
We've declared the constructor in robot.h:
Robot (const std::string& filenamel, const std::string& filename2);
You must implement the Robot Class constructor in robot.cc. It takes two string
parameters, the filename for the robot icon to show first, and the filename for the robot
icon to show second. Your constructor should save these filenames in the filename1_
and filename2_ member variables but should not construct the images yet.
Why not load the images in the constructor? It's not good practice to do work in
constructors because if there is an error it leaves the program in a bad state.
Don't forget: when you implement a member function, you must specify the class
name before the function name, so the compiler knows that the function is a
member of the class e.g.:
int MyClassName::MyFunctionName() {
}](/v2/_next/image?url=https%3A%2F%2Fcontent.bartleby.com%2Fqna-images%2Fquestion%2F7b779863-aa72-4537-9750-69366ddc9e6e%2Ff943baa6-b8e3-44e0-8e15-c9324af5ef6f%2F128tlep_processed.jpeg&w=3840&q=75)
![Accessors (Getter) Functions
We declared the accessors (getter) functions for you in robot.h:
int GetX();
int GetY();
The Robot class tracks its (x, y) pixel location with two private integer member
variables. It must provide public accessor functions for x and y, GetX() and GetY()
which both return int s. Implement these accessor functions in robot.cc, following
the appropriate syntax for member function definitions (don't forget the class name!)
Mutator (Setter) Function
To set the (x, y) location, Robot provides a public function Set Position which takes
two arguments, the int x position and int y position.
void SetPosition(int x, int y);
Implement this mutator function in robot.cc, following the appropriate syntax for
member function definitions (don't forget the class name!)
How the Robot icon is Drawn
If you're curious how the Robot icon is drawn on the screen:
Robot must implement a function, Draw, that takes a reference to a
graphics::Image, and draws the robot icon on that image.
The Draw function will be called repeatedly by main.cc. Each time Draw is called
Robot should draw an icon centered at (x, y). Robot must alternate which icon is
drawn from the the two filenames in the constructor: The first time Draw is called it
shows the first icon, and the second time the second icon, etc. The Draw function
loads the image assets passed in the filenames that you should have stored in the
constructor. Draw uses this method from graphics::Image :
/*
* Loads an image from a file. Returns false if the image could
* not be loaded. Note: this clears any current state, including
* pixel values, width and height.
*/
bool Load (const std::string& filename);](/v2/_next/image?url=https%3A%2F%2Fcontent.bartleby.com%2Fqna-images%2Fquestion%2F7b779863-aa72-4537-9750-69366ddc9e6e%2Ff943baa6-b8e3-44e0-8e15-c9324af5ef6f%2Foombyzn_processed.jpeg&w=3840&q=75)
![](/static/compass_v2/shared-icons/check-mark.png)
Trending now
This is a popular solution!
Step by step
Solved in 3 steps
![Blurred answer](/static/compass_v2/solution-images/blurred-answer.jpg)
![Database System Concepts](https://www.bartleby.com/isbn_cover_images/9780078022159/9780078022159_smallCoverImage.jpg)
![Starting Out with Python (4th Edition)](https://www.bartleby.com/isbn_cover_images/9780134444321/9780134444321_smallCoverImage.gif)
![Digital Fundamentals (11th Edition)](https://www.bartleby.com/isbn_cover_images/9780132737968/9780132737968_smallCoverImage.gif)
![Database System Concepts](https://www.bartleby.com/isbn_cover_images/9780078022159/9780078022159_smallCoverImage.jpg)
![Starting Out with Python (4th Edition)](https://www.bartleby.com/isbn_cover_images/9780134444321/9780134444321_smallCoverImage.gif)
![Digital Fundamentals (11th Edition)](https://www.bartleby.com/isbn_cover_images/9780132737968/9780132737968_smallCoverImage.gif)
![C How to Program (8th Edition)](https://www.bartleby.com/isbn_cover_images/9780133976892/9780133976892_smallCoverImage.gif)
![Database Systems: Design, Implementation, & Manag…](https://www.bartleby.com/isbn_cover_images/9781337627900/9781337627900_smallCoverImage.gif)
![Programmable Logic Controllers](https://www.bartleby.com/isbn_cover_images/9780073373843/9780073373843_smallCoverImage.gif)