Thread: Graphics in c++ not working

  1. #1
    Registered User
    Join Date
    May 2019
    Posts
    47

    Exclamation Graphics in c++ not working

    Hello friends
    I am new to c++ and i have installed graphicsin codeblocks
    following this tutorial How to include graphics.h in CodeBlocks? | Code with C

    but when i try to run this code ..it shows blank screen instead of 3 lines
    Code:
    #include<graphics.h>using namespace std;
    
    
    
    
    
    
    int main()
    {
       // gm is Graphics mode which is a computer display
        // mode that generates image using pixels.
        // DETECT is a macro defined in "graphics.h" header file
      int gd =DETECT,gm;
    
    
      // initgraph initializes the graphics system
        // by loading a graphics driver from disk
    
    
      initgraph(&gd,&gm,"");
    
    
      // line for x1, y1, x2, y2
        line(150, 150, 450, 150);
    
    
        // line for x1, y1, x2, y2
        line(150, 200, 450, 200);
    
    
        // line for x1, y1, x2, y2
        line(150, 250, 450, 250);
    
    
        getch();
    
    
        // closegraph function closes the graphics
        // mode and deallocates all memory allocated
        // by graphics system .
        closegraph();
    }
    any help will be appreciated..thanks

  2. #2
    Registered User
    Join Date
    Dec 2017
    Posts
    1,664
    Presumably gm is supposed to be set to some value (try 0).
    And maybe the color needs to be set, e.g., setcolor(2).

    You should also put this after the call to initgraph to see if it succeeded.
    Code:
       int errorcode = graphresult();
       if (errorcode != grOk) { /* an error occurred */
          printf("Graphics error: %s\n", grapherrormsg(errorcode));
          printf("Press any key to halt:");
          getch();
          exit(1);
       }
    Borland Graphics Interface (BGI) Documentation

    But this is an ancient graphics library.
    It would be better to use a modern graphics library like cairo: cairographics.org
    One advantage of cairo is that (IIRC) it can be used in html to draw on a <canvas> element with javascript.
    Last edited by john.c; 12-31-2019 at 05:43 PM.
    All truths are half-truths. - A.N. Whitehead

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 5
    Last Post: 04-28-2017, 01:21 PM
  2. Replies: 4
    Last Post: 02-25-2016, 08:49 AM
  3. Replies: 9
    Last Post: 03-30-2009, 04:09 AM
  4. graphics in c
    By asahin11 in forum C Programming
    Replies: 16
    Last Post: 05-31-2005, 12:25 PM
  5. Graphics with Dev 6.0 C++
    By gabaji123 in forum C++ Programming
    Replies: 1
    Last Post: 05-19-2002, 02:33 AM

Tags for this Thread