home *** CD-ROM | disk | FTP | other *** search
/ Amiga ACS 1998 #2 / amigaacscoverdisc1998-021998.iso / utilities / shareware / dev / libx11 / graphics_examples / class2.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-10-31  |  12.4 KB  |  647 lines

  1.  
  2. /* 
  3.  
  4.  
  5.  
  6.   Program Two
  7.   Bert Nelson
  8.   Copyright 1992 Bert Nelson 
  9.  
  10.   Purpose:  Draw a triangle with different line styles and widths
  11.             Draw a colored Rectangle
  12.             Draw a filled Octagon
  13.             Draw a Circle
  14.             Draw a Pie Chart with a different pattern in each slice
  15.             Draw a series of colored arcs to get a "rainbow" effect
  16.  
  17.  
  18.   Compile Instructions:  cc file.c -lX11 -lm -o file
  19.  
  20.   Run Instructions:  type 'file' after compiling and click a button
  21.                      on the mouse to draw (or redraw) the figures
  22.  
  23.  
  24.  
  25.    Permission to use, copy, modify, and distribute this software and its
  26.    documentation for any purpose and without fee is hereby granted,
  27.    provided that the above copyright notice appear in all copies and that
  28.    both that copyright notice and this permission notice appear in
  29.    supporting documentation.  It is provided "as is" without any express or
  30.    implied warranty.
  31.  
  32.    Bert Nelson DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING
  33.    ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO EVENT SHALL
  34.    Bert Nelson BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR
  35.    ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS,
  36.    WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION,
  37.    ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS
  38.    SOFTWARE.
  39.  
  40.  
  41. */
  42.  
  43.  
  44. #include <X11/Xlib.h>
  45. #include <X11/Xutil.h>
  46. #include <math.h>
  47.  
  48. #define PI 3.141592654
  49. #define stipple_width 16
  50. #define stipple_height 16
  51.  
  52. char hello[] = "Program Two - by Bert Nelson";
  53. char hi[]    = "Hi!";
  54. char tri_str[] = "Triangle";
  55. char rect_str[] = "Rectangle";
  56. char oct_str[] = "Octagon";
  57. char circ_str[] = "Circle";
  58. char sect_str[] = "Sectors";
  59. char arcs_str[] = "Arcs";
  60. Font font;
  61. static char *ncolors[9] = { 
  62.     " ", "blue","red","green4",
  63.     "chocolate4","gold1","firebrick",
  64.     "magenta1","DarkOrchid4"};
  65.  
  66. unsigned long fore,back;
  67.  
  68. Colormap color_map;
  69. XColor colors[8];
  70. XColor exact_colors[8];
  71.  
  72. Pixmap stipple_pixmap1;
  73. Pixmap stipple_pixmap2;
  74. Pixmap stipple_pixmap3;
  75. Pixmap stipple_pixmap4;
  76. Pixmap stipple_pixmap5;
  77. Pixmap stipple_pixmap6;
  78. Pixmap stipple_pixmap7;
  79.  
  80.  
  81.  
  82. main(argc,argv)
  83. int argc;
  84. char **argv;
  85. {
  86.     Display *mydisplay;
  87.     Window  mywindow;
  88.     GC mygc;
  89.     XEvent myevent;
  90.     KeySym mykey;
  91.     XSizeHints myhint;
  92.  
  93.     int myscreen;
  94.     unsigned long myforeground, mybackground;
  95.     int i;
  96.     char text[10];
  97.     int done;
  98.  
  99.  
  100.  
  101.     mydisplay = XOpenDisplay("");
  102.  
  103.     myscreen = DefaultScreen (mydisplay);
  104.     back = mybackground = WhitePixel (mydisplay, myscreen);
  105.     fore = myforeground = BlackPixel (mydisplay, myscreen);
  106.  
  107.     myhint.x = 0;
  108.     myhint.y = 0;
  109.  
  110.     myhint.width = 630;
  111.     myhint.height = 425;
  112.     myhint.flags = PPosition | PSize;
  113.     mywindow = XCreateSimpleWindow (mydisplay,
  114.         DefaultRootWindow (mydisplay),
  115.         myhint.x, myhint.y, myhint.width, myhint.height,
  116.         5, myforeground, mybackground);
  117.  
  118.     XSetStandardProperties (mydisplay, mywindow, hello, hello,
  119.         None, argv, argc, &myhint);
  120.  
  121.     mygc = XCreateGC (mydisplay, mywindow, 0, 0);
  122.  
  123.  
  124.     font = XLoadFont(mydisplay,"8x16");
  125.     XSetFont(mydisplay,mygc,font);
  126.  
  127.  
  128.     XSetBackground (mydisplay, mygc, mybackground);
  129.     XSetForeground (mydisplay, mygc, myforeground);
  130.  
  131.     XSelectInput (mydisplay, mywindow,
  132.         ButtonPressMask | KeyPressMask | ExposureMask);
  133.  
  134.     XMapRaised (mydisplay, mywindow);
  135.  
  136.     color_set_up(mydisplay);
  137.  
  138.  
  139.     done = 0;
  140.     while (done == 0) {
  141.         XNextEvent (mydisplay, &myevent );
  142.         switch (myevent.type) {
  143.  
  144.         case Expose:
  145.             if (myevent.xexpose.count == 0)
  146.  
  147.                 break;
  148.  
  149.  
  150.         case MappingNotify:
  151.             XRefreshKeyboardMapping (&myevent);
  152.             break;
  153.  
  154.         case ButtonPress:
  155.  
  156.             triangle(mydisplay,mywindow,mygc);
  157.             rect(mydisplay,mywindow,mygc);
  158.             oct(mydisplay,mywindow,mygc);
  159.             circ(mydisplay,mywindow,mygc);
  160.             sectors(mydisplay,mywindow,mygc);
  161.             arcs(mydisplay,mywindow,mygc);
  162.             
  163.             break;
  164.  
  165.  
  166.         case KeyPress:
  167.             i = XLookupString (&myevent, text, 10, &mykey, 0);
  168.             if (i == 1 && text[0] == 'q')
  169.                 done = 1;
  170.             break;
  171.  
  172.  
  173.         } /* switch */
  174.  
  175.  
  176.     } /* while (done == 0) */
  177.  
  178.     XFreeGC (mydisplay, mygc);
  179.     XDestroyWindow (mydisplay, mywindow);
  180.     XCloseDisplay (mydisplay);
  181.  
  182.     exit(0);
  183.  
  184. } /* main */
  185.  
  186.  
  187. /* Draw a Triangle */
  188.  
  189. triangle(the_display,the_window,the_gc)
  190. Display *the_display;
  191. Drawable the_window;
  192. GC the_gc;
  193.  
  194. {
  195.  
  196.     XPoint points[4] ;
  197.  
  198.  
  199.     points[0].x = 100;
  200.     points[0].y = 100;
  201.     points[1].x = 50;
  202.     points[1].y = 200;
  203.     points[2].x = 150;
  204.     points[2].y = 200;
  205.     points[3].x = 100;
  206.     points[3].y = 100;
  207.  
  208.  
  209.     XSetLineAttributes(the_display,the_gc,1,LineSolid,CapButt,JoinMiter);
  210.  
  211.     XDrawLine(the_display,the_window,the_gc,100,100,50,200);
  212.  
  213.     XSetLineAttributes(the_display,the_gc,3,
  214.         LineOnOffDash,CapButt,JoinMiter);
  215.  
  216.     XDrawLine(the_display,the_window,the_gc,50,200,150,200);
  217.  
  218.     XSetLineAttributes(the_display,the_gc,5,
  219.         LineDoubleDash,CapButt,JoinMiter);
  220.  
  221.     XDrawLine(the_display,the_window,the_gc,150,200,100,100);
  222.  
  223.     XDrawImageString(the_display,the_window,the_gc,70,220,
  224.         tri_str,strlen(tri_str));
  225.  
  226.     /* reset back to normal line atrributes */
  227.  
  228.     XSetLineAttributes(the_display,the_gc,0,LineSolid,CapButt,JoinMiter);
  229.  
  230. }
  231.  
  232.  
  233. /* Draw a blue rectangle */
  234.  
  235.  
  236. rect(the_display,the_window,the_gc)
  237.  
  238.  
  239. Display *the_display;
  240. Drawable the_window;
  241. GC the_gc;
  242. {
  243.  
  244.     XSetForeground(the_display,the_gc,colors[1].pixel);
  245.     XDrawRectangle(the_display,the_window,the_gc,225,100,150,100);
  246.     reset_colors(the_display,the_gc);
  247.  
  248.     XDrawImageString(the_display,the_window,the_gc,265,85,                
  249.         rect_str,strlen(rect_str));
  250. }
  251.  
  252.  
  253. /* Draw a filled octagon that is red */
  254.  
  255.  
  256. oct(the_display,the_window,the_gc)
  257.  
  258.  
  259. Display *the_display;
  260. Drawable the_window;
  261. GC the_gc;
  262.  
  263. {
  264.  
  265.     XPoint points[9];
  266.  
  267.     points[0].x = 517;
  268.     points[0].y = 100;
  269.     points[1].x = 550;
  270.     points[1].y = 133;
  271.     points[2].x = 550;
  272.     points[2].y = 166;
  273.     points[3].x = 516;
  274.     points[3].y = 200;
  275.     points[4].x = 484;
  276.     points[4].y = 200;
  277.     points[5].x = 450;
  278.     points[5].y = 166;
  279.     points[6].x = 450;
  280.     points[6].y = 133;
  281.     points[7].x = 483;
  282.     points[7].y = 100;
  283.     points[8].x = 516;
  284.     points[8].y = 100;
  285.  
  286.     XSetForeground(the_display,the_gc,colors[2].pixel);
  287.  
  288.  
  289.     XDrawLines(the_display,the_window,the_gc,points,9,CoordModeOrigin);
  290.  
  291.     XFillPolygon(the_display,the_window,the_gc,points,
  292.         9,Convex,CoordModeOrigin);
  293.     reset_colors(the_display,the_gc);
  294.  
  295.     XDrawImageString(the_display,the_window,the_gc,470,85,
  296.         oct_str,strlen(oct_str));
  297.  
  298.  
  299. }
  300.  
  301. /* Draw a circle */
  302.  
  303.  
  304. circ(the_display,the_window,the_gc)
  305.  
  306. Display *the_display;
  307. Drawable the_window;
  308. GC the_gc;
  309.  
  310. {
  311.  
  312.     XDrawArc(the_display,the_window,the_gc,50,300,100,100,0,360*64);
  313.  
  314.     XDrawImageString(the_display,the_window,the_gc,75,358,
  315.         circ_str,strlen(circ_str));
  316.  
  317. }
  318.  
  319. /* Draw a pie chart */
  320.  
  321.  
  322. sectors(the_display,the_window,the_gc)
  323.  
  324. Display *the_display;
  325. Drawable the_window;
  326. GC the_gc;
  327.  
  328. {
  329.  
  330.     int i;
  331.     double x;
  332.     double y;
  333.     int index=0;
  334.     int degrees[13];
  335.     int degrees2[13];
  336.     XPoint points[2];
  337.  
  338.     degrees[0] = 0;
  339.     degrees[1] = 20;
  340.     degrees[2] = 25;
  341.     degrees[3] = 85;
  342.     degrees[4] = 90;
  343.     degrees[5] = 175;
  344.     degrees[6] = 180;
  345.     degrees[7] = 215;
  346.     degrees[8] = 220;
  347.     degrees[9] = 295;
  348.     degrees[10] = 300;
  349.     degrees[11] = 355;
  350.     degrees[12] = 0;
  351.  
  352.     points[0].x = 300;
  353.     points[0].y = 350;
  354.  
  355.  
  356.  
  357.     for( i=0; i<12; i++)
  358.  
  359.     {
  360.  
  361.         
  362.  
  363.         x = cos(degrees[i] * (PI/180)) * 50;
  364.         y = sin(degrees[i] * (PI/180)) * 50;
  365.         y = -1.0 * y;
  366.  
  367.  
  368.         points[1].x = x + 300;
  369.         points[1].y = y + 350;
  370.  
  371.  
  372.         if (i == 0 || (i%2) == 0)
  373.         {
  374.  
  375.             index++;
  376.  
  377.             change_stipple(the_display,the_window,the_gc);
  378.  
  379.             XSetForeground(the_display,the_gc,colors[index].pixel);
  380.  
  381.  
  382.             XDrawArc(the_display,the_window,the_gc,
  383.                 250,300,100,100,degrees[i] * 64,
  384.                 (degrees[i+1] - degrees[i])*64);
  385.  
  386.             XFillArc(the_display,the_window,the_gc,
  387.                 250,300,100,100,degrees[i] * 64, 
  388.                 (degrees[i+1] - degrees[i])*64);
  389.  
  390.         }
  391.  
  392.  
  393.  
  394.         XDrawLines(the_display,the_window,the_gc,
  395.             points,2,CoordModeOrigin);
  396.  
  397.     } /* end for loop */
  398.  
  399.     reset_colors(the_display,the_gc);
  400.  
  401.     XDrawImageString(the_display,the_window,the_gc,275,285,sect_str,
  402.         strlen(sect_str));
  403.  
  404.  
  405. }
  406.  
  407. /* Draw four arcs; each one being a different color */
  408.  
  409.  
  410. arcs(the_display,the_window,the_gc)
  411.  
  412. Display *the_display;
  413. Drawable the_window;
  414. GC the_gc;
  415.  
  416. {
  417.  
  418.     XSetStipple(the_display,the_gc,stipple_pixmap7);
  419.  
  420.  
  421.     XSetForeground(the_display,the_gc,colors[1].pixel);
  422.  
  423.     XDrawArc(the_display,the_window,the_gc,460,350,100,100,0,180*64);
  424.  
  425.     XSetForeground(the_display,the_gc,colors[6].pixel );
  426.  
  427.     XDrawArc(the_display,the_window,the_gc,470,360,80,80,0,180*64);
  428.  
  429.     XSetForeground(the_display,the_gc,colors[2].pixel);
  430.  
  431.     XDrawArc(the_display,the_window,the_gc,480,370,60,60,0,180*64);
  432.  
  433.     XSetForeground(the_display,the_gc,colors[5].pixel);
  434.  
  435.     XDrawArc(the_display,the_window,the_gc,490,380,40,40,0,180*64);
  436.  
  437.     reset_colors(the_display,the_gc);
  438.  
  439.     XDrawImageString(the_display,the_window,the_gc,
  440.         490,340,arcs_str,strlen(arcs_str)+1);
  441.  
  442.  
  443. }
  444.  
  445.  
  446. /* set up colors */
  447.  
  448. color_set_up(the_display)
  449.  
  450. Display *the_display;
  451.  
  452. {
  453.  
  454.     int i;
  455.  
  456.     color_map = DefaultColormap(the_display,0);
  457.  
  458.  
  459.     for (i=1; i<9; i++)
  460.     {
  461.  
  462.         XAllocNamedColor(the_display,color_map,
  463.             ncolors[i],&exact_colors[i],
  464.                 &colors[i]);
  465.  
  466.     }
  467.  
  468. }
  469.  
  470. reset_colors(display,gc)
  471.  
  472. Display *display;
  473. GC gc;
  474.  
  475.  
  476. {
  477.  
  478.     XSetForeground(display,gc,fore);
  479.  
  480. }
  481.  
  482. /* change stipple pattern */
  483.  
  484. change_stipple(the_display,the_window,the_gc)
  485.  
  486. Display *the_display;
  487. Window the_window;
  488. GC the_gc;
  489.  
  490. {
  491.  
  492.  
  493.     /* set up stipple patterns in Hexadecimal */
  494.  
  495.  
  496.     static char stipple_bits1[] = {
  497.  
  498.  
  499.         0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00,
  500.         0xff, 0xff, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0xff, 0xff,
  501.         0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00,
  502.         0xff, 0xff, 0x00, 0x00, 0xff, 0xff, 0x00, 0x00, 0xff, 0xff    };
  503.  
  504.     static char stipple_bits2[] = {
  505.  
  506.         0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
  507.         0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
  508.         0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01,
  509.         0x02, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01, 0x01     };
  510.  
  511.     static char stipple_bits3[] = {
  512.  
  513.         0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03,
  514.         0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03,
  515.         0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03,
  516.         0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03, 0x03     };
  517.  
  518.  
  519.     static char stipple_bits4[] = {
  520.  
  521.         0x01, 0xff, 0x01, 0xff, 0x01, 0xff, 0x01, 0xff, 0x01, 0xff,
  522.         0x01, 0xff, 0x01, 0xff, 0x01, 0xff, 0x01, 0xff, 0x01, 0xff,
  523.         0x01, 0xff, 0x01, 0xff, 0x01, 0xff, 0x01, 0xff, 0x01, 0xff,
  524.         0x01, 0xff, 0x01, 0xff, 0x01, 0xff, 0x01, 0xff, 0x01, 0xff     };
  525.  
  526.  
  527.     static char stipple_bits5[] = {
  528.  
  529.         0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99,
  530.         0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99,
  531.         0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99,
  532.         0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99, 0x99     };
  533.  
  534.  
  535.     static char stipple_bits6[] = {
  536.  
  537.         0x02, 0x02, 0xff, 0xff, 0x02, 0x02, 0xff, 0xff, 0x02, 0x02,
  538.         0xff, 0xff, 0x02, 0x02, 0xff, 0xff, 0x02, 0x02, 0xff, 0xff,
  539.         0x02, 0x02, 0xff, 0xff, 0x02, 0x02, 0xff, 0xff, 0x02, 0x02,
  540.         0xff, 0xff, 0x02, 0x02, 0xff, 0xff, 0x02, 0x02, 0xff, 0xff     };
  541.  
  542.     static char stipple_bits7[] = {
  543.  
  544.         0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  545.         0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  546.         0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff,
  547.         0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff     };
  548.  
  549.  
  550.     static int i=0;
  551.  
  552.  
  553.     i++;
  554.  
  555.  
  556.     if (i > 6)
  557.         i = 0;
  558.  
  559.     stipple_pixmap7 = XCreateBitmapFromData(the_display, 
  560.         the_window,stipple_bits7,stipple_width,stipple_height);
  561.  
  562.     /* for each time this function is called create a different
  563.         stipple pattern */
  564.  
  565.  
  566.  
  567.     if (i == 1)
  568.  
  569.     {
  570.  
  571.         stipple_pixmap1 = XCreateBitmapFromData(the_display,    
  572.             the_window,stipple_bits1,stipple_width,stipple_height);
  573.  
  574.         XSetStipple(the_display,the_gc,stipple_pixmap1);
  575.  
  576.     }
  577.  
  578.  
  579.     if (i == 2)
  580.  
  581.     {
  582.  
  583.         stipple_pixmap2 = XCreateBitmapFromData(the_display,
  584.             the_window,stipple_bits2,stipple_width,stipple_height);
  585.  
  586.         XSetStipple(the_display,the_gc,stipple_pixmap2);
  587.  
  588.  
  589.     }
  590.  
  591.  
  592.  
  593.     if (i==3)
  594.  
  595.  
  596.     {
  597.  
  598.         stipple_pixmap3 = XCreateBitmapFromData(the_display,
  599.             the_window,stipple_bits3,stipple_width,stipple_height);
  600.  
  601.         XSetStipple(the_display,the_gc,stipple_pixmap3);
  602.  
  603.     }
  604.  
  605.  
  606.  
  607.     if (i == 4)
  608.  
  609.  
  610.     {
  611.  
  612.         stipple_pixmap4 = XCreateBitmapFromData(the_display,
  613.             the_window,stipple_bits4,stipple_width,stipple_height);
  614.  
  615.         XSetStipple(the_display,the_gc,stipple_pixmap4);
  616.  
  617.     }
  618.  
  619.  
  620.     if (i == 5)
  621.  
  622.  
  623.     {
  624.  
  625.         stipple_pixmap5 = XCreateBitmapFromData(the_display,
  626.             the_window,stipple_bits5,stipple_width,stipple_height);
  627.  
  628.         XSetStipple(the_display,the_gc,stipple_pixmap5);
  629.  
  630.     }
  631.  
  632.  
  633.     if (i==6)
  634.  
  635.     {
  636.         stipple_pixmap6 = XCreateBitmapFromData(the_display,
  637.             the_window,stipple_bits6,stipple_width,stipple_height);
  638.  
  639.         XSetStipple(the_display,the_gc,stipple_pixmap6);
  640.  
  641.     }
  642.  
  643.  
  644.     XSetFillStyle(the_display,the_gc,FillStippled);
  645.  
  646. }
  647.