/**************************************************************************** ** COPYRIGHT (C): 1997 Cay S. Horstmann. All Rights Reserved. ** PROJECT: Practical OO Development with C++ and Java ** FILE: fillrect.cpp ** PURPOSE: test filled rectangles in chapter 8 ** VERSION 1.0 ** PROGRAMMERS: Cay Horstmann (CSH) ** RELEASE DATE: 3-15-97 (CSH) ** UPDATE HISTORY: ****************************************************************************/ /* Project: GUI Application Add files fillrect.cpp \PracticalOOBook\cpplib\ Additional include directory: \PracticalOOBook\cpplib\graphics.cpp */ #include "setup.h" #include #include #include "graphics.h" #define min(a, b) (((a) < (b)) ? (a) : (b)) #define max(a, b) (((a) > (b)) ? (a) : (b)) class Point { public: Point(int xx =0, int yy =0); void scale(Point center, double s); void move(int x, int y); void plot(Graphics& g) const; void print(ostream& os) const; int x() const; int y() const; private: int _x; int _y; }; /*-------------------------------------------------------------*/ class Rectangle { public: Rectangle(); Rectangle(Point, Point); virtual void scale(Point center, double s); virtual void move(int x, int y); virtual void plot(Graphics& g) const; virtual void print(ostream& os) const; Point topleft() const; Point bottomright() const; private: Point _topleft; Point _bottomright; }; /*-------------------------------------------------------------*/ class FilledRect : public Rectangle { public: FilledRect(); FilledRect(Point, Point, Color); virtual void plot(Graphics& g) const; private: Color _color; }; /*-------------------------------------------------------------*/ Point::Point(int xx, int yy) : _x(xx), _y(yy) {} /*.............................................................*/ void Point::plot(Graphics& g) const { g.drawOval(_x - 2, _y - 2, 5, 5); } /*.............................................................*/ void Point::print(ostream& os) const { os << "(" << _x << "," << _y << ")"; } /*.............................................................*/ void Point::scale(Point center, double s) { _x = center._x + (int)((_x - center._x) * s); _y = center._y + (int)((_y - center._y) * s); } /*.............................................................*/ void Point::move(int x, int y) { _x += x; _y += y; } /*.............................................................*/ inline int Point::x() const { return _x; } /*.............................................................*/ inline int Point::y() const { return _y; } /*-------------------------------------------------------------*/ Rectangle::Rectangle() {} /*.............................................................*/ Rectangle::Rectangle(Point p, Point q) /* RECEIVES: p, q - two corner points */ : _topleft(min(p.x(), q.x()), min(p.y(), q.y())), _bottomright(max(p.x(), q.x()), max(p.y(), q.y())) {} /*.............................................................*/ void Rectangle::plot(Graphics& g) const { g.drawRect(_topleft.x(), _topleft.y(), _bottomright.x() - _topleft.x(), _bottomright.y() - _topleft.y()); } /*.............................................................*/ void Rectangle::print(ostream& os) const { _topleft.print(os); _bottomright.print(os); } /*.............................................................*/ void Rectangle::scale(Point center, double s) { _topleft.scale(center, s); _bottomright.scale(center, s); } /*.............................................................*/ void Rectangle::move(int x, int y) { _topleft.move(x, y); _bottomright.move(x, y); } /*.............................................................*/ Point Rectangle::topleft() const { return _topleft; } /*.............................................................*/ Point Rectangle::bottomright() const { return _bottomright; } /*-------------------------------------------------------------*/ FilledRect::FilledRect() : _color(Color::black) {} /*.............................................................*/ FilledRect::FilledRect(Point p, Point q, Color b) : _color(b), Rectangle(p, q) {} /*.............................................................*/ void FilledRect::plot(Graphics& g) const { g.setColor(_color); g.fillRect(topleft().x(), topleft().y(), bottomright().x() - topleft().x(), bottomright().y() - topleft().y()); g.setColor(Color::black); } /*-------------------------------------------------------------*/ void paint(Graphics& g) { #ifndef PRACTICALOO_MUST_USE_ALLOCATOR vector rects(4); // a vector of pointers #else vector > rects(4); #endif rects[0] = new FilledRect(Point(10, 70), Point(20, 20), Color::red); rects[1] = new Rectangle(Point(20, 70), Point(30, 10)); rects[2] = new FilledRect(Point(30, 70), Point(40, 30), Color::blue); rects[3] = new Rectangle(Point(5, 5), Point(45, 75)); for (int i = 0; i < rects.size(); i++) rects[i]->plot(g); }