Graphics::Simple -- a simple , device-independent graphics API for Perl


Graphics::Simple -- a simple , device-independent graphics API for Perl


        use Graphics::Simple;
        line 100,100,200,200;
        circle 50,50,25;
        stop(); clear(); # Wait for a button press, clear the page


Ever had a Commodore C-64 or Vic-20 or some other of the machines of that era? Where doing graphics was as simple as

        line 20,20,50,30;

and you didn't have to go through things like XOpenDisplay etc.

This module tries to bring back the spirit of that era in a modern environment: this module presents a simple, unified API to several different graphics devices - currently X (using Gtk and Gnome) and PostScript.

The interface is primarily made easy-to-use, starting from the idea that the above line command must work. Therefore, it exports most of the primitives by default (you can turn this off).

However, everything is not sacrificed in the name of simplicity: believing in ``simple things simple, complicated things possible'', this module also allows multiple windows (all the primitives also work as methods of window objects) as well as raw access to the underlying devices - although the device-independence is then lost. In future plans are some sort of interactions with the devices with which it is possible as well as the addition of more devices.

The use command currently accepts the forms

        use Graphics::Simple;
        use Graphics::Simple qw/line circle/;
        use Graphics::Simple 300,400; # portrait paper
        use Graphics::Simple 300,400, qw/line circle/;

i.e. the optional size of the default window first and then normal Exporter arguments.

Graphics::Simple has several different back-ends, currently GnomeCanvas, TkCanvas, PostScript and (not fully working yet) Fig. Other backends are expected.

To start Graphics::Simple with a given backend, you should set the environment variable GSIMPL to the value, e.g. by running your script with the command

        GSIMPL='PostScript' perl

or by setting the environment variable permanently in your shell, by

        export GSIMPL


        setenv GSIMPL PostScript

depending on which shell you use.

line [$win_to], [$name], $x1, $y1, $x2, $y2, ...

Draws a line through the points given.

arrow [$win_to], [$name], $x1, $y1, $x2, $y2, ...

Like line, but makes an arrowhead in the end.

line_to [$win], [$name], $x1, $y1, $x2, $y2, ...

Called several times in a sequence, starts and continues adding points to a line. If called with no coordinates, finishes the current line. This is just a convenient wrapper over a line call with all the parameters given - a faster way would just be to collect your parameters to an array.

circle [$win], [$name], $x, $y, $radius


ellipse [$win], [$name], $x1, $y1, $x2, $y2

The ellipse enclosed in the rectangle given by its two corners

text [$win], [$name], $x, $y, $string


image [$win], [$name], $x, $y, $width, $height, $depth, $string

This command creates a rectangular bit- or pixmap. The $depth parameter is the number of bytes per pixel (1 for B/W or 3 for RGB) and the string is a string of packed bytes that describe the image.

For instance, the module PDL is a good source for such strings.

clear, stop

        stop [$win]
        clear [$win]

clear removes all the drawn elements from the window. <wait> waits for a button press. These are usually coupled:

        stop; clear;

set_window, get_window

See the source - undocumented and potentially changing api

push_window, pop_window

Graphics::Simple maintains a simple window stack so that subroutines can easily use

        push_window $win;
        line ...

to avoid having too many method calls.

color [$win,] $color;

Set the current color to $color. Currently, the colors known are

        red green blue black white

as well as any RGB color with the X syntax:

        color '#FFFF00';

is yellow. You can also give an array ref of three numbers between 0 and 1 for RGB colors.

linewidth [$win,] $width


This is an alpha proof-of-principle version - the API may still change wildly.


Copyright(C) Tuomas J. Lukka 1999. All rights reserved. This software may be distributed under the same conditions as Perl itself.

 Graphics::Simple -- a simple , device-independent graphics API for Perl