How to use Graphics in XWindow

1.       Use the following codes as the mainframe of your program.

 

#include <X11/Xlib.h>

#include <X11/Xutil.h>

#include <stdio.h>

#include <unistd.h>

 

#define WIN_WIDTH   640

#define WIN_HEIGHT  480

 

unsigned long GetColor( Display* dis, char* color_name )

{

    Colormap cmap;

    XColor near_color, true_color;

 

    cmap = DefaultColormap( dis, 0 );

    XAllocNamedColor( dis, cmap, color_name, &near_color, &true_color );

    return( near_color.pixel );

}

 

int main( void )

{

    Display* dis;

    Window win;

    XSetWindowAttributes att;

    GC gc;

    XEvent ev;

 

    int t;

 

    dis = XOpenDisplay( NULL );

    win = XCreateSimpleWindow( dis, RootWindow(dis,0), 100, 100,       WIN_WIDTH, WIN_HEIGHT, 5, WhitePixel(dis,0), BlackPixel(dis,0) );

 

    att.backing_store = WhenMapped;

    XChangeWindowAttributes( dis, win, CWBackingStore, &att );

 

    XSelectInput( dis, win, ExposureMask );

    XMapWindow( dis, win );

 

    do{

        XNextEvent( dis, &ev);

    }while( ev.type != Expose );

 

    gc = XCreateGC( dis, DefaultRootWindow(dis), 0, 0 );

    XSetFunction( dis, gc, GXxor );

    XSetForeground( dis, gc, BlackPixel(dis,0)^GetColor( dis, "blue")  );

 

    XFillRectangle( dis, win, gc, 0, 0, 50, 50 );

    XDrawLine( dis, win, gc, 50, 50, 100, 100);

    XDrawArc( dis, win, gc, 150, 150, 50, 50, 0, 360*64);

    XDrawRectangle( dis, win, gc, 10, 280, 80, 40);

    XFillArc( dis, win, gc, 300, 300, 20, 20, 0, 360*64);

    XDrawPoint( dis, win, gc, 500, 50);

 

    for (int t = 0; t < 300; t++)

        usleep(100000);

    XDestroyWindow( dis , win );

    XCloseDisplay( dis );

    return(0);

}

2.       Replace those codes marked with red with your own codes. All those graphic functions will be described at the end of this file.

3.       Save the file.

4.       Compile the file with command “g++ xxx.cpp –L/usr/X11R6/Lib –lX11”, in which xxx is the name of your cpp file. Be careful that the command is case-sensitive.

5.       Run the program with command “a.out

 

Description of Graphics Functions

XDrawPoint(display, d, gc, x, y)

      Display *display;
      Drawable d;
      GC gc;
      int x, y;
 

display

Specifies the connection to the X server.

d

Specifies the drawable.

gc

Specifies the GC.

x
y

Specify the x and y coordinates where you want the point drawn.

XDrawLine(display, d, gc, x1, y1, x2, y2)

      Display *display;
      Drawable d;
      GC gc;
      int x1, y1, x2, y2;

display

Specifies the connection to the X server.

d

Specifies the drawable.

gc

Specifies the GC.

x1
y1
x2
y2

Specify the points (x1, y1) and (x2, y2) to be connected.

XDrawArc(display, d, gc, x, y, width, height, angle1, angle2)

      Display *display;
      Drawable d;
      GC gc;
      int x, y;
      unsigned int width, height;
      int angle1, angle2;

display

Specifies the connection to the X server.

d

Specifies the drawable.

gc

Specifies the GC.

x

.ns

y

Specify the x and y coordinates, which are relative to the origin of the drawable and specify the upper-left corner of the bounding rectangle.

width
height

Specify the width and height, which are the major and minor axes of the arc.

angle1

Specifies the start of the arc relative to the three-o'clock position from the center, in units of degrees * 64.

angle2

Specifies the path and extent of the arc relative to the start of the arc, in units of degrees * 64.

XDrawRectangle(display, d, gc, x, y, width, height)

      Display *display;
      Drawable d;
      GC gc;
      int x, y;
      unsigned int width, height;

display

Specifies the connection to the X server.

d

Specifies the drawable.

gc

Specifies the GC.

x
y

Specify the x and y coordinates, which specify the upper-left corner of the rectangle.

width
height

Specify the width and height, which specify the dimensions of the rectangle.

XFillArc(display, d, gc,  x, y, width, height, angle1, angle2)

      Display *display;
      Drawable d;
      GC gc;
      int x, y;
      unsigned int width, height;
      int angle1, angle2;

display

Specifies the connection to the X server.

d

Specifies the drawable.

gc

Specifies the GC.

x y

Specify the x and y coordinates, which are relative to the origin of the drawable and specify the upper-left corner of the bounding rectangle.

width height

Specify the width and height, which are the major and minor axes of the arc.

angle1

Specifies the start of the arc relative to the three-o'clock position from the center, in units of degrees * 64.

angle2

Specifies the path and extent of the arc relative to the start of the arc, in units of degrees * 64.

XFillRectangle(display, d, gc, x, y, width, height)

      Display *display;
      Drawable d;
      GC gc;
      int x, y;
      unsigned int width, height;

display

Specifies the connection to the X server.

d

Specifies the drawable.

gc

Specifies the GC.

x
y

Specify the x and y coordinates, which are relative to the origin of the drawable and specify the upper-left corner of the rectangle.

width
height

Specify the width and height, which are the dimensions of the rectangle to be filled.