#include <gtk/gtk.h>
void activate();
void draw_callback();
int main(int argc, char **argv)
{
GtkApplication *app;
int status;
app = gtk_application_new ("jp.vivacocoa.line", 0 );
g_signal_connect (app, "activate",
G_CALLBACK( activate), NULL);
status = g_application_run (G_APPLICATION(app), argc, argv);
g_object_unref (app);
return status;
}
void activate(GtkApplication *app, gpointer data)
{
GtkWidget *window;
GtkWidget *darea;
/* Window */
window =
gtk_application_window_new (app);
gtk_window_set_title (GTK_WINDOW(window), "Line" );
gtk_window_set_default_size (GTK_WINDOW(window), 300, 200);
gtk_window_set_position (GTK_WINDOW(window), 1 );
/* Drawing area */
darea = gtk_drawing_area_new();
gtk_container_add (GTK_CONTAINER(window), darea );
/* Signal */
g_signal_connect (darea, "draw",
G_CALLBACK(draw_callback), NULL);
/* Show window */
gtk_widget_show_all (window);
}
void draw_callback(GtkWidget *widget, 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, 20, 20 );
cairo_line_to (cr, 280, 20 );
cairo_stroke (cr);
cairo_move_to (cr, 20, 50 );
cairo_line_to (cr, 280, 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, 20, 80 );
cairo_line_to (cr, 280, 80 );
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, 30, 110 );
cairo_line_to (cr, 270, 110 );
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, 30, 140 );
cairo_line_to (cr, 270, 140 );
gdk_rgba_parse (&color, "#0ff" );
gdk_cairo_set_source_rgba
(cr, &color );
cairo_set_line_cap
(cr, CAIRO_LINE_CAP_ROUND );
cairo_stroke (cr);
cairo_move_to (cr, 30, 170 );
cairo_line_to (cr, 270, 170 );
gdk_rgba_parse (&color, "#f0f" );
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> // for time
void activate();
void draw_callback();
int main(int argc, char **argv)
{
GtkApplication *app;
int status;
app = gtk_application_new ("jp.vivacocoa.point", 0 );
g_signal_connect (app, "activate",
G_CALLBACK( activate), NULL);
status = g_application_run (G_APPLICATION(app), argc, argv);
g_object_unref (app);
return status;
}
void activate(GtkApplication *app, gpointer data)
{
GtkWidget *window;
GtkWidget *darea;
/* Window */
window =
gtk_application_window_new (app);
gtk_window_set_title (GTK_WINDOW(window), "Point" );
gtk_window_set_default_size (GTK_WINDOW(window), 300, 200);
gtk_window_set_position (GTK_WINDOW(window), 1 );
/* Drawing area */
darea = gtk_drawing_area_new();
gtk_container_add (GTK_CONTAINER(window), darea );
/* Signal */
g_signal_connect (darea, "draw",
G_CALLBACK(draw_callback), NULL);
/* Show window */
gtk_widget_show_all (window);
}
void draw_callback(GtkWidget *darea, cairo_t *cr)
{
/* Set random */
srand(time(NULL));
GdkRGBA color;
guint x = 0, y = 0, width = 0, height = 0;
width = gtk_widget_get_allocated_width (darea);
height = gtk_widget_get_allocated_height (darea);
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 );
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 activate();
void draw_callback();
int main(int argc, char **argv)
{
GtkApplication *app;
guchar status;
app = gtk_application_new ("jp.vivacocoa.color", 0 );
g_signal_connect (app, "activate",
G_CALLBACK( activate), NULL);
status = g_application_run (G_APPLICATION(app), argc, argv);
g_object_unref (app);
return status;
}
void activate(GtkApplication *app, gpointer data)
{
GtkWidget *window;
GtkWidget *darea;
/* Window */
window =
gtk_application_window_new (app);
gtk_window_set_title (GTK_WINDOW(window), "Color" );
gtk_window_set_default_size (GTK_WINDOW(window), 300, 200);
gtk_window_set_position (GTK_WINDOW(window), 1 );
/* Drawing area */
darea = gtk_drawing_area_new();
gtk_container_add (GTK_CONTAINER(window), darea );
/* Signal */
g_signal_connect (darea, "draw",
G_CALLBACK(draw_callback), NULL);
/* Show window */
gtk_widget_show_all (window);
}
void draw_callback(GtkWidget *darea, cairo_t *cr)
{
GdkRGBA color;
cairo_rectangle (cr, 15, 15, 80, 75 );
gdk_rgba_parse (&color, "#0ff" ); // cyan
gdk_cairo_set_source_rgba(cr, &color );
cairo_fill (cr);
cairo_rectangle (cr, 110, 15, 80, 75 );
gdk_rgba_parse (&color, "#f0f" ); // magenta
gdk_cairo_set_source_rgba(cr, &color );
cairo_fill (cr);
cairo_rectangle (cr, 205, 15, 80, 75 );
gdk_rgba_parse (&color, "#ff0" ); // yellow
gdk_cairo_set_source_rgba(cr, &color );
cairo_fill (cr);
cairo_rectangle (cr, 15, 110, 80, 75);
gdk_rgba_parse (&color, "#f00" ); // red
gdk_cairo_set_source_rgba(cr, &color );
cairo_fill (cr);
cairo_rectangle (cr, 110, 110, 80, 75 );
gdk_rgba_parse (&color, "#0f0" ); // green
gdk_cairo_set_source_rgba(cr, &color );
cairo_fill (cr);
cairo_rectangle (cr, 205, 110, 80, 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> // for M_PI
void activate();
void draw_callback();
int main(int argc, char **argv)
{
GtkApplication *app;
guchar status;
app = gtk_application_new ("jp.vivacocoa.shape", 0 );
g_signal_connect (app, "activate",
G_CALLBACK( activate), NULL);
status = g_application_run (G_APPLICATION(app), argc, argv);
g_object_unref (app);
return status;
}
void activate(GtkApplication *app, gpointer data)
{
GtkWidget *window;
GtkWidget *darea;
/* Window */
window =
gtk_application_window_new (app);
gtk_window_set_title (GTK_WINDOW(window), "Shape" );
gtk_window_set_default_size (GTK_WINDOW(window), 300, 200);
gtk_window_set_position (GTK_WINDOW(window), 1 );
/* Drawing area */
darea = gtk_drawing_area_new();
gtk_container_add (GTK_CONTAINER(window), darea );
/* Signal */
g_signal_connect (darea, "draw",
G_CALLBACK(draw_callback), NULL);
/* Show window */
gtk_widget_show_all (window);
}
void draw_callback(GtkWidget *darea, cairo_t *cr)
{
GdkRGBA color;
cairo_set_line_width(cr, 1.0);
cairo_save (cr);
/* Ellipse */
cairo_scale (cr, 1, 0.6);
cairo_arc (cr, 60, 60, 40, (0.0 * (M_PI/180.0)),
(360.0 * (M_PI/180.0)));
/* Rounded rectanble*/
cairo_restore (cr);
cairo_move_to (cr, 120, 25);
cairo_arc (cr, 130, 25, 10, (180 * (M_PI/180.0)),
(270.0 * (M_PI/180.0)));
cairo_line_to (cr, 190, 15);
cairo_arc (cr, 190, 25, 10, (270 * (M_PI/180.0)),
(360.0 * (M_PI/180.0)));
cairo_line_to (cr, 200, 60);
cairo_arc (cr, 190, 60, 10, (0 * (M_PI/180.0)),
(90.0 * (M_PI/180.0)));
cairo_line_to (cr, 130, 70);
cairo_arc (cr, 130, 60, 10, (90 * (M_PI/180.0)),
(180.0 * (M_PI/180.0)));
cairo_close_path (cr);
/* Arc */
cairo_move_to (cr, 220, 15);
cairo_line_to (cr, 280, 15);
cairo_arc (cr, 220, 15, 60, (0 * (M_PI/180.0)),
(90.0 * (M_PI/180.0)));
cairo_close_path (cr);
/* Circle */
cairo_move_to (cr, 60, 130);
cairo_arc (cr, 60, 130, 40, (0 * (M_PI/180.0)),
(360.0 * (M_PI/180.0)));
/* Square */
cairo_rectangle (cr, 120, 90, 80, 80);
/* Draw */
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);
/* String */
gdk_rgba_parse (&color, "#777" );
gdk_cairo_set_source_rgba (cr, &color );
/*
cairo_select_font_face (cr, "Sans",
CAIRO_FONT_SLANT_OBLIQUE,
CAIRO_FONT_WEIGHT_BOLD);
*/
cairo_set_font_size (cr, 30.0);
cairo_move_to (cr, 210, 170);
cairo_show_text (cr, "Hello!");
}