Buchempfehlung
MySQL kurz & gut
MySQL kurz & gut
Das preiswerte Taschen- buch stellt MySQL-rele- vante Inhalte systematisch und knapp dar, sodass es sich optimal zum Nach- schlagen beim Pro- grammieren eignet. [Mehr Infos...]
FreeBASIC-Chat
Es sind Benutzer im FreeBASIC-Chat online.
(Stand:  )
FreeBASIC bei Twitter
Twitter FreeBASIC-Nachrichten jetzt auch über Twitter erhalten. Follow us!

Code-Beispiel

Code-Beispiele » Grafik und Fonts

Zeichnen mit GooCanvas (ein Graph)

Lizenz:Erster Autor:Letzte Bearbeitung:
GPLv3MitgliedTJF 07.09.2012
Bildschirmausgabe
Vergrößern
Bildschirmausgabe

Dieses Code Beispiel öffnet ein Fenster und erzeugt die abgebildete Grafik darin. Beim Anklicken des Graphen ... ups, wir wollen doch nicht alles verraten.

Das Beispiel ist unter Windows und LINUX kompilierbar. Als GUI-Toolkit kommt GTK zum Einsatz (hier mal ohne Glade3) und die Zeichenfunktionen werden mit der Erweiterung GooCanvas (siehe Externer Link!Beschreibung auf gnome.org) realisiert. Dazu sind aktuelle Header erforderlich, wie sie in fbc-0.24.0 Aktuelle Compiler (oder neuer) enthalten sind. Hinweise zur Installation der Binaries finden sich in goocanvas.bih.

Für diesen Installationsaufwand wird man schließlich mit Grafik belohnt, die nicht nur mächtige Befehle bietet und durch Qualität und Geschwindigkeit überzeugt - sie ist auch überraschend einfach in Pixel- bzw. Vektorgrafik-Dateien zu exportieren (,z. B. im Dateiformat PostScript (*.ps) zur qualitativ hochwertigen Ausgabe auf dem Drucker).

#INCLUDE ONCE "gtk/gtk.bi"
#INCLUDE ONCE "goocanvas.bi"

' Some variables and types
CONST Xmax = 1000, Ymax = 1000, Smin = 0.1
CONST GridX = 100.0, GridY = GridX, GridW = 400.0, GridH = 300.0


' Initialize GTK+.
gtk_init (@__FB_ARGC__, @__FB_ARGV__)

' Create the window and widgets.
VAR win = gtk_window_new (GTK_WINDOW_TOPLEVEL)
gtk_window_set_default_size (GTK_WINDOW (win), 640, 600)
gtk_widget_show (win)
g_signal_connect (win, "delete_event", G_CALLBACK(@gtk_main_quit), NULL)

VAR scrolled_win = gtk_scrolled_window_new (NULL, NULL)
gtk_scrolled_window_set_shadow_type (GTK_SCROLLED_WINDOW (scrolled_win), _
                                     GTK_SHADOW_IN)
gtk_widget_show (scrolled_win)
gtk_container_add (GTK_CONTAINER (win), scrolled_win)

VAR canvas = goo_canvas_new ()
gtk_widget_set_size_request (canvas, 600, 450)
goo_canvas_set_bounds (GOO_CANVAS (canvas), 0, 0, Xmax, ymax)
gtk_widget_show (canvas)
gtk_container_add (GTK_CONTAINER (scrolled_win), canvas)


VAR root = goo_canvas_get_root_item (GOO_CANVAS (canvas))

VAR title = goo_canvas_text_new(root, _
       "<span size=""xx-large"">GooCanvas FreeBasic-Example</span>", _
                                GridX + 0.5 * GridW, GridY - 25, -1, _
                                GTK_ANCHOR_S, _
                                "use-markup", 1, _
                                 "font", "Times bold 14", _
                                NULL)

VAR group = goo_canvas_group_new(root, _
                                 "font", "Sans 14", _
                                 NULL)

' calculate some values
VAR line_group = 0.8 ' basic line width
VAR az = 100, pi = 4.0 * ATN(1), n = 2 * az - 1
VAR fx = GridW / (n - 1), sx = (az - 1) / pi, dx = 0.25 * GridW
VAR fy = GridH * 0.45, oy = GridY + 0.5 * GridH

VAR grid = goo_canvas_grid_new(group, _
                               GridX, GridY, GridW, GridH, _
                               dx, fy * 0.5, dx * 1.001, 0.05 * GridH, _
                               "border-width", line_group * 2, _
                               "horz-grid-line-width", line_group, _
                               "vert-grid-line-width", line_group, _
                               "horz-grid-line-color", "gray", _
                               "vert-grid-line-color", "gray", _
                               NULL)

VAR CanPoi = goo_canvas_points_new(az), copo = CanPoi->coords
FOR i AS INTEGER = 0 TO n STEP 2
  copo[i] = GridX + i * fx
  copo[i + 1] = oy - SIN(i/sx) * fy
NEXT
VAR poly = goo_canvas_polyline_new(group, 0, 0, _
                                   "stroke-color", "red",_
                                   "line-width", line_group * 4,_
                                   "tooltip", "Test Polyline", _
                                   "points", CanPoi,_
                                   NULL)
goo_canvas_points_unref(CanPoi)

' and now some text, text, text, ...
VAR ox = GridX - 8 * line_group
VAR text = goo_canvas_text_new(group, "1", _
                               ox, oy - fy, -1, _
                               GTK_ANCHOR_E, _
                               NULL)
text = goo_canvas_text_new(group, "-1", _
                           ox, oy + fy, -1, _
                           GTK_ANCHOR_E, _
                           NULL)
fy /= 2
text = goo_canvas_text_new(group, "<small>0.5</small>", _
                           ox, oy - fy, -1, _
                           GTK_ANCHOR_E, _
                           "use-markup", 1, _
                           NULL)
text = goo_canvas_text_new(group, "<small>-0.5</small>", _
                           ox, oy + fy, -1, _
                           GTK_ANCHOR_E, _
                           "use-markup", 1, _
                           NULL)
text = goo_canvas_text_new(group, "0", _
                           ox, oy, -1, _
                           GTK_ANCHOR_E, _
                           NULL)
ox -= 35
text = goo_canvas_text_new(group, "f(<i>&#966;</i>) = sin(<i>&#966;</i>)", _
                           ox, oy, -1, _
                           GTK_ANCHOR_S, _
                           "use-markup", 1, _
                           NULL)
goo_canvas_item_rotate(text, -90.0, ox, oy)

ox = GridX
oy = GridY + GridH + 8 * line_group
text = goo_canvas_text_new(group, "0", _
                           ox, oy, -1, _
                           GTK_ANCHOR_N, _
                           NULL)
ox += dx
text = goo_canvas_text_new(group, "<small>0.5&#960;</small>", _
                           ox, oy, -1, _
                           GTK_ANCHOR_N, _
                           "use-markup", 1, _
                           NULL)
ox += dx
text = goo_canvas_text_new(group, "&#960;", _
                           ox, oy, -1, _
                           GTK_ANCHOR_N, _
                           NULL)
text = goo_canvas_text_new(group, "This <i>&#966;</i> looks like an <i>angel</i>", _
                           ox, oy + 25, -1, _
                           GTK_ANCHOR_N, _
                           "use-markup", 1, _
                           NULL)
ox += dx
text = goo_canvas_text_new(group, "<small>1.5&#960;</small>", _
                           ox, oy, -1, _
                           GTK_ANCHOR_N, _
                           "use-markup", 1, _
                           NULL)
ox += dx
text = goo_canvas_text_new(group, "2&#960;", _
                           ox, oy, -1, _
                           GTK_ANCHOR_N, _
                           NULL)
text = goo_canvas_text_new(group, "<span size=""x-small""><span foreground=""blue"">" _
                           !"Click me!</span>\n<span background=""yellow"">" _
                           " l · m · r </span></span>", _
                           ox - 0.5 * dx, GridY + .05 * GridH + 0.5 * fy, -1, _
                           GTK_ANCHOR_CENTER, _
                           "use-markup", 1, _
                           "alignment", PANGO_ALIGN_CENTER, _
                           NULL)

' unvisible rect to make the grid clickable (not only lines)
VAR rect = goo_canvas_rect_new(group, _
                               GridX, GridY, GridW, GridH, _
                               "fill-color-rgba", &hFF000000, _
                               "stroke-pattern", NULL, _
                               NULL)

TYPE CbPointer
  AS GooCanvasItem PTR Title, Group
END TYPE

' signal handler (callback)
FUNCTION on_button_press CDECL(BYVAL item AS GooCanvasItem PTR, _
                               BYVAL target AS GooCanvasItem PTR, _
                               BYVAL event AS GdkEventButton PTR, _
                               BYVAL dat AS CbPointer PTR) AS gboolean
  STATIC AS INTEGER mo = 0
  IF event->button = 2 THEN
    goo_canvas_item_stop_animation(dat->Title) : mo = 0
    goo_canvas_item_set_simple_transform (dat->Title, 0.0, 0.0, 1.0, 0.0)
    goo_canvas_item_stop_animation(dat->Group) : mo = 0
    goo_canvas_item_set_simple_transform (dat->Group, 0.0, 0.0, 1.0, 0.0)
    RETURN TRUE
  ELSEIF mo = 0 THEN
    goo_canvas_item_animate (dat->Title, _
                             GridX + 0.5 * GridW, GridY - 25, _
                             Smin, -7.0, _
                             FALSE, 8 * 40, 40, _
                             GOO_CANVAS_ANIMATE_BOUNCE)
    goo_canvas_item_animate (dat->Group, _
                             Xmax, Ymax, _
                             Smin, 90, _
                             FALSE, event->button * 50 * 40, 40, _
                             GOO_CANVAS_ANIMATE_BOUNCE)
    mo = 1
  ELSE
    goo_canvas_item_stop_animation(dat->Title)
    goo_canvas_item_set_simple_transform (dat->Title, 0.0, 0.0, 1.0, 0.0)
    goo_canvas_item_stop_animation(dat->Group) : mo = 0
  END IF

  RETURN TRUE
END FUNCTION
g_signal_connect (group, "button_press_event", _
                  G_CALLBACK (@on_button_press), @TYPE<CbPointer>(title, group))

' Pass control to the GTK+ main event loop.
gtk_main ()

END 0

English

See Externer Link!english forum.


Zusätzliche Informationen und Funktionen
  • Das Code-Beispiel wurde am 23.09.2011 von MitgliedTJF angelegt.
  • Die aktuellste Version wurde am 07.09.2012 von MitgliedTJF gespeichert.
  Bearbeiten Bearbeiten  

  Versionen Versionen