VERSION 1
#include
"library.h"
class
Component
{
protected:
int xleft, ytop, width, height;
string text;
public:
Component(int x, int y, int w, int
h, string t)
{ xleft=x;
ytop=y;
width=w;
height=h;
text=t; }
void draw_background(void)
{ PenColor(0.7,
0.7, 0.7);
FillRectangleXYWH(xleft, ytop, width, height); }
void draw_border(void)
{ PenColor(0, 0,
0);
DrawRectangleXYWH(xleft, ytop, width, height); }
void draw_content(void)
{ PenColor(0, 0,
0);
MoveTo(xleft+10,
ytop+2);
WriteText(text);
}
void draw(void)
{ draw_background();
draw_border();
draw_content();
} };
class
Window
{
protected:
Component *content[100];
int num;
int width, height;
bool showing;
public:
Window(int w, int h)
{ width=w;
height=h;
num=0;
showing=false; }
void draw(void)
{ if (!showing)
{ MakeWindow(width,
height);
showing=true; }
PenColor(1, 1,
1);
FillRectangleXYWH(0,
0, width, height);
for (int i=0; i<num; i+=1)
content[i]->draw();
}
void add(Component *c)
{ if (num>=100) return;
content[num]=c;
num+=1; } };
void
main(void)
{
Window *w = new Window(100, 170);
Component *c1=new Component(10, 10, 75, 20,
"Hello");
w->add(c1);
Component *c2=new Component(10, 40, 75, 20,
"abcdef");
w->add(c2);
w->draw(); }
VERSION 2
#include
"library.h"
class
Component
{ ...
... //
same as before
... };
class
PictureComponent: public Component
{
protected:
Image *picture;
public:
PictureComponent(int x, int y, string filename)
{ xleft=x;
ytop=y;
text=filename;
picture=ImageFromFile(filename);
if (picture==NULL)
{ printf("Could
not open file '%s': %s\n", filename.c_str(), GetReasonForFailure().c_str());
return; }
picture->GetSize(width,
height); }
void draw_content(void)
{ DrawImage(picture,
xleft, ytop); } };
class
Window
{ ...
... //
same as before
... };
void
main(void)
{
Window *w = new Window(100, 170);
Component *c1=new Component(10, 10, 75, 20,
"Hello");
w->add(c1);
Component *c2=new Component(10, 40, 75, 20,
"abcdef");
w->add(c2);
PictureComponent
*pc1=new PictureComponent(10, 70, "ant.bmp");
w->add(pc1);
w->draw(); }
//
Note: GetReasonForFailure returns a C++ string, but printf takes C-style char arrays,
// so the method c_str()
must be used.
//
Also Note: The only image files currently understood are windows bitmap (.BMP)
files;
// the file must be in your main working
folder, along with the .cpp and .h files,
// or you can give the exact path
explicitly: “c:\\temp\\ant.bmp”.
This
compilation gets error message:
Line 33: 'Component' : no appropriate default
constructor available
Line
33 is this one:
PictureComponent(int x, int y, string filename)
VERSION 3
#include
"library.h"
class
Component
{ ...
... //
same as before
... };
class
PictureComponent: public Component
{
protected:
Image *picture;
public:
PictureComponent(int x, int y, string filename):
Component(x, y, 0, 0, filename)
{ xleft=x;
ytop=y;
text=filename;
picture=ImageFromFile(filename);
if (picture==NULL)
{ printf("Could
not open file '%s': %s\n", filename.c_str(), GetReasonForFailure().c_str());
return; }
picture->GetSize(width,
height); }
void draw_content(void)
{ DrawImage(picture,
xleft, ytop); } };
class
Window
{ ...
... //
same as before
... };
void
main(void)
{
Window *w = new Window(100, 170);
Component *c1=new Component(10, 10, 75, 20,
"Hello");
w->add(c1);
Component *c2=new Component(10, 40, 75, 20,
"abcdef");
w->add(c2);
PictureComponent
*pc1=new PictureComponent(10, 70, "ant.bmp");
w->add(pc1);
w->draw(); }
//
Note: the only change from version 2 is on line 33.
VERSION 4
#include
"library.h"
class
Component
{
protected:
int xleft, ytop, width, height;
string text;
public:
Component(int x, int y, int w, int
h, string t)
{ ...
...
// as before
... }
virtual void draw_background(void)
{ ...
...
// as before
... }
virtual void draw_border(void)
{ ...
...
// as before
... }
virtual void draw_content(void)
{ ...
...
// as before
... }
virtual void draw(void)
{ ...
...
// as before
... } };
class
PictureComponent: public Component
{
protected:
Image *picture;
public:
PictureComponent(int x, int y, string filename):
Component(x, y, 0, 0, filename)
{ ...
...
// as before
... }
virtual void draw_content(void)
{ ...
...
// as before
... } };
class
Window
{ ...
... //
as before
... };
void
main(void)
{
Window *w = new Window(100, 170);
Component *c1=new Component(10, 10, 75, 20,
"Hello");
w->add(c1);
Component *c2=new Component(10, 40, 75, 20,
"abcdef");
w->add(c2);
PictureComponent
*pc1=new PictureComponent(10, 70, "ant.bmp");
w->add(pc1);
w->draw(); }
VERSION 5
#include
"library.h"
class
Component
{ ...
... //
as before
... };
class
PictureComponent: public Component
{
protected:
Image *picture;
public:
PictureComponent(int x, int y, string filename):
Component(x, y, 0, 0, filename)
{ xleft=x-1;
ytop=y-1;
text=filename;
picture=ImageFromFile(filename);
if (picture==NULL)
{ printf("Could
not open file '%s': %s\n", filename.c_str(), GetReasonForFailure().c_str());
return; }
picture->GetSize(width,
height);
width+=2;
height+=2; }
virtual void draw_content(void)
{ if (picture!=NULL) DrawImage(picture,
xleft+1, ytop+1); } };
class
Window
{ ...
... //
as before
... };
void
main(void)
{
Window *w = new Window(100, 170);
Component *c1=new Component(10, 10, 75, 20,
"Hello");
w->add(c1);
Component *c2=new Component(10, 40, 75, 20,
"abcdef");
w->add(c2);
PictureComponent
*pc1=new PictureComponent(10, 70, "ant.bmp");
w->add(pc1);
w->draw(); }