·设为首页
·收藏本站  
·错误报告
您现在的位置: 首页>>理工论文>>计算机论文>>新闻

站长欢迎各位朋友投稿6366083#163.com(#换成@),本站永久免费,请各位朋友记住本站网址:www.lwen.net,别忘记介绍新朋友过来奥^_^

会员登陆
最近更新
有广告才有收入,网站才能发展。
DOS界面下通用图形编辑软件的设计
  发表时间:2005-9-28 14:56:29  浏览人次: 我要投稿
>一、数据结构与数据文件格式由于所有的操作都基本建立在图素的基础之上,故数据结构也以图素为中心。以下以圆、直线、矩形、字符串为例,其它图素类似。1.定义所需图素struct circle /*定义圆 */{int x,y,r; /* 圆心,半径 */char linecolor,linestyle; /* 圆外围线的颜色,线型 */char fillcolor,fillstyle; /* 填充颜色,模式 */};struct line /* 定义直线 */{int x1,y1;int x2,y2;char linecolor,linestyle,linethick; /* 线颜色,模式,粗细 */};struct box /* 定义矩形 */{int x1,y1;int x2,y2;char linecolor,linestyle;char fillcolor,fillstyle;};struct string /* 定义字符串 */{int x,y;char str[10]char backcolor,dir;char str-color,str-style;};.. /* 定义其它图素 */.2.将各图素置于一条链表之中typedef struct tagElementList{char ElementType; /* 标识元素类别 */int ElementID; /* 元素标识符,在接口中用来控制其属性 */union tagElement {struct circle circle;struct box box;struct string string;struct line line;.. /* 可在此说明其它元素 */.}Element;struct tagElementList *next;}ElementList;利用这种数据结构可在内存中形成一个图素链表,所有操作都可以此链表为基础。3.定义几个指针,以备各种操作ElementList *List-head. *List-end,*List-temp, *List-here;4.定义一个全局变量,记录图素个数static int Elementcount=0;图形文件格式为:第一字节(char),表示整个图形的背景颜色;接下来一个字(word),对应于Elementcount,表示图素个数;后面是内存链表中每个图素的属性值。二、图形编辑功能的实现本软件包含的图形编辑功能主要有:作图、修改、移动、删除、复制,下面仅举几例说明实现的方法。1.作图以圆为例,其它图形类似。drawcircle(){int i;char s[20],c;int cx,cy,cr;int cls,clc,cfc,cfs;movecursor(); /* 移动光标,确定圆心 */cx=cursor-x;cy=cursor-y;movecursor(); /* 确定半径 */cr=(int)sqrt((cursor-x-cx)*(cursor-x-cx)+(cursor-y-cy)*(cursor-y-cy);setcolor(WHITE);circle (cx,cy,cr); /* 画圆 */cls=selectlinestyle();clc=selectcolor ("select-line-color");setcolor(clc);for(i=0;i<=cls;i++)circle(cx,cy,cr-i);cfs=selectfillstyle();cfc=selectcolor("set-fill-color");setfillstyle(cfs.cfc);floodfill(cx,cy,clc); /* 填充 */temp(ElementList *) malloc(sizeof(ElementList));temp->ElementType= 'c';temp->Element.circle.x=cx;temp->Element.circle.y=cy;temp->Element.circle.r=cr;temp->Element.circle.lcolor=clc;temp->Element.circle.lstyle=cls;temp->Element.circle.fcolor=cfc;temp->Element.circle.fstyle=cfs;addtolist(temp); /* 将图素加入图素链表 */}其中 addtolist ()可以如下实现:addtolist (ElementList *Etemp){if(List-head==NULL){List-head=Etemp;List-end=Etemp;}else{ List-end->next=Etemp;List-end=Etemp;Etemp->next=NULL;}Elementcount++;}2.图形的移动、删除、复制功能以移动为例,首先用箭头键或鼠标框取要移动的区域,区域矩形的左上,右下坐标分别为(block-x1,block-y1),(block-x2,block-y2),然后移动标识矩形到要到达的地方,确定。这样标识矩形的终止位置与初始位置存在一个偏差,水平与垂直偏差分别为dl-x,dl-y。接下来搜索内存图素链表,确定每个图素的外接矩形,判断外接矩形是否在初始标识矩形内,若在,则将该图素的坐标属性值改变dl-x,dl-y。清除图形区,根据新的图素链表作图。图形的删除功能类似,只需将符合条件的图素从链表中清除,再修改Elementcount值即可。拷贝图形则只需将符合条件的图素备份一个结点,修改结点的坐标属性值,再将该结点加入链表,相应增加Elementcount的值。以下为移动图形的代码。fnMove (){Rect rect; /* 定义的矩形 */int i;selectblock (); /* 选择要移动的块 */moveblock (); /* 移动块 */List-temp=List-head;for (i=0;i<Elementcount; i++){getrect (&rect, List-temp); /* 计算List-temp所指图素的外接矩形 */if (inblock(rect.x1,rect.x2,rect.y1.rect.y2))/* 判断外接矩形是否在所选块内 */change (List-temp, dl-x,dl-y);/* 改变图素的坐标属性 */List-temp=List-temp->next;}clearscreeen (); /* 清除作图区 */drawlink (); /* 依据图素链表画图 */}其中,change ( )可以实现如下。change(ElementList *Ctemp, int dl-x,int dl-y){switch (Ctemp->ElementType){case 'c': Ctemp->Element.circle.x+=dl-x;Ctemp->Element.circle.y+=dl-y;break;case 'b': Ctemp->Element.box.x1+=dl-x;Ctemp->Element.box.x2+=dl-x;Ctemp->Element.box.y1+=dl-y;Ctemp->Element.box.y2+=dl-y;break;case 'l': Ctemp->Element.line.x1+=dl-x;Ctemp->Element.line.y1+=dl-y;Ctemp->Element.line.x2+=dl-x;Ctemp->Element.line.y2+=dl-y;break;case 's': Ctemp->Element.string.x+=dl-x;Ctemp->Element.string.y+=dl-y;break;...}}三、文件功能的实现存盘时,打开文件,写入图形的背景颜色,写入图素个数Elementcount,再将内存链表中各图素的属性值依次写入文件即可。读盘时,在内存中动态建立图素链表,将文件中的图素属性值依次放入链表中,再根据背景颜色、图素属性值在屏幕上显示图形。存盘过程实现如下。savefile(char * filename){FILE *fp;int i;List-temp=List-head;Eid=0;if((fp=fopen(filename,"w+b"))==NULL){printf ("%s", "Cant't open the file ");exit(1);}fwrite(&back-color, sizeof(char),1,fp);fwrite(&Elementcount,sizeof(int),1,fp);for(i=0;i<Elementcount;i++){ List-temp->ElementID=Eid;fwrite(List-temp,sizeof(ElementList),1,fp);List-temp=List-temp->next;Eid++;}fclose(fp);}四、应用程序编程接口应用程序编程接口主要功能是读图形文件并显示,对画面图素进行动态刷新。这些接口均以函数形式出现,供控制应用程序调用。1.draw-chart (char * filename)功能:读图形文件,在内存中建立图素链表,显示图形。2.change-chart(int Element-ID, int how)功能:改变图素Element-ID的特性,怎样改变由how决定。该接口能方便地实现图形的动态刷新。3.clear-chart( )功能:释放图素链表占用的内存。4.draw ( char * filename)功能:不建立链表,边读图形文件,边显示。该函数不占用内存,适用于图素多、数据文件较大,而又不需动态刷新的图形画面显示。 作者:汪建平 陆志才
文章搜索
关键字:
Copyright ©2005 - 2006 Lwen.Net      广告合作—QQ 1573351 Email:6366083#163.com(#换成@)