#include <graphics.h> #include <iostream.h> #include <stdio.h> #include <iomanip.h> #include <conio.h> #include <dos.h> #include <stdlib.h> #include <string.h> #include <math.h> #define MAX_X getmaxx() #define MAX_Y getmaxy() #define MIN_X 0 #define MIN_Y 0 #define DRAW_X1 85 #define DRAW_X2 580 #define DRAW_Y1 40 #define DRAW_Y2 450 FILE *fp; /////////////////////////////////////////////////////////////////////////////////////////// class Mouse //Class of mouse,hardware interfacing {
protected: int gd, button_left, x, y; union REGS i,o;
public: Mouse(): gd(DETECT), button_left(0), x(0), y(0) { } Mouse(int g, int button, int a, int b): gd(DETECT), button_left(button), x(a), y(b) { } int getx() { return x; } int gety() { return y; } int getbutton() { return button_left; } int initmouse(); void showmouseptr(); void restrictmouseptr(int, int, int, int); void getmousepos(); void hidemouseptr(); void printmousepos();
};
////////////////////////////////////////////////////////////////////////////////// class Shape //Base class Shape { protected: int x_initial, y_initial; public: Shape(); Shape(int a, int b); void set_xy(int a, int b); int get_x(); int get_y(); void print(int x, int y, char *s1); ~Shape() { ; } }; ////////////////////////////////////////////////////////////////////////////////////// class Eraser //Eraser class { private: int i, j; int color;
public: Eraser(int a = 0, int b = 0, int c = 0): i(a), j(b), color©{ } void set_color(int c) { color = c; setcolor(color); } int get_color() { return color; } void eraser(); ~Eraser() { ; } };
//////////////////////////////////////////////////////////////////////////////////////
class Freehand : public Shape //Freehand class derived publically from shape {
private: int x_final, y_final;// inntial x, y are inherited from shape int color;
public: Freehand(): Shape() { x_final = 0; y_final = 0; color = WHITE; setcolor(color); }
Freehand(int xf, int yf, int a, int b, int c) : Shape(a, b) { x_final = xf; y_final = yf; color = c; setcolor(color); }
void set_xfyf(int xf, int yf); //mutator function of x_final, y_final int get_xf(); int get_yf(); int get_color(); void set_color(int c); void draw(); ~Freehand() { ; } };
///////////////////////////////////////////////////////////////////
class Circle : public Shape //Cirlce class derived publically from shape {
private: int radius; int color;
public: Circle(): Shape() { radius = 0; color = WHITE; setcolor(color); }
Circle(int r, int a, int b, int c) : Shape(a, b) { radius = r; color = c; setcolor(color); } void set_radius(int r); int get_radius(); int get_color(); void set_color(int c); void show(); void draw(); ~Circle() { ; }
};
/////////////////////////////////////////////////////////////////////
class Rectangle : public Shape //Rectangle class derived publically from shape {
protected: int length, width; int color;
public: Rectangle(): Shape() { length = 0; width = 0; color = WHITE; setcolor(color); } Rectangle(int l, int w, int a, int b, int c) : Shape(a, b) { length = l; width = w; color = c; setcolor(color); } void set_length_width(int l, int b); int get_color(); void set_color(int c); int get_length(); int get_width(); void show(); void draw(); void fill(int c); ~Rectangle() { ; }
};
//////////////////////////////////////////////////////////////////////////////////////////////
class Titlebar //Displays the title bar { private: Rectangle r1,r2,r3; public: void show(); };
//////////////////////////////////////////////////////////////////////////////////////////////
class Toolbox //Displays the basic tools { private: Rectangle r; public: void show(); };
class Statusbox //Displays the current tool and color selected { private: Rectangle r; public: void show(); };
///////////////////////////////////////////////////////////////////////////////////////////
class Colorbox //Displays available color { private: Rectangle r;
public: void show(); };
//////////////////////////////////////////////////////////////////////////////////////////
class Window //A class containing All the display Objects(Aggregation) {
private: Titlebar obj_Titlebar; Colorbox obj_colorbox; Toolbox obj_Toolbox; Statusbox obj_Statusbox;
public: void show_all(); void show_titlebar(); void show_toolbar(); void show_colorbox(); void show_toolbox(); void show_statusbox(); void show_textbox(); void events(); };
//////////////////////////////////////////////////////////////////////////////////////////
int main(void) { clrscr(); int graphdriver, graphmode, errorcode; fp = fopen("C:\\logging.cpp","a+"); if(fp == NULL) { cout<<"Error Opening Log File."<<endl; } detectgraph(&graphdriver, &graphmode); graphdriver = DETECT; graphmode = DETECT; initgraph( &graphdriver, &graphmode,"c:\\tc\\bgi"); /* read result of initialization */ errorcode = graphresult(); if (errorcode != grOk) /* an error occurred */ { fputs("Graphics error\n",fp); //write to file cout<<"Graphics error:"<<endl; cout<<"Press any key to halt:"<<endl; getch(); exit(1); /* return with error code */ } fputs("Graphics Initialized\n",fp); //write to log file setbkcolor(BLACK); Window win; win.show_all(); fputs("Interface Visible\n",fp); //write to log file win.events(); fputs("Events Visible",fp); //write to file getch(); closegraph(); return 0; }
/////////////////////////////////////////////////////////////////////////////////////////////
int Mouse::initmouse() { i.x.ax = 0; int86(0x33, &i, &o); return(o.x.ax); }
void Mouse::showmouseptr() { i.x.ax = 1; int86(0x33, &i, &o); }
void Mouse::restrictmouseptr(int x1,int y1,int x2,int y2) { i.x.ax = 7; i.x.cx = x1; i.x.dx = x2; int86(0x33, &i, &o); i.x.ax = 8; i.x.cx = y1; i.x.dx = y2; int86(0x33, &i, &o); }
void Mouse::getmousepos() { i.x.ax = 3; int86(0x33, &i, &o); button_left = o.x.bx; x = o.x.cx; y = o.x.dx; }
void Mouse::hidemouseptr() { i.x.ax = 2; int86(0x33, &i, &o); }
void Mouse::printmousepos() {
gotoxy(75, 5); cout << "x="; cout << setw( 3 ) << x; //these are co-ordinates display gotoxy(75, 6); cout << "y="; cout<<setw(3)<<y ; //using manipulators for 3-character display
}
////////////////////////////////////////////////////////////////////////
Shape::Shape() { x_initial = 0; y_initial = 0; }
Shape::Shape(int a, int b) { x_initial = a; y_initial = b; }
void Shape::set_xy(int a, int b) { x_initial = a; y_initial = b; }
int Shape::get_x() { return x_initial; }
int Shape::get_y() { return y_initial; }
void Shape::print(int x, int y, char s1[]) { outtextxy(x, y, s1); }
////////////////////////////////////////////////////////////////////////
void Circle::set_radius(int r) { radius = r; }
int Circle::get_radius( )
{ return radius; }
int Circle::get_color() { return color; }
void Circle::set_color(int c) { color = c; setcolor(color); }
void Circle::show() { circle(x_initial, y_initial, radius); }
void Circle::draw() { x_initial = 0, y_initial = 0; // data members of shape as well as circle due to inheritance radius = 0; // data member of circle Mouse m; // Mouse Object int x_final, y_final, flag = 0;
int x, y, button_left;
while(1) { m.getmousepos();
x = m.getx(), y = m.gety(); button_left = m.getbutton();
if(x>=DRAW_X1 && x<=DRAW_X2 && y>=DRAW_Y1 && y<=DRAW_Y2) { if(button_left == 1 && flag == 0) // flag = 0 means 1st click { x_initial = x; y_initial = y; putpixel(x_initial, y_initial, RED); flag = 1; }
m.printmousepos();
if(button_left == 1 && flag == 1) // flag = 1 means 2nd click { x_final = x; y_final = y; radius = (float)sqrt((double) pow((x_final-x_initial), 2) + (double)pow((y_final-y_initial), 2)); m.showmouseptr(); }
if(button_left != 1 && flag==1) { circle(x_initial, y_initial, radius); flag = 0; int p_color = getcolor();
/*hiding the part of the circle outside drawing area*/ setcolor(BLACK); setfillstyle(SOLID_FILL,BLACK); bar3d(MIN_X,MIN_Y,MAX_X,37, 0, 0); setfillstyle(SOLID_FILL,BLACK); bar3d(MIN_X,461,MAX_X,MAX_Y, 0, 0); setfillstyle(SOLID_FILL,BLACK); bar3d(590,MIN_Y,MAX_X,MAX_Y, 0, 0); setfillstyle(SOLID_FILL,BLACK); bar3d(MIN_X,MIN_Y,81,MAX_Y, 0, 0); setcolor(WHITE); Window w; w.show_all(); //display the contents setcolor(p_color); //return back the drawing color }
}// main if ends
else break;
}//while ends
}
////////////////////////////////////////////////////////////////////////////
void Rectangle::set_length_width(int l, int b) { length = l; width = b; }
int Rectangle::get_length() { return length; }
int Rectangle::get_width() { return width; }
int Rectangle::get_color() { return color; }
void Rectangle::set_color(int c) { color = c; setcolor(color); }
void Rectangle::show() { rectangle(x_initial, y_initial, length, width); }
void Rectangle::draw() {
Mouse m; // Mouse Object x_initial = 0, y_initial = 0; // data members of shape class and due to inheritance it is member of Rectangle also length = 0, width = 0 ; // data members of Rectangle class int x, y, button_left, flag = 0;
while(1) { m.getmousepos();
x = m.getx(), y = m.gety(); button_left = m.getbutton();
if(x>=DRAW_X1 && x<=DRAW_X2 && y>=DRAW_Y1 && y<=DRAW_Y2) { if(button_left == 1 && flag == 0) // flag = 0 determines that its a first click for rectangle { x_initial = x; y_initial = y; flag = 1; }
m.printmousepos();
if(button_left == 1 && flag == 1) // flag = 1 means 2nd click { length = x; width = y; m.showmouseptr(); }
else if(button_left != 1) { rectangle(x_initial, y_initial, length, width); flag = 0; } } // main if ends
else break;
} //while ends
}
void Rectangle::fill(int c) { int border_color = get_color(); color = c;
setfillstyle(SOLID_FILL, color);
if( (x_initial<length) && (y_initial < width) ) floodfill(x_initial+2, y_initial+2, border_color); // +2 for inner co-ordinate, reqiured for floodfill else if((x_initial>length) && (y_initial>width) ) floodfill(x_initial-2, y_initial-2, border_color); // +2 for inner co-ordinate, reqiured for floodfill else if( (x_initial<length) && (y_initial> width) ) floodfill(x_initial+2, y_initial-2, border_color); // +2 for inner co-ordinate, reqiured for floodfill else if((x_initial>length) && (y_initial<width) ) floodfill(x_initial-2, y_initial+2, border_color); // +2 for inner co-ordinate, reqiured for floodfill
else outtextxy(0 ,0,"Error_Square"); }
///////////////////////////////////////////////////////////////////////////////////
void Titlebar::show() { r1.set_xy(0,0); r1.set_length_width(MAX_X, MIN_Y+20); r1.set_color(WHITE); r1.show(); r1.fill(LIGHTBLUE); settextstyle(DEFAULT_FONT,HORIZ_DIR,1); r1.print(MIN_X+15, MIN_Y+7, "UF-Paint");
/* Close Button */
setfillstyle(SOLID_FILL,BLUE); setcolor(WHITE); rectangle(MAX_X-20, MIN_Y+3,MAX_X-5, MIN_Y+17); line(MAX_X-20, MIN_Y+3, MAX_X-5, MIN_Y+17); line(MAX_X-20, MIN_Y+17,MAX_X-5, MIN_Y+3);
}
/////////////////////////////////////////////////////////////////////////////////
void Colorbox::show() { int start_y,end_y,count,col,color = 0; for(col=20;col<80;col+=10) { for(start_y=190,end_y=200,count=0;count<2 ;count++,start_y=end_y,end_y+=10,color++) { r.set_xy(MIN_X+col, MIN_Y+start_y); r.set_length_width(MIN_X+col+10, MIN_Y+end_y); r.set_color(WHITE); r.show(); r.fill(color); settextstyle(SMALL_FONT,HORIZ_DIR,4); }//inner for ends }//outer for ends }
//////////////////////////////////////////////////////////////////////////////
void Toolbox::show() { int start_y,end_y,count; for(start_y=70,end_y=90,count=0;count<5 ;count++,start_y=end_y,end_y+=20 ) { r.set_xy(MIN_X+20, MIN_Y+start_y); r.set_length_width(MIN_X+80, MIN_Y+end_y); r.set_color(WHITE); r.show(); r.fill(9); settextstyle(SMALL_FONT,HORIZ_DIR,4);
//why not use switch case? if(start_y == 70) outtextxy(MIN_X+25, MIN_Y+start_y,"Clear"); else if(start_y == 90) outtextxy(MIN_X+25, MIN_Y+start_y,"Pencil"); else if(start_y == 110) outtextxy(MIN_X+25, MIN_Y+start_y,"Circle"); else if(start_y == 130) outtextxy(MIN_X+25, MIN_Y+start_y,"Rectangle"); else if(start_y == 150) outtextxy(MIN_X+25, MIN_Y+start_y,"Eraser");
}//for ends }
////////////////////////////////////////////////////////////////////////////////////
void Statusbox::show() {
int start_y,end_y,count; for(start_y=240, end_y=280, count=0; count<2 ; count++, start_y = end_y, end_y+=40 ) { r.set_xy(MIN_X+20, MIN_Y+start_y); r.set_length_width(MIN_X+80, MIN_Y+end_y); r.set_color(WHITE); r.show();
//fill each with color//how about different colours? r.fill(9); settextstyle(SMALL_FONT,HORIZ_DIR,4); //why not use switch case? if(start_y == 240) outtextxy(MIN_X+25, MIN_Y+start_y,"Tool"); else if(start_y == 280) outtextxy(MIN_X+25, MIN_Y+start_y,"Color"); }//for ends
}
////////////////////////////////////////////////////////////////////////
void Window::show_all() { obj_Titlebar.show(); obj_colorbox.show(); obj_Toolbox.show(); obj_Statusbox.show(); setcolor(BLUE); setlinestyle(DOTTED_LINE,BLUE,3); rectangle(82,38,590,460); setlinestyle(SOLID_LINE,WHITE,1); fputs("Titlebar,Colorbar,Toolbar,Statusbar and Drawing Area visible \n",fp); //write to log file
}
void Window::show_titlebar() { obj_Titlebar.show(); }
void Window::show_colorbox() { obj_colorbox.show(); }
void Window::show_toolbox() { obj_Toolbox.show(); }
void Window::events() { Mouse m; // Mouse Object int choice = 0, choice_1; int x , y, button_left;
setfillstyle(SOLID_FILL, LIGHTBLUE); settextstyle(SMALL_FONT, HORIZ_DIR, 4);
while(1) { x = m.getx(), y = m.gety(); button_left = m.getbutton();
m.showmouseptr(); m.restrictmouseptr(MIN_X,MIN_Y,MAX_X,MAX_Y); m.getmousepos(); // whenever you write getmousepos(),the current co-ordinates are sent to x and y declared globally.You can pass them to your function. m.printmousepos(); // moreover whenever you want to use see if the mouse is clicked ,on right click button_right=1 and on left click button_left=1 Freehand F; Rectangle R; Circle C; Eraser E;
if(x>=20 && x<=30 && y>=190 && y<=200 && button_left == 1) // For colorbox events { fputs("Color Choice made BLACK \n",fp); //write to log file
choice_1 = 1; setfillstyle(SOLID_FILL, LIGHTBLUE); bar(22, 295, 78, 310); outtextxy(25, 298, "BLACK"); } else if(x>=30 && x<=40 && y>=190 && y<=200 && button_left == 1) { fputs("Color Choice made LIGHT GREEN \n",fp); //write to log file
choice_1 = 2; setfillstyle(SOLID_FILL, LIGHTBLUE); bar(22, 295, 78, 310); outtextxy(25, 298, "GREEN"); } else if(x>=40 && x<=50 && y>=190 && y<=200 && button_left == 1) { fputs("Color choice made RED \n",fp); //write to log file
choice_1 = 3; setfillstyle(SOLID_FILL, LIGHTBLUE); bar(22, 295, 78, 310); outtextxy(25, 298, "RED"); }
else if(x>=50 && x<=60 && y>=190 && y<=200 && button_left == 1) { fputs("Color choice made BROWN \n",fp); //write to log file choice_1 = 4; setfillstyle(SOLID_FILL, LIGHTBLUE); bar(22, 295, 78, 310); outtextxy(25, 298, "BROWN"); } else if(x>=60 && x<=70 && y>=190 && y<=200 && button_left == 1) { fputs("Color choice made HIGH-GRAY \n",fp); //write to log file choice_1 = 5; setfillstyle(SOLID_FILL, LIGHTBLUE); bar(22, 295, 78, 310); outtextxy(25, 298, "HIGH-GRAY"); }
else if(x>=70 && x<=80 && y>=190 && y<=200 && button_left == 1) { fputs("Color choice made LOW-GRAY \n",fp); //write to log file choice_1 = 6; setfillstyle(SOLID_FILL, LIGHTBLUE); bar(22, 295, 78, 310); outtextxy(25, 298, "LOW-GREEN"); }
/* row change of colorbox */
else if(x>=20 && x<=30 && y>=200 && y<=210 && button_left == 1) { fputs("Color choice made BLUE \n",fp); //write to log file choice_1 = 7; setfillstyle(SOLID_FILL, LIGHTBLUE); bar(22, 295, 78, 310); outtextxy(25, 298, "BLUE"); }
else if(x>=30 && x<=40 && y>=200 && y<=210 && button_left == 1) { fputs("Color choice made CYAN \n",fp); //write to log file choice_1 = 8; setfillstyle(SOLID_FILL, LIGHTBLUE); bar(22, 295, 78, 310); outtextxy(25, 298, "CYAN"); }
else if(x>=40 && x<=50 && y>=200 && y<=210 && button_left == 1) { fputs("Color choice made MAGENTA \n",fp); //write to log file choice_1 = 9; setfillstyle(SOLID_FILL, LIGHTBLUE); bar(22, 295, 78, 310); outtextxy(25, 298, "MAGENTA"); }
else if(x>=50 && x<=60 && y>=200 && y<=210 && button_left == 1) { fputs("Color choice made LOW-GRAY \n",fp); //write to log file choice_1 = 10; setfillstyle(SOLID_FILL, LIGHTBLUE); bar(22, 295, 78, 310); outtextxy(25, 298, "LOW-GRAY"); }
else if(x>=60 && x<=70 && y>=200 && y<=210 && button_left == 1) { fputs("Color choice made LOW-BLUE \n",fp); //write to log file choice_1 = 11; setfillstyle(SOLID_FILL, LIGHTBLUE); bar(22, 295, 78, 310); outtextxy(25, 298, "LOW-BLUE"); }
else if(x>=70 && x<=80 && y>=200 && y<=210 && button_left == 1) { fputs("Color choice made LOW-CYAN \n",fp); //write to log file choice_1 = 12; setfillstyle(SOLID_FILL, LIGHTBLUE); bar(22, 295, 78, 310); outtextxy(25, 298, "LOW-CYAN"); }
/* for toolbox events*/
/* for Clear */ if(x>=20 && x<=80 && y>=70 && y<=90 && button_left == 1) { setfillstyle(SOLID_FILL, BLACK); bar(83, 39, 589, 459); setfillstyle(SOLID_FILL, LIGHTBLUE); bar(22, 255, 78, 270); // to clear tool status bar(22, 295, 78, 310); // to clear color status choice = 1; }
/* for pencil or freehand */ else if(x>=20 && x<=80 && y>=90 && y<=110 && button_left == 1) { fputs("Pencil Selected \n",fp); //write to log file choice = 2; setfillstyle(SOLID_FILL, LIGHTBLUE); bar(22, 255, 78, 270); outtextxy(25, 258, " Pencil"); }
/* for circle */ else if(x>=20 && x<=80 && y>=110 && y<=130 && button_left == 1) { fputs("Circle Selected \n",fp); //write to log file choice = 3; setfillstyle(SOLID_FILL, LIGHTBLUE); bar(22, 255, 78, 270); outtextxy(25, 258, " Circle"); }
/* for rectangle */ else if(x>=20 && x<=80 && y>=130 && y<=150 && button_left == 1) { fputs("Rectangle Selected \n",fp); //write to log file choice = 4; setfillstyle(SOLID_FILL, LIGHTBLUE); bar(22, 255, 78, 270); outtextxy(25, 258, "Rectangle"); } /* for Eraser */ else if(x>=20 && x<=80 && y>=150 && y<=170 && button_left == 1) { fputs("Eraser Selected \n",fp); //write to log file choice = 5; setfillstyle(SOLID_FILL, LIGHTBLUE); bar(22, 255, 78, 270); //setcolor(WHITE); outtextxy(25, 258, " Eraser"); }
/* For colorbox */ switch(choice_1) { case 1 : setcolor(BLACK); break; case 2 : setcolor(GREEN); break; case 3 : setcolor(RED); break; case 4 : setcolor(BROWN); break; case 5 : setcolor(DARKGRAY); break; case 6 : setcolor(LIGHTGREEN); break; case 7 : setcolor(BLUE); break; case 8 : setcolor(CYAN); break; case 9 : setcolor(MAGENTA); break; case 10 : setcolor(LIGHTGRAY); break; case 11 : setcolor(LIGHTBLUE); break; case 12 : setcolor(LIGHTCYAN); break; }
/* For toolbox */ switch(choice) {
case 1 : choice = 0; break; case 2 : F.draw(); break; case 3 : C.draw(); break; case 4 : R.draw(); break; case 5 : E.eraser(); break; }
/* for exit cross button on titile bar */ if(x>=620 && x<=632 && y>=2 && y<=18 && button_left == 1) { fputs("Exit Button Clicked \n",fp); //write to log file exit(0); } }
}
/////////////////////////////////////////////////////////////////////////////////////////////////////
void Eraser::eraser() { Mouse m; // Mouse Object
int x, y, button_left; color = BLACK;
while(1) {
m.getmousepos();
x = m.getx(), y = m.gety(); button_left = m.getbutton();
if(x>=85 && x<=570 && y>=40 && y<=440) { m.printmousepos();
if(button_left == 1) { m.hidemouseptr(); for(i=y; i<=y+16; i++) for(j=x; j<=x+16; j++) putpixel(j,i, color); }
m.showmouseptr();
}
else break;
}// WHILE ENDS }
///////////////////////////////////////////////////////////////////////////////////////////////
void Freehand::set_xfyf(int xf, int yf) //mutator function of x_final, y_final { x_final = xf; y_final = yf; }
int Freehand::get_xf() { return x_final; }
int Freehand::get_yf() { return y_final; }
int Freehand::get_color() { return color; }
void Freehand::set_color(int c) { color =c; setcolor(color); }
void Freehand::draw() {
Mouse m; // Mouse Object int previous_x = 1, previous_y = 1, flag=0; int x, y, button_left; while(1) { m.getmousepos();
x = m.getx(), y = m.gety(); button_left = m.getbutton();
if(x>=85 && x<=580 && y>=40 && y<=450) { m.printmousepos();
if(button_left == 1) { if(flag == 0) { m.hidemouseptr(); flag=1; }
else line(previous_x, previous_y, x, y); }
else if(button_left != 1) { m.showmouseptr(); flag = 0; }
previous_x = x; previous_y = y;
}// main if ends
else { m.showmouseptr(); break; }
}//While ends
}
|