In case of X11 display programming, I was wondering how to erase a structure from window once it is displayed. Let's say I want to draw a straight line, rectangle, and circle. However, I want the straight line to be vanished before rectangle is drawn and so on. Is there any function as such to do so?
Code:
#include <stdlib.h>
#include <stdio.h>
#include <X11/Xlib.h>
#include <math.h>
#include "time.h"
#include "sys/time.h"
// Global variables
Display *display;
Window window;
XSetWindowAttributes attributes;
XGCValues gr_values;
XFontStruct *fontinfo;
GC gr_context;
Visual *visual;
int depth;
int screen;
XEvent event;
XColor color, dummy;
main (argc, argv)
char *argv[];
int argc;
{
display = XOpenDisplay(NULL);
screen = DefaultScreen(display);
visual = DefaultVisual(display,screen);
depth = DefaultDepth(display,screen);
attributes.background_pixel = XWhitePixel(display,screen);
window = XCreateWindow( display,XRootWindow(display,screen),
200, 200, 1050, 1200, 5, depth, InputOutput,
visual ,CWBackPixel, &attributes);
XSetStandardProperties(display,window,"Welcome","Hi",None,NULL,0,NULL);
XSelectInput(display,window,ExposureMask | KeyPressMask) ;
fontinfo = XLoadQueryFont(display,"6x10");//6*10
XAllocNamedColor(display, DefaultColormap(display, screen),"red",
&color,&dummy);
gr_values.font = fontinfo->fid;
gr_values.foreground = color.pixel;
gr_context=XCreateGC(display,window,GCFont+GCForeground, &gr_values);
// gr_context2=XCreateGC(display,window,GCFont+GCForeground, &gr_values);
XFlush(display);
XMapWindow(display,window);
XFlush(display);
int i,j,a,b,h,w,angle1,angle2;
while(1){
XNextEvent(display,&event);
switch(event.type){
case Expose:
//Straight line
XDrawLine(display,window,gr_context,800,800, 400, 450);
XFlush(display);
usleep(50000);
// Which function should I write here to make the line vanished??
//Rectangle
XDrawRectangle(display,window,gr_context,102,103, 200, 150);
XFlush(display);
usleep(50000);
//Arc and Circle
a = 600, b = 700;
h = 100, w = 100;
angle1 = 0, angle2 = 360*64;
XDrawArc(display, window, gr_context, a-(w/2), b-(h/2), w, h, angle1, angle2);
XFlush(display);
usleep(50000);
break;
case KeyPress:
XCloseDisplay(display);
exit(0);
}
}
}
Updated Code: Concern - displays twice and then erases the first one
#include <stdlib.h>
#include <stdio.h>
#include <X11/Xlib.h>
#include <math.h>
#include "time.h"
#include "sys/time.h"
// Global variables
Display *display;
Window window;
XSetWindowAttributes attributes;
XGCValues gr_values;
XFontStruct *fontinfo;
GC gr_context;
Visual *visual;
int depth;
int screen;
XEvent event;
XColor color, dummy;
main (argc, argv)
char *argv[];
int argc;
{
display = XOpenDisplay(NULL);
screen = DefaultScreen(display);
visual = DefaultVisual(display,screen);
depth = DefaultDepth(display,screen);
attributes.background_pixel = XWhitePixel(display,screen);
window = XCreateWindow( display,XRootWindow(display,screen),
200, 200, 1050, 1200, 5, depth, InputOutput,
visual ,CWBackPixel, &attributes);
XSetStandardProperties(display,window,"Welcome","Hi",None,NULL,0,NULL);
XSelectInput(display,window,ExposureMask | KeyPressMask) ;
fontinfo = XLoadQueryFont(display,"6x10");
XAllocNamedColor(display, DefaultColormap(display, screen),"cyan",
&color,&dummy);
gr_values.font = fontinfo->fid;
gr_values.foreground = color.pixel;
gr_values.function = GXxor;
gr_context=XCreateGC(display,window,GCFont+GCForeground+GCFunction, &gr_values);
XFlush(display);
XMapWindow(display,window);
XFlush(display);
int i,j,a,b,h,w,angle1,angle2;
while(1){
XNextEvent(display,&event);
switch(event.type){
case Expose:
XDrawLine(display,window,gr_context,800,800, 400, 450);
XFlush(display);
usleep(500000);
XDrawRectangle(display,window,gr_context,102,103, 200, 150);
XFlush(display);
usleep(500000);
break;
case KeyPress:
XCloseDisplay(display);
exit(0);
}
}
}