DOS圖形編輯軟件設計論文

時間:2022-03-20 02:22:00

導語:DOS圖形編輯軟件設計論文一文來源于網友上傳,不代表本站觀點,若需要原創文章可咨詢客服老師,歡迎參考。

DOS圖形編輯軟件設計論文

一、數據結構與數據文件格式

由于所有的操作都基本建立在圖素的基礎之上,故數據結構也以圖素為中心。以下以圓、直線、矩形、字符串為例,其它圖素類似論文

1.定義所需圖素

structcircle/*定義圓*/

{

intx,y,r;/*圓心,半徑*/

charlinecolor,linestyle;/*圓外圍線的顏色,線型*/

charfillcolor,fillstyle;/*填充顏色,模式*/

};

structline/*定義直線*/

{

intx1,y1;

intx2,y2;

charlinecolor,linestyle,linethick;/*線顏色,模式,粗細*/

};

structbox/*定義矩形*/

{

intx1,y1;

intx2,y2;

charlinecolor,linestyle;

charfillcolor,fillstyle;

};

structstring/*定義字符串*/

{

intx,y;

charstr[10]

charbackcolor,dir;

charstr-color,str-style;

};

.

./*定義其它圖素*/

.

2.將各圖素置于一條鏈表之中

typedefstructtagElementList

{

charElementType;/*標識元素類別*/

intElementID;/*元素標識符,在接口中用來控制其屬性*/

uniontagElement{

structcirclecircle;

structboxbox;

structstringstring;

structlineline;

.

./*可在此說明其它元素*/

.

}Element;

structtagElementList*next;

}ElementList;

利用這種數據結構可在內存中形成一個圖素鏈表,所有操作都可以此鏈表為基礎。

3.定義幾個指針,以備各種操作

ElementList*List-head.*List-end,*List-temp,*List-here;

4.定義一個全局變量,記錄圖素個數

staticintElementcount=0;

圖形文件格式為:第一字節(char),表示整個圖形的背景顏色;接下來一個字(word),對應于Elementcount,表示圖素個數;后面是內存鏈表中每個圖素的屬性值。

二、圖形編輯功能的實現

本軟件包含的圖形編輯功能主要有:作圖、修改、移動、刪除、復制,下面僅舉幾例說明實現的方法。

1.作圖

以圓為例,其它圖形類似。

drawcircle()

{

inti;

chars[20],c;

intcx,cy,cr;

intcls,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()

{

Rectrect;/*定義的矩形*/

inti;

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,intdl-x,intdl-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;

inti;

List-temp=List-head;

Eid=0;

if((fp=fopen(filename,"w+b"))==NULL)

{

printf("%s","Cant''''topenthefile");

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(intElement-ID,inthow)功能:改變圖素Element-ID的特性,怎樣改變由how決定。該接口能方便地實現圖形的動態刷新。

3.clear-chart()功能:釋放圖素鏈表占用的內存。

4.draw(char*filename)功能:不建立鏈表,邊讀圖形文件,邊顯示。該函數不占用內存,適用于圖素多、數據文件較大,而又不需動態刷新的圖形畫面顯示。