我想为我在X11图形窗口上绘制的不同对象添加不同的颜色。如何更改X11图形编程中不同对象的图形颜色?
示例:这是我们用来指定窗口显示颜色以及因此绘制的对象的通用代码。
XAllocNamedColor(display,DefaultColormap(display,screen),“red”,& color,& dummy);
在下面显示的代码中,直线和矩形都以红色显示。有没有办法以红色和矩形显示蓝色的线?我需要在代码中做什么更改?
代码:
#include <stdio.h>
#include <X11/Xlib.h>
#include <unistd.h>
#include <math.h>
#include "time.h"
#include "sys/time.h"
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),
0, 0, 1250, 1200, 5, depth, InputOutput,
visual ,CWBackPixel, &attributes);
XSelectInput(display,window,ExposureMask | KeyPressMask) ;
fontinfo = XLoadQueryFont(display,"6x10");
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);
XFlush(display);
XMapWindow(display,window);
XFlush(display);
while(1){
XNextEvent(display,&event);
switch(event.type){
case Expose:
XDrawLine(display,window,gr_context,800,800, 400, 450);
XDrawRectangle(display,window,gr_context,i+10,j+10, 200, 150);
break;
case KeyPress:
XCloseDisplay(display);
exit(0);
}
}
}
嗨托马斯,谢谢。我也把代码放在了问题中。如您所见,两个结构都以红色显示。我应该对代码进行哪些更改,以红色显示直线并用蓝色显示矩形我们可以这么说吗? – beginner
您可以使用“XChangeGC”更新图形上下文,或者为每种颜色制作两个图形上下文。请记住,通过调用X服务器完成对GC的更改(并且X服务器可以缓存“少数”GC)。 –
谢谢。那么,我无法按照你之前的回答来回答我的后续问题。我在说如果我需要绘制点让我们说使用XDrawPoint(),我只是给像素坐标输入。但是,如果我想打印每个具有特定像素值的点,即R,G,B,如何编写该特定部分的代码,请给我一个提示? – beginner