#include <gtk/gtk.h>
void draw_callback();
int main(int argc, char *argv[])
{
GtkWidget *window;
GtkWidget *da;
gtk_init(&argc, &argv);
window = gtk_window_new (0);
gtk_window_set_title (GTK_WINDOW(window), "Line");
gtk_window_set_default_size (GTK_WINDOW(window), 320, 240);
gtk_container_set_border_width (GTK_CONTAINER(window), 15);
da = gtk_drawing_area_new ();
gtk_container_add (GTK_CONTAINER(window), da);
g_signal_connect (da, "draw",
G_CALLBACK(draw_callback), NULL);
g_signal_connect (window, "destroy",
G_CALLBACK(gtk_main_quit), NULL);
gtk_widget_show_all (window);
gtk_main ();
return 0;
}
void draw_callback(GtkWidget *da, cairo_t *cr)
{
GdkRGBA color;
gdouble dashes[]= {50.0, 10.0, 10.0, 10.0};
guchar num = sizeof(dashes) / sizeof(dashes[0]);
cairo_move_to(cr, 0, 10);
cairo_line_to(cr, 300, 10);
cairo_stroke(cr);
cairo_move_to(cr, 0, 50);
cairo_line_to(cr, 300, 50);
gdk_rgba_parse(&color, "#f00");
gdk_cairo_set_source_rgba(cr, &color);
cairo_set_dash(cr, dashes, num, 0.0);
cairo_set_line_width(cr, 1.0);
cairo_stroke(cr);
cairo_move_to(cr, 0, 90);
cairo_line_to(cr, 300, 90);
cairo_set_dash(cr, dashes, num, 25.0);
gdk_rgba_parse(&color, "#0f0");
gdk_cairo_set_source_rgba(cr, &color);
cairo_stroke(cr);
cairo_move_to(cr, 20, 130);
cairo_line_to(cr, 250, 130);
cairo_set_dash(cr, dashes, 0, 25.0);
gdk_rgba_parse(&color, "#00f");
gdk_cairo_set_source_rgba(cr, &color);
cairo_set_line_cap(cr, CAIRO_LINE_CAP_BUTT);
cairo_set_line_width(cr, 20.0);
cairo_stroke(cr);
cairo_move_to(cr, 20, 170);
cairo_line_to(cr, 250, 170);
gdk_rgba_parse(&color, "#f0f");
gdk_cairo_set_source_rgba(cr, &color);
cairo_set_line_cap(cr,CAIRO_LINE_CAP_ROUND);
cairo_stroke(cr);
cairo_move_to(cr, 20, 200);
cairo_line_to(cr, 250, 200);
gdk_rgba_parse(&color, "#ff0");
gdk_cairo_set_source_rgba(cr, &color);
cairo_set_line_cap(cr, CAIRO_LINE_CAP_SQUARE);
cairo_stroke(cr);
}
#include <gtk/gtk.h>
#include <time.h>
void draw_callback();
int main(int argc, char *argv[])
{
GtkWidget *window;
GtkWidget *da;
gtk_init(&argc, &argv);
srand(time(NULL));
window = gtk_window_new (0);
gtk_window_set_title (GTK_WINDOW(window), "Point");
gtk_window_set_default_size (GTK_WINDOW(window), 320, 240);
gtk_container_set_border_width (GTK_CONTAINER(window), 15);
da = gtk_drawing_area_new ();
gtk_container_add (GTK_CONTAINER(window), da);
g_signal_connect (da, "draw",
G_CALLBACK(draw_callback), NULL);
g_signal_connect (window, "destroy",
G_CALLBACK(gtk_main_quit), NULL);
gtk_widget_show_all (window);
gtk_main ();
return 0;
}
void draw_callback(GtkWidget *da, cairo_t *cr)
{
GdkRGBA color;
guint x = 0, y = 0, width = 0, height = 0;
width = gtk_widget_get_allocated_width (da);
height = gtk_widget_get_allocated_height (da);
gdk_rgba_parse (&color, "#00f");
gdk_cairo_set_source_rgba (cr, &color);
cairo_paint (cr);
gdk_rgba_parse (&color, "#fff");
gdk_cairo_set_source_rgba (cr, &color);
cairo_set_line_width (cr, 1.0);
for (int i = 0; i < 1000; i++)
{
x = rand() % width + 1;
y = rand() % height + 1;
cairo_move_to(cr, x, y);
cairo_line_to(cr, x+1, y);
}
cairo_stroke(cr);
}
#include <gtk/gtk.h>
void draw_callback();
int main(int argc, char *argv[])
{
GtkWidget *window;
GtkWidget *da;
gtk_init(&argc, &argv);
window = gtk_window_new (0);
gtk_window_set_title (GTK_WINDOW(window), "Color");
gtk_window_set_default_size (GTK_WINDOW(window), 410, 240);
gtk_container_set_border_width (GTK_CONTAINER(window), 15);
da = gtk_drawing_area_new ();
gtk_container_add (GTK_CONTAINER(window), da);
g_signal_connect (da, "draw",
G_CALLBACK(draw_callback), NULL);
g_signal_connect (window, "destroy",
G_CALLBACK(gtk_main_quit), NULL);
gtk_widget_show_all (window);
gtk_main ();
return 0;
}
void draw_callback(GtkWidget *da, cairo_t *cr)
{
GdkRGBA color;
cairo_rectangle(cr, 20, 20, 100, 75);
gdk_rgba_parse (&color, "#0ff"); // シアン(cyan)
gdk_cairo_set_source_rgba(cr, &color);
cairo_fill(cr);
cairo_rectangle(cr, 140, 20, 100, 75);
gdk_rgba_parse (&color, "#f0f"); // マジェンタ(magenta)
gdk_cairo_set_source_rgba(cr, &color);
cairo_fill(cr);
cairo_rectangle(cr, 260, 20, 100, 75);
gdk_rgba_parse (&color, "#ff0"); // イエロー(yellow)
gdk_cairo_set_source_rgba(cr, &color);
cairo_fill(cr);
cairo_rectangle(cr, 20, 115, 100, 75);
gdk_rgba_parse (&color, "#f00"); // レッド(red)
gdk_cairo_set_source_rgba(cr, &color);
cairo_fill(cr);
cairo_rectangle(cr, 140, 115, 100, 75);
gdk_rgba_parse (&color, "#0f0"); // グリーン(green)
gdk_cairo_set_source_rgba(cr, &color);
cairo_fill(cr);
cairo_rectangle(cr, 260, 115, 100, 75);
gdk_rgba_parse (&color, "#00f"); // ブルー(blue)
gdk_cairo_set_source_rgba(cr,&color);
cairo_stroke(cr);
}
シアン | Cyan | "#0ff" | "rgb(0, 255, 255)" |
マジェンタ | Magenta | "#f0f" | "rgb(255, 0, 255)" |
イエロー | Yellow | "#ff0" | "rgb(255, 255, 0)" |
レッド | Red | "#f00" | "rgb(255, 0, 0)" |
グリーン | Green | "#0f0" | "rgb(0, 255, 0)" |
ブルー | Blue | "#00f" | "rgb(0, 0, 255)" |
ホワイト | White | "#fff" | "rgb(255, 255, 255)" |
ブラック | Black | "#000" | "rgb(0, 0, 0)" |
グレイ | Gray | "#666" | "rgb(127, 127, 127)" |
#include <gtk/gtk.h>
#include <math.h>
void draw_callback();
int main(int argc, char *argv[])
{
GtkWidget *window;
GtkWidget *da;
gtk_init(&argc, &argv);
window = gtk_window_new (0);
gtk_window_set_title (GTK_WINDOW(window), "Shape");
gtk_window_set_default_size (GTK_WINDOW(window), 430, 260);
gtk_container_set_border_width (GTK_CONTAINER(window), 15);
da = gtk_drawing_area_new ();
gtk_container_add (GTK_CONTAINER(window), da);
g_signal_connect (da, "draw",
G_CALLBACK(draw_callback), NULL);
g_signal_connect (window, "destroy",
G_CALLBACK(gtk_main_quit), NULL);
gtk_widget_show_all (window);
gtk_main ();
return 0;
}
void draw_callback(GtkWidget *da, cairo_t *cr)
{
GdkRGBA color;
cairo_set_line_width(cr, 1.0);
cairo_save (cr);
/*楕円形*/
cairo_scale (cr, 1, 0.6);
cairo_arc (cr, 70, 70, 50, (0.0 * (M_PI/180.0)),
(360.0 * (M_PI/180.0)));
/*角丸四角形*/
cairo_restore (cr);
cairo_move_to (cr, 150, 20);
cairo_arc (cr, 160, 20, 10, (180 * (M_PI/180.0)),
(270.0 * (M_PI/180.0)));
cairo_line_to (cr, 240, 10);
cairo_arc (cr, 240, 20, 10, (270 * (M_PI/180.0)),
(360.0 * (M_PI/180.0)));
cairo_line_to (cr, 250, 60);
cairo_arc (cr, 240, 60, 10, (0 * (M_PI/180.0)),
(90.0 * (M_PI/180.0)));
cairo_line_to (cr, 160, 70);
cairo_arc (cr, 160, 60, 10, (90 * (M_PI/180.)),
(180.0 * (M_PI/180.0)));
cairo_close_path(cr);
/*円弧*/
cairo_move_to (cr, 280, 10);
cairo_line_to (cr, 340, 10);
cairo_arc (cr, 280, 10, 100, (0 * (M_PI/180.0)),
(90.0 * (M_PI/180.0)));
cairo_close_path(cr);
/*正円*/
cairo_move_to (cr, 70, 160);
cairo_arc (cr, 70, 160, 50, (0 * (M_PI/180.0)),
(360.0 * (M_PI/180.0)));
/*正方形*/
cairo_rectangle (cr, 150, 110, 100, 100);
gdk_rgba_parse (&color, "rgb(255, 17, 17)");
gdk_cairo_set_source_rgba(cr, &color);
cairo_stroke_preserve(cr);
gdk_rgba_parse (&color, "rgb(17, 255, 17)");
gdk_cairo_set_source_rgba(cr, &color);
cairo_fill (cr);
/*文字列*/
gdk_rgba_parse (&color, "#777");
gdk_cairo_set_source_rgba(cr, &color);
/*
cairo_select_font_face(cr, "Sans", CAIRO_FONT_SLANT_NORMAL,
CAIRO_FONT_WEIGHT_BOLD);
*/
cairo_set_font_size(cr, 45.0);
cairo_move_to (cr, 280, 210);
cairo_show_text (cr, "Hello!");
}