[comp.windows.x] XDrawRectangle - XFillRectangle

csmoe@mtsu.EDU (Monisha Guglani) (02/21/91)

Hi,
	I'm a new user of x and
	I'm having problems trying to use XDrawRectangle and 
	XFillRectangle. It works some times & not others.
	Is there a way to make it work all the time?
	I'm using R4 on a dec-vaxstation 3100. 
	Is this a bug in the code?

	I go through the sequence of opening the window with the
	graphic context and when I attempt to draw into it I get
	no rectangle filled or otherwise & no error message.
	Any help of any kind would be appreciated.


	

	
-- 
/**************************************************************/
/*       csmoe@knuth.mtsu.edu        Monisha Guglani          */	   
/* "I haven't lost my mind its backed-up on a tape somewhere" */
/**************************************************************/

mouse@lightning.mcrcim.mcgill.EDU (02/21/91)

> I'm a new user of x and I'm having problems trying to use
> XDrawRectangle and XFillRectangle.  It works some times & not others.

> I go through the sequence of opening the window with the graphic
> context and when I attempt to draw into it I get no rectangle filled
> or otherwise & no error message.

You don't say how your program is structured.  I suspect the problem is
this one from the FAQ.  (If this isn't the case, and you're properly
waiting for Expose events, I'd have to see the code to guess what's
wrong.)

----------------------------------------------------------------------
Subject: 81)  Why doesn't anything appear when I run this simple program?

> ...
> the_window = XCreateSimpleWindow(the_display,
>      root_window,size_hints.x,size_hints.y,
>      size_hints.width,size_hints.height,BORDER_WIDTH,
>      BlackPixel(the_display,the_screen),
>      WhitePixel(the_display,the_screen));
> ...
> XSelectInput(the_display,the_window,ExposureMask|ButtonPressMask|
> 	ButtonReleaseMask);
> XMapWindow(the_display,the_window);
> ...
> XDrawLine(the_display,the_window,the_GC,5,5,100,100);
> ...

	You are right to map the window before drawing into it. However, the 
window is not ready to be drawn into until it actually appears on the screen --
until your application receives an Expose event. Drawing done before that will 
generally not appear. You'll see code like this in many programs; this code 
would appear after window was created and mapped:
  while (!done)
    {
      XNextEvent(the_display,&the_event);
      switch (the_event.type) {
	case Expose:	 /* On expose events, redraw */
		XDrawLine(the_display,the_window,the_GC,5,5,100,100);
		break;
	...
	}
    }

	Note that there is a second problem: some X servers don't set up the 
default graphics context to have reasonable foreground/background colors, and 
your program should not assume that the server does, so this program could 
previously include this code to prevent the case of having the foreground and 
background colors the same:
  ...
  the_GC_values.foreground=BlackPixel(the_display,the_screen);	/* e.g. */
  the_GC_values.background=WhitePixel(the_display,the_screen);	/* e.g. */
  the_GC = XCreateGC(the_display,the_window,
                GCForeground|GCBackground,&the_GC_values);
  ...
 
Note: the code uses BlackPixel and WhitePixel to avoid assuming that 1 is 
black and 0 is white or vice-versa.  The relationship between pixels 0 and 1 
and the colors black and white is implementation-dependent.  They may be 
reversed, or they may not even correspond to black and white at all.

----------------------------------------------------------------------

					der Mouse

			old: mcgill-vision!mouse
			new: mouse@larry.mcrcim.mcgill.edu

cjmchale@cs.tcd.ie (Ciaran McHale) (02/21/91)

In <m0j96Kd-0006xLC@knuth.mtsu.edu> csmoe@mtsu.EDU (Monisha Guglani) writes:

>Hi,
>	I'm a new user of x and
>	I'm having problems trying to use XDrawRectangle and 
>	XFillRectangle. It works some times & not others.
>	[...]
>	I go through the sequence of opening the window with the
>	graphic context and when I attempt to draw into it I get
>	no rectangle filled or otherwise & no error message.
>	Any help of any kind would be appreciated.

This is in the "frequently asked questions" posting. Here's
the relevant part:

----------------------------------------------------------------------
Subject: 81)  Why doesn't anything appear when I run this simple program?

> ...
> the_window = XCreateSimpleWindow(the_display,
>      root_window,size_hints.x,size_hints.y,
>      size_hints.width,size_hints.height,BORDER_WIDTH,
>      BlackPixel(the_display,the_screen),
>      WhitePixel(the_display,the_screen));
> ...
> XSelectInput(the_display,the_window,ExposureMask|ButtonPressMask|
> 	ButtonReleaseMask);
> XMapWindow(the_display,the_window);
> ...
> XDrawLine(the_display,the_window,the_GC,5,5,100,100);
> ...

	You are right to map the window before drawing into it. However, the 
window is not ready to be drawn into until it actually appears on the screen --
until your application receives an Expose event. Drawing done before that will 
generally not appear. You'll see code like this in many programs; this code 
would appear after window was created and mapped:
  while (!done)
    {
      XNextEvent(the_display,&the_event);
      switch (the_event.type) {
	case Expose:	 /* On expose events, redraw */
		XDrawLine(the_display,the_window,the_GC,5,5,100,100);
		break;
	...
	}
    }

	Note that there is a second problem: some X servers don't set up the 
default graphics context to have reasonable foreground/background colors, and 
your program should not assume that the server does, so this program could 
previously include this code to prevent the case of having the foreground and 
background colors the same:
  ...
  the_GC_values.foreground=BlackPixel(the_display,the_screen);	/* e.g. */
  the_GC_values.background=WhitePixel(the_display,the_screen);	/* e.g. */
  the_GC = XCreateGC(the_display,the_window,
                GCForeground|GCBackground,&the_GC_values);
  ...
 
Note: the code uses BlackPixel and WhitePixel to avoid assuming that 1 is 
black and 0 is white or vice-versa.  The relationship between pixels 0 and 1 
and the colors black and white is implementation-dependent.  They may be 
reversed, or they may not even correspond to black and white at all.
----------------------------------------------------------------------


Ciaran.
-- 
Ciaran McHale		"Verbosity says it all"			      ____
Department of Computer Science, Trinity College, Dublin 2, Ireland.   \  /
Telephone: +353-1-772941 ext 1538	FAX: +353-1-772204	       \/
Telex: 93782 TCD EI			email: cjmchale@cs.tcd.ie