使用Cairo实时绘制线条

时间:2017-04-28 20:31:38

标签: c gtk3 cairo

zetcode"行"代码,我试图在单击鼠标按钮时实时显示这些行。然后我将点击的功能更改为

static gboolean clicked(GtkWidget *widget, GdkEventButton *event,
    gpointer user_data)
{
    if (event->button == 1) {
        glob.coordx[glob.count] = event->x;
        glob.coordy[glob.count++] = event->y;
        gtk_widget_queue_draw(widget);
    }

    return TRUE;
}

我想它会在每次点击按钮1时显示这些行,但它们根本没有在窗口上绘制。我在这里缺少什么?

2 个答案:

答案 0 :(得分:2)

在绘图上运行的函数do_drawing()在其末尾包含此行:

glob.count = 0;

所以它在全部绘制后清除了数组,因此很难像你一样积累大量的行。

答案 1 :(得分:0)

如果您遵循的是Zetcode示例,则需要更改do_drawing方法。这是通过每次单击实时绘制一个点来实现的。

static void do_drawing(cairo_t *cr)
{
  cairo_set_source_rgb(cr, 0, 0, 0);//Line colour
  cairo_set_line_width(cr, 0.5);//Line width
  cairo_translate(cr, -170, -170);//Shift where line 

  //i is starting point, i+1 is next mouse coordinate 
  int i;
  for (i = 0; i < glob.count - 1; i++ ) {
    cairo_move_to(cr, glob.coordx[i], glob.coordy[i]);
    cairo_line_to(cr, glob.coordx[i+1], glob.coordy[i+1]);
    printf("from x:%f, y:%f\t to: x:%f, y:%f\n",glob.coordx[i],glob.coordy[i], glob.coordx[i+1], glob.coordy[i+1]);
    cairo_stroke(cr);    
  }
}