1 / 21

ปฏิบัติการที่ ๑๒

ปฏิบัติการที่ ๑๒. Graphics in DOS. โปรแกรมกราฟิกส์. ก่อนจะทราบคำสั่งต่างๆ ที่ใช้โปรแกรมกราฟิกส์ คงต้องมาเตรียมวัตถุดิบในที่นี้คือไฟล์ไลบรารี่ต่างๆ ที่จะช่วยเหลือให้เราสามารถเขียนโปรแกรมกราฟิกส์ได้สำเร็จ เราควรต้องตรวจสอบไฟล์ต่อไปนี้

Télécharger la présentation

ปฏิบัติการที่ ๑๒

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. ปฏิบัติการที่ ๑๒ Graphics in DOS

  2. โปรแกรมกราฟิกส์ • ก่อนจะทราบคำสั่งต่างๆ ที่ใช้โปรแกรมกราฟิกส์ คงต้องมาเตรียมวัตถุดิบในที่นี้คือไฟล์ไลบรารี่ต่างๆ ที่จะช่วยเหลือให้เราสามารถเขียนโปรแกรมกราฟิกส์ได้สำเร็จ เราควรต้องตรวจสอบไฟล์ต่อไปนี้ • ไฟล์ไลบราลีที่เราต้องใช้คือ graphics.hมีอยู่ในไดเรกทอรี่ “C:\<BORLAND C++ DIR>\INCLUDE\” • ไฟล์กราฟิกส์ไดรเวอร์ คือไฟล์สกุล .BGIมีอยู่ใน “C:\<BORLAND C++ DIR>\BGI\”

  3. วิธีคอมไพล์และรันโปรแกรมกราฟิกส์วิธีคอมไพล์และรันโปรแกรมกราฟิกส์ • ทำการพิมพ์โปรแกรมกราฟิกส์ลงใน Editor • เลื่อนเมาส์ไปที่หน้าต่าง Editor แล้วคลิกเมาส์ขวาจะมาสปีดเมนูเกิดขึ้นให้เลือก TargetExpert • เมื่อหน้าต่าง TargetExpert ปรากฏให้ดูทางซ้ายมือสุดมี Target Type เลือก Application [.exe] • ในช่อง Platform ให้คลิกเมาส์ที่รายการ DOS • ในกรอบ Libraries ให้คลิกเมาส์ที่ BGI • คลิก OK • กด F9 เพื่อคอมไพล์ และ Ctrl+F9 เพื่อรัน

  4. ลองคอมไพล์และรันกันเลยลองคอมไพล์และรันกันเลย //FirstCircle.cpp #include <graphics.h> #include <conio.h> void main(){ int GrDriver=DETECT, GrMode; initgraph(&GrDriver, &GrMode, "C:\\BC5\\BGI"); setcolor(YELLOW); circle(getmaxx()/2,getmaxy()/2,getmaxy()/2); getch(); closegraph(); }

  5. //DetectGraphicCard.cpp #include <graphics.h> #include <conio.h> #include <iostream.h> char *Driver[] = { "DETECT", "CGA", "MCGA", "EGA", "EGA64", "EGAMONO", "IBM8514", "HERCMONO", "ATT400", "VGA", "IBM3270"}; void main(){ int GrDriver, GrMode, LoMode, HiMode; detectgraph(&GrDriver, &GrMode); getmoderange(GrDriver,&LoMode,&HiMode); clrscr(); cout << "You have " << Driver[GrDriver] << " video display card.\n"; cout << "Graphics driver mode range = " << LoMode << " to " << HiMode; getch(); }

  6. //CheckError.cpp #include <graphics.h> #include <conio.h> #include <iostream.h> void main(){ int GrDriver = DETECT, GrMode, GrError; initgraph(&GrDriver, &GrMode, “/*c:\\bc5\\bgi*/”); GrError = graphresult(); if (GrError!=0) { cout << “\aGRAPHIC ERROR:” << grapherrormsg(GrError); exit(GrError); } setcolor(YELLOW); circle(getmaxx()/2,getmaxy()/2,getmaxy()/2); getch(); closegraph() }

  7. เนื่องจากการตรวจสอบข้อผิดพลาดควรมีทุกโปรแกรมดังนั้นขอละส่วนของโปรแกรมต่อไปนี้ ด้วยข้อความ <<<check result>>> ทุกครั้ง int GrDriver = DETECT, GrMode, GrError; initgraph(&GrDriver, &GrMode, “/*c:\\bc5\\bgi*/”); GrError = graphresult(); if (GrError!=0) { cout << “\aGRAPHIC ERROR:” << grapherrormsg(GrError); exit(GrError); }

  8. //AspectRatio.cpp #include <graphics.h> #include <stdlib.h> #include <conio.h> #include <iostream.h> void main(){ int XAspect, YAspect; <<<check result>>> restorecrtmode(); getaspectratio(&XAspect, &YAspect); cout << "Graph Driver = " << GrDriver << endl; cout << "Graph Mode = " << GrMode << endl; cout << "XAspect = " << XAspect << endl; cout << "YAspect = " << YAspect << endl; cout << "Aspect Ratio = " << XAspect/YAspect << endl; getch(); closegraph(); }

  9. ผลการรัน Graph Driver = 9 Graph Mode = 2 Xaspect = 10000 Yaspect = 10000 Aspect Ratio = 1

  10. //AspectRatio.cpp #include <graphics.h> #include <stdlib.h> #include <conio.h> #include <iostream.h> void main(){ int XAspect, YAspect; <<< check result >>> setcolor(YELLOW); circle(getmaxx()/2,getmaxy()/2,getmaxy()/2); getch(); restorecrtmode(); getaspectratio(&XAspect, &YAspect); cout << "Graph Driver = " << GrDriver << endl; cout << "Graph Mode = " << GrMode << endl; cout << "XAspect = " << XAspect << endl; cout << "YAspect = " << YAspect << endl; cout << "Aspect Ratio = " << XAspect/YAspect << endl; getch(); setgraphmode(GrMode); setcolor(LIGHTRED); circle(getmaxx()/2,getmaxy()/2,getmaxy()/2); getch(); closegraph(); }

  11. //SetViewport.cpp #include <iostream.h> #include <stdlib.h> #include <conio.h> #include <graphics.h> void main(){ int Color,rad = 50; <<<check result>>> setviewport(200,100,getmaxx()-200,getmaxy()-100,1); randomize(); do { Color = random(getmaxcolor()); setcolor(Color); circle( 200, 100, rad ); rad += 20; }while(rad < 200); getch(); closegraph(); }

  12. //SetViewport.cpp #include <iostream.h> #include <stdlib.h> #include <conio.h> #include <graphics.h> int RandomColor(void); void main(){ int Color = 1; <<<check result>>> setviewport(200,100,getmaxx()-200,getmaxy()-100,1); randomize(); do { Color = RandomColor(); setbkcolor(Color); delay(1000); Color = RandomColor(); setcolor(Color); circle( 100, 100, 200 ); delay(1000) }while(!kbhit()); closegraph(); } int RandomColor(void){ int x; do { x = random(getmaxcolor()+1); }while (x==0); return x; }

  13. //PutGetDot.cpp #include <iostream.h> #include <stdlib.h> #include <conio.h> #include <graphics.h> void main(){ int Color; <<<check result>>> for(int y=0;y<=getmaxy();y++) for(int x=0;x<=getmaxx();x++) putpixel(x,y,x%16); getch(); randomize(); do { int X = random(getmaxx()), Y = random(getmaxy()); if (getpixel(X,Y)==RED) putpixel(X,Y,WHITE); }while(!kbhit()); closegraph(); }

  14. //DrawPixel.cpp #include <graphics.h> #include <stdlib.h> #include <conio.h> #include <iostream.h> void main(){ int X,Y; char Ch='x'; int Color=BLACK; setbkcolor(WHITE); X=getmaxx()/2; Y=getmaxy()/2; while(Ch!=27) { moveto(X,Y);putpixel(X,Y,Color); Ch = getch(); if(Ch==0){ // entended code Ch = getch(); switch(Ch){ case 72: --Y; if(Y<0); break; //up arrow case 80: ++Y; if(Y>getmaxy()) Y=getmaxy(); break; //down arrow case 77: ++X; if(X>getmaxx()) X=getmaxx(); break; //right case 75: --X; if(X<0) X=0; break; //left default: cout < '\a'; } } else{ switch(Ch){ case '0':Color=WHITE; break; case '1':Color=BLUE; break; case '2':Color=GREEN; break; case '3':Color=CYAN; break; case '4':Color=RED; break; case '5':Color=MAGENTA; break; case '6':Color=BROWN; break; case '7':Color=LIGHTGRAY; break; case '8':Color=LIGHTRED; break; case '9':Color=YELLOW; break; default: cout << '\a'; } } } closegraph(); }

  15. //Line.cpp #include <iostream.h> #include <stdlib.h> #include <conio.h> #include <graphics.h> void main(){ <<<check result>>> setlinestyle(SOLID_LINE, 0, THICK_WIDTH); setcolor(LIGHTBLUE); line(getmaxx()/2,0,getmaxy()/2,getmaxy()); setlinestyle(USERBIT_LINE, 0x5555, THICK_WIDTH); setcolor(WHITE); line(0,getmaxy()/2,getmaxx(),getmaxy()/2); getch(); closegraph(); }

  16. //Line.cpp #include <iostream.h> #include <stdlib.h> #include <conio.h> #include <graphics.h> void main(){ <<<check result>>> setbkcolor(WHITE); moveto(getmaxx()/2,getmaxy()/2); randomize(); do{ int LineStyle = random(4); int Thick = random(3); int Color = random(getmaxcolor()); int X = random(getmaxx()); int y = random(getmaxy()); setlinestyle(LineStyle,0,Thick); setcolor(Color); lineto(X,Y); Ch = getch(); if (Ch==0) if (getch()==27) break; }while(1); closegraph(); }

  17. //Rectangle.cpp #include <iostream.h> #include <stdlib.h> #include <conio.h> #include <graphics.h> void main(){ <<<check result>>> setbkcolor(WHITE); setcolor(RED); setlinestyle(2,0,3); rectangle(20,20,200,180); getch(); closegraph(); }

  18. แบบฝึกหัด • จงวาดเขียนโปรแกรมที่ทำการวาดรูปแล้วมีลักษณะของ animation เช่น มีรูปสี่เหลี่ยมแสดงสักระยะแล้วค่อยๆ เลื่อนมาทางด้านล่าง เช่นนี้เป็นต้น

  19. //Circle.cpp #include <iostream.h> #include <stdlib.h> #include <conio.h> #include <graphics.h> void main(){ <<<check result>>> setbkcolor(WHITE); setcolor(RED); setlinestyle(2,0,3); circle(200,200,80); getch(); closegraph(); }

  20. //Circle.cpp #include <iostream.h> #include <stdlib.h> #include <conio.h> #include <graphics.h> void main(){ <<<check result>>> setbkcolor(WHITE); setcolor(RED); setlinestyle(2,0,3); arc(getmaxx()/2,getmaxy()/2,135,225,80); getch(); closegraph(); }

  21. //Circle.cpp #include <iostream.h> #include <stdlib.h> #include <conio.h> #include <graphics.h> void main(){ <<<check result>>> setbkcolor(WHITE); setcolor(RED); setlinestyle(2,0,3); ellipse(getmaxx()/2,getmaxy()/2,135,225,80,100); getch(); closegraph(); }

More Related