分享
 
 
 

面向对象开发范例之多态化图形操作

王朝other·作者佚名  2006-01-09
窄屏简体版  字體: |||超大  

/*

作者:meteor135

Email:meteor@mail.biti.edu.cn

smith_135@163.com

时间:2003.5.22

*/

#include <stdio.h>

#include <conio.h>

#include <graphics.h>

#include <bios.h>

#include <dos.h>

#define path "C:\\TC\\BGI" //改为你自己的BGI路径

#include <stdlib.h>

#include <string.h>

#include <iostream.h>

#include <math.h>

#define Pi 3.1415926

#define RIGHT 77

#define LEFT 75

#define UP 72

#define DOWN 80

#define HIDE 'h'

#define SHOW 's'

#define ENLARGE '='

#define REDUCE 'b'

#define Alt_X '-'

enum bool { false, true };

int GetKey();

int prompt();

void window(int, int, int, int, int, int, int);

//////////////////////////////////////////////////

class Location

{

protected:

int x0, y0;

public:

Location(int x, int y){x0=x;y0=y;}

int GetX0(){return x0;}

int GetY0(){return y0;}

};

class Point:public Location

{

protected:

bool isVisible;

void Draw();

public:

void Show(){putpixel(x0,y0,getcolor());}

Point(int x, int y):Location(x,y){};

bool GetVisible(){return isVisible;}

};

//////////////////////////////////////////////////////////////

class Figure : public Point

{

protected:

int changeStep;

virtual void Draw ()=0;

public:

Figure(int x, int y):Point(x, y){changeStep=10;}

public:

virtual void Show ();

virtual void Left ();

virtual void Up ();

virtual void Down ();

virtual void Hide ();

virtual void Right ();

public:

virtual void Enlarge()=0;

virtual void Reduce ()=0;

};

void Figure::Hide()

{

unsigned color = getcolor();

isVisible = false;

setcolor(getbkcolor());

Draw();

setcolor(color);

}

void Figure::Show()

{

setcolor(getcolor());

Draw();

isVisible=true;

}

void Figure::Up()

{

Hide();

(y0 -= changeStep) ? 0 : (y0 += 480);

Draw();

}

void Figure::Down()

{

Hide();

y0 = (y0 + changeStep) % 480;

Draw();

}

void Figure::Right()

{

Hide();

x0 = (x0 + changeStep) % 640;

Draw();

}

void Figure::Left()

{

Hide();

(x0 -= changeStep) ? 0 : (x0 += 640);

Draw();

}

//////////////////////////////////////////////////////////////

class Rectangle : public Figure

{

private:

int width,height;

void Draw()

{

int left,top,right,bottom;

left = x0-width /2;

top = y0-height/2;

right = x0+width /2;

bottom = y0+height/2;

rectangle(left,top,right,bottom);

}

public:

Rectangle(int x, int y, int w, int h):Figure(x,y)

{

width = w;

height = h;

}

public:

int GetWidth (){return width ;}

int GetHeight(){return height;}

void Enlarge();

void Reduce ();

};

void Rectangle::Enlarge()

{

Hide();

width += changeStep;

height += changeStep;

Draw();

}

void Rectangle::Reduce()

{

Hide();

width -= width >0&&height>0 ? changeStep : 0;

height -= height>0&&height>0 ? changeStep : 0;

Draw();

}

//////////////////////////////////////////////////////////////

class Circle: public Figure

{

private:

int radius;

void Draw(){circle(x0, y0, radius);}

public:

Circle(int x, int y, int radius);

int GetRadius(){return radius;}

void Enlarge();

void Reduce ();

};

Circle::Circle(int x, int y, int radius):Figure(x, y)

{

this->radius = radius;

}

void Circle::Enlarge()

{

Hide();

radius += changeStep;

Draw();

}

void Circle::Reduce()

{

Hide();

radius -= radius>0 ? changeStep : 0;

Draw();

}

/////////////////////////////////////////////////////////////////////////

void main()

{

Figure *pFigure;

int choice;

while((choice=prompt())>0)

{

switch(choice)

{

case 1:

pFigure = new Circle(320, 240, 100);

break;

case 2:

pFigure = new Rectangle(200,200,300,200);

break;

default:

pFigure = new Circle(320, 240, 100);

}

char s1[10], s2[10];

//char s3[15];

int key;

int gdriver = DETECT, gmode;

initgraph(&gdriver, &gmode, path);

setbkcolor(LIGHTCYAN);

cleardevice();

window(0, 0, getmaxx(), getmaxy(), 20, BLUE, LIGHTCYAN);

settextstyle(1, 0, 1);

setcolor(RED);

outtextxy(20, 15, "Now you will see a figure and you can do something with it");

outtextxy(20, 33, "Press any key to continue!");

outtextxy(20, 57, "h:hide s:show =:enlarge b:reduce");

outtextxy(20, 75, " :right :left :up :down alt_x:quit");

setlinestyle(SOLID_LINE, 1, 3);

line(26, 88, 40, 88);

line(40, 88, 36, 84);

line(40, 88, 36, 92);

line(100, 88, 114, 88);

line(100, 88, 104, 84);

line(100, 88, 104, 92);

line(214, 82, 214, 95);

line(214, 95, 210, 91);

line(214, 95, 218, 91);

line(166, 82, 166, 95);

line(166, 82, 162, 86);

line(166, 82, 170, 86);

setcolor(RED);

rectangle(390,62,441,81);

rectangle(454,62,505,81);

//rectangle(518,62,601,81);

while((key = GetKey())!= Alt_X)

{

setfillstyle(SOLID_FILL,CYAN);

bar(391,63,440,80);

bar(455,63,504,80);

//bar(519,63,600,80);

setviewport(20, 100, 618, 458 , 1);

switch(key)

{

case RIGHT : pFigure->Right ();

break;

case LEFT : pFigure->Left ();

break;

case UP : pFigure->Up ();

break;

case DOWN : pFigure->Down ();

break;

case ENLARGE : pFigure->Enlarge();

break;

case REDUCE : pFigure->Reduce ();

break;

case HIDE : pFigure->Hide ();

break;

case SHOW : pFigure->Show ();

break;

default :

break;

}

setviewport(0, 0, 639, 479, 1);

settextstyle(2,0,5);

setcolor(RED);

sprintf(s1,"X0=%d",pFigure->GetX0());

outtextxy(392,62,s1);

sprintf(s2,"Y0=%d",pFigure->GetY0());

outtextxy(456,62,s2);

//sprintf(s3,"RADIUS=%d",pFigure->GetRadius());

//outtextxy(520,62,s3);

}

delete pFigure;

closegraph();

}

}

///////////////////////////////////////////////////////////////////////////////////////

void window(int left, int top, int right, int bottom, int width, int color1, int color2)

{

setfillstyle(SOLID_FILL, color1);

bar(left, top, right, bottom);

setcolor(color2);

rectangle(left+width, top+width*5, right-width, bottom-width);

setfillstyle(SOLID_FILL, color2);

floodfill(left+width+1, top+width*5+1, color2);

rectangle(left+2, top+2, right-2, bottom-2);

rectangle(left+width-2, top+width-2, right-width+2, bottom-width+2);

line(left+width-1, top+width*3-2, right-width+1, top+width*3-2);

line(left+width-1, top+width*5-2, right-width+1, top+width*5-2);

line(left+2, top+2, left+width-2, top+width-2);

line(right-2, top+2, right-width+2, top+width-2);

line(left+2, bottom-2, left+width-2, bottom-width+2);

line(right-2, bottom-2, right-width+2, bottom-width+2);

}

///////////////////////////////////////////////////////////

int prompt()

{

printf("This program shows a figure, it can move there and here,\n");

printf("hide and then show.\n");

printf("\nPlease input your choice to show what figure you want to see:");

printf("\n1:Circle\n2:Rectangle\n0:quit\n");

int choice = 0;

cin >> choice;

return choice;

}

//////////////////////////////////////////////////////////

int GetKey()

{

int key;

while(bioskey(1) == 0);

key = bioskey(0);

key = key & 0xff ? key & 0xff : key >> 8;

return(key);

}

/////////////////////////////////////////////////////////

//The program is over here.

 
 
 
免责声明:本文为网络用户发布,其观点仅代表作者个人观点,与本站无关,本站仅提供信息存储服务。文中陈述内容未经本站证实,其真实性、完整性、及时性本站不作任何保证或承诺,请读者仅作参考,并请自行核实相关内容。
2023年上半年GDP全球前十五强
 百态   2023-10-24
美众议院议长启动对拜登的弹劾调查
 百态   2023-09-13
上海、济南、武汉等多地出现不明坠落物
 探索   2023-09-06
印度或要将国名改为“巴拉特”
 百态   2023-09-06
男子为女友送行,买票不登机被捕
 百态   2023-08-20
手机地震预警功能怎么开?
 干货   2023-08-06
女子4年卖2套房花700多万做美容:不但没变美脸,面部还出现变形
 百态   2023-08-04
住户一楼被水淹 还冲来8头猪
 百态   2023-07-31
女子体内爬出大量瓜子状活虫
 百态   2023-07-25
地球连续35年收到神秘规律性信号,网友:不要回答!
 探索   2023-07-21
全球镓价格本周大涨27%
 探索   2023-07-09
钱都流向了那些不缺钱的人,苦都留给了能吃苦的人
 探索   2023-07-02
倩女手游刀客魅者强控制(强混乱强眩晕强睡眠)和对应控制抗性的关系
 百态   2020-08-20
美国5月9日最新疫情:美国确诊人数突破131万
 百态   2020-05-09
荷兰政府宣布将集体辞职
 干货   2020-04-30
倩女幽魂手游师徒任务情义春秋猜成语答案逍遥观:鹏程万里
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案神机营:射石饮羽
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案昆仑山:拔刀相助
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案天工阁:鬼斧神工
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案丝路古道:单枪匹马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:与虎谋皮
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:李代桃僵
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案镇郊荒野:指鹿为马
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:小鸟依人
 干货   2019-11-12
倩女幽魂手游师徒任务情义春秋猜成语答案金陵:千金买邻
 干货   2019-11-12
 
推荐阅读
 
 
 
>>返回首頁<<
 
靜靜地坐在廢墟上,四周的荒凉一望無際,忽然覺得,淒涼也很美
© 2005- 王朝網路 版權所有