Monday, 24 August 2015

Paint application in C language

NOTE: Please change the path in the initgraph() function as per your BGI directory.

Sample Output :

Computer graphics, C program for paint application in C

Computer graphics, C program for paint application in C



Program:

#include<graphics.h>
#include<stdio.h>
#include<conio.h>
#include<dos.h>
#include<math.h>
#include"mouse.c"
void main()
{
 int gd=DETECT,gm,i,c=15, x,y,b,errorcode,radius,Circle_flag,c1,c2,cex,cey,Paint_flag;
 int Line_flag=1,Rect_flag,tlx,tly,brx,bry;

 printf("\t\twww.shadowhackit.blogspot.com\n\n");
 printf("ShadowHack's Paint Application\n");


 printf("Instruction:\n");
 printf("1. Use left click on left pane to select from the menus\n");
 printf("2. Draw in the white box. \n");
 printf("NOTE: This program is not yet completed...(Under Construction)\n");
 printf("So FILL option in the menu will not work\n");

 printf("Press any key to continue...\n");
 getch();






 // Graphics Initialisation
 initgraph(&gd,&gm,"C:\\TurboC3\\BGI");
 //Error Detection
 errorcode=graphresult();
 if(errorcode!=grOk)
   printf("graphics error: \nYou might need to check the path of the BGI directory in initgraph()");
  /** Code for menus starts  **/

 // For making Color Pallets
 for(i=0;i<=15;i++)
 {
  rectangle(10,10*i,20,10+10*i);
  setfillstyle(SOLID_FILL,i);
  floodfill(12,10*i+2,WHITE);
 }
  rectangle(35,0,getmaxx(),getmaxy());
 setfillstyle(LINE_FILL,WHITE);

  //bar for 3mm linewidth
  bar(10,170,20,180);
  outtextxy(5,185,"3mm");

 //bar for 3mm linewidth
  bar(10,200,20,210);
  outtextxy(5,215,"1mm");

 // For circle
   setfillstyle(CLOSE_DOT_FILL,WHITE);
   bar(10,230,20,240);
   outtextxy(0,245,"CIR");

  //Paint Box
   setfillstyle(INTERLEAVE_FILL,WHITE);
   bar(10,260,20,270);
   outtextxy(0,275,"Fill");

  //Rectangle
  setfillstyle(INTERLEAVE_FILL,WHITE);
   bar(10,290,20,300);
   outtextxy(0,305,"Rect");

  //Reset
  setfillstyle(SOLID_FILL,RED);
   bar(10,320,20,330);
   outtextxy(5,335,"CLR");



  /** Code for menus starts  **/

 // for white backgorund

 rectangle(35,0,getmaxx(),getmaxy());
   setfillstyle(SOLID_FILL,WHITE);
   floodfill(37,20,WHITE);
   setcolor(BLACK);



 // Untill a key is pressed
 while(!kbhit()){
 showmouseptr();
 getmousepos(&x,&y,&b);


 //****** Left Pane starts ******//
 if(x>10&&x<20){
 if(b==1)//Choose B=2 if you want to make the selections from menus using right click
 {
 // Color Selection
 if(y<160)
 {
 hidemouseptr();
 c=getpixel((int)x,(int)y);
  setcolor(c);
 }

 // Set line Width=3mm
  if(y>170&&y<180)
 {
     hidemouseptr();
     setlinestyle(SOLID_LINE,1,3);
     Line_flag=1;
     Circle_flag=0;
     Paint_flag=0;
     Rect_flag=0;
     showmouseptr();
   }

 // Set line Width=1mm
  if(y>200&&y<210)
  {
     hidemouseptr();
     setlinestyle(SOLID_LINE,1,1);
     Line_flag=1;
     Circle_flag=0;
     Rect_flag=0;
     Paint_flag=0;
     showmouseptr();
   }

   //Selecting Circle
    if(y>230&&y<240)
 {
 radius=20;
 Circle_flag=1;
 Paint_flag=0;
 Line_flag=0;
 Rect_flag=0;
   }

    //Selecting Paint Box
    if(y>260&&y<270)
     {
 hidemouseptr();
 sleep(.1);
 Paint_flag=1;
 Line_flag=0;
 Circle_flag=0;
 Rect_flag=0;
 showmouseptr();
   }

   //Selcting Rectangle
    if(y>290&&y<300)
 {
 hidemouseptr();
 Rect_flag=1;
 Paint_flag=0;
 Line_flag=0;
 Circle_flag=0;
 showmouseptr();
       }

    //Reseting
    if(y>320&&y<330)
       {
  hidemouseptr();
  sleep(.1);
  showmouseptr();
  setfillstyle(SOLID_FILL,15);
  bar(37,0,getmaxx()-2,getmaxy()-2);
  rectangle(37,0,getmaxx()-2,getmaxy()-2);

       }

  }
}
  // ******Left pane ends******


  //Choosing Right Side to Draw
 if((int)x>37&&x<getmaxx()-2&&y>2&& y<getmaxy()-2)

    // ******Code for Left Click ******  // for making drawings
    if(b==1){


      // ** Line Code Starts **
      if(Line_flag==1){
    
      hidemouseptr();// this line of code is very important while mouse going in the upward direction
      moveto((int)x,(int)y);

      // keep drawing while left key is hold
      while(b==1){
 if((int)x>35)
   lineto((int)x,(int)y);
 getmousepos(&x,&y,&b);
       }
     }
     //** Line Code Ends**


     //** Circle Code Starts **
      if(Circle_flag==1){
       c1=x;
       c2=y;
 while(b==1){
 getmousepos(&x,&y,&b);

  radius=(int)sqrt((y-c2)*(y-c2)+(x-c1)*(x-c1));

  if(c1-radius<37)// if while drawing circle we move out of bounds
 // then the circle's radius will get restricted to the boundary
  radius=c1-37;
 }

  hidemouseptr();

       circle(c1,c2,radius);
 showmouseptr();

      }
      //** Circle Code Ends**


     //** Paint Box Code Starts**
     // work on this section of code later
     if(Paint_flag==1){
     setfillstyle(SOLID_FILL,getcolor());

     hidemouseptr();
 floodfill(x,y,WHITE);
     showmouseptr();
     }
     //** Paint Box Code Ends **


     //** Rectangle Code Starts
       if(Rect_flag==1)
       {
 tlx=x;
 tly=y;
 if(x>35)
 while(b==1){
   getmousepos(&x,&y,&b);
   brx=x;
   bry=y;
 }
 if(brx<35)
 brx=36;
 hidemouseptr();
 rectangle(tlx,tly,brx,bry);
 showmouseptr();
       }


     //** Rectangle Code Ends


    }
    //**** Left Click Code Ends ****
 showmouseptr();

 }
 // ****** Key Hit Occured  ******

 getch();
}
//---- Main Ends -----

No comments:

Post a Comment