|
我想用cairo绘制折线图,x坐标每次增加30,y坐标随机产生,每1秒刷新一次图像,但现在每次刷新使上次画布的东西都清空了,怎么回事呢
#include <cairo.h>
#include <gtk/gtk.h>
#include <glib.h>
gint x = 0, y = 0;
cairo_t *cr;
gint update(gpointer data) {
gtk_widget_queue_draw((GtkWidget*) data);
}
static gboolean
on_expose_event(GtkWidget *widget,
GdkEventExpose *event,
gpointer data) {
GRand *rand;
gint x1 = 0, y1 = 0;
rand = g_rand_new();
cr = gdk_cairo_create(widget->window);
cairo_set_source_rgb(cr, 0, 0, 0);
cairo_set_line_width(cr, 2.0);
cairo_move_to(cr, 100, 450);
cairo_line_to(cr, 700, 450);
cairo_move_to(cr, 100, 450);
cairo_line_to(cr, 100, 50);
cairo_move_to(cr, x1, y1);
x += 30;
y = g_rand_int_range(rand, 55, 400);
cairo_line_to(cr, x, y);
x1 = x;
y1 = y;
cairo_stroke(cr);
//cairo_set_source_rgb(cr, 0, 0, 0);
//cairo_arc(cr, x, y, 2, 0, 2 * 3.14);
//cairo_clip(cr);
//cairo_rectangle(cr, 0, 0, 256, 256);
//cairo_fill(cr);
cairo_select_font_face(cr, "urisa",
CAIRO_FONT_SLANT_NORMAL,
CAIRO_FONT_WEIGHT_BOLD);
cairo_set_font_size(cr, 13);
cairo_move_to(cr, 10, 50);
cairo_show_text(cr, "温度(单位:℃)");
cairo_move_to(cr, 700, 475);
cairo_show_text(cr, "时间(单位:s)");
cairo_stroke(cr); //描绘轮廓
// cairo_restore(cr);
cairo_destroy(cr); //销毁画笔
return FALSE;
}
int main(int argc, char *argv[]) {
GtkWidget *window;
gtk_init(&argc, &argv);
window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
g_signal_connect(window, "expose-event",
G_CALLBACK(on_expose_event), NULL);
g_signal_connect(window, "destroy",
G_CALLBACK(gtk_main_quit), NULL);
gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
gtk_window_set_default_size(GTK_WINDOW(window), 800, 500);
gtk_widget_set_app_paintable(window, TRUE);
gtk_timeout_add(1000, update, window);
gtk_widget_show_all(window);
gtk_main();
return 0;
} |
|