Re: drawing shapes with color



Here's what I use:

#!/usr/bin/perl

use Gtk2 '-init';

{
my %allocated_colors;
sub get_color {
    my ($colormap, $name) = @_;
    my $ret;

    if ($ret = $allocated_colors{$name}) {
        return $ret;
    }

    my $color = Gtk2::Gdk::Color->parse($name);
    $colormap->alloc_color($color,TRUE,TRUE);

    $allocated_colors{$name} = $color;

    return $color;
}
}

sub draw_line {
    my($widget) = shift;
    my($line) = shift;
    my($color) = shift;
    my $colormap = $widget->window->get_colormap;
    my $gc = $widget->{gc} || new Gtk2::Gdk::GC $widget->window;
    $gc->set_foreground(get_color($colormap, $color));

    $widget->window->draw_line($gc, @$line);
}

# Draw a line in the expose callback
sub cb_da_event {
    my $this = shift;
    my $event = shift;

    if ($event->type eq "expose") {
        draw_line($this, [30,30, 200,100], "red");
    }
}

sub create_widgets {
    my $mw = Gtk2::Window->new();
    my $w_drawing_area = Gtk2::DrawingArea->new();
    $mw->add($w_drawing_area);
    $w_drawing_area->set_size_request(256,256);
    $w_drawing_area->signal_connect(event=>\&cb_da_event);

    $mw->show_all;
}

create_widgets();

main Gtk2;

Hope this helps.

Regards,
Dov

On 3/30/06, Veli Ogla Sungutay <velisungutay gmail com> wrote:
Would someone please show me how to draw a shape, say a rectangle or a
line  with some color on????? I would really appreciate it. I've been
trying to find my way around these and to no avail:

## IMAGE WORKING:
 $hbox->realize();
($pixmap,$mask)=Gtk2::Gdk::Pixmap->create_from_xpm($hbox->window,undef,"image.jpg");
$img = Gtk2::Image->new_from_pixmap($pixmap,$mask);
$vbox->pack_start($img,FALSE,FALSE,0);

## GARBAGE BG, white line
$pixmap = Gtk2::Gdk::Pixmap->new($hbox->window,100,100,-1);
$gc = $hbox->style->fg_gc('selected');
$pixmap->draw_line($gc,0,0,100,100);
$img2 = Gtk2::Image->new_from_pixmap($pixmap,undef);
$vbox->pack_start($img2,FALSE,FALSE,0);


## WORKING
$pixmap = Gtk2::Gdk::Pixmap->new(undef,100,100,-1);
$gc = $hbox->style->fg_gc('selected');
$pixmap->draw_rectangle($gc,TRUE,0,0,100,100);

## GARBAGE BG
$pixmap = Gtk2::Gdk::Pixmap->new(undef,100,100,-1);
$gc = $hbox->style->fg_gc('selected');
$pixmap->draw_line($gc,0,0,100,100);
$img = Gtk2::Image->new_from_pixmap($pixmap,undef);
$vbox->pack_start($img,FALSE,FALSE,0);




## EITHER GETTING BLACK RECT OR GARBAGE depending on the draw_ methods
my $pixmap = Gtk2::Gdk::Pixmap->new(undef,100,100,-1);
my $gc = Gtk2::Gdk::GC->new($pixmap);
my $c = Gtk2::Gdk::Color->new(65535,65535,65535);
my $c2 = Gtk2::Gdk::Color->new(32000,0,0);
$gc->set_background($c);
$gc->set_foreground($c2);
$pixmap->draw_rectangle($gc,TRUE,0,0,100,100);
$img = Gtk2::Image->new_from_pixmap($pixmap,undef);
$vbox->pack_start($img,FALSE,FALSE,0);
_______________________________________________
gtk-perl-list mailing list
gtk-perl-list gnome org
http://mail.gnome.org/mailman/listinfo/gtk-perl-list




[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]