Re: The syntax of Gnome2::Canvas::Line?



On Sat, 14 Jun 2008 07:57:52 -0700
walt <wa1ter myrealbox com> wrote:

Hi list,

This is an example from the tarball of how to draw a line:

Gnome2::Canvas::Item->new ($group,
                         'Gnome2::Canvas::Line',
                         points => [200.0, 0.0, 200.0, 450.0],
                         fill_color => 'black',
                         width_units => 4.0);

My puzzle is why the syntax is any different from Rectangle, i.e.
why use 'points' instead of x1,y1,x2,y2?

Second, the use of brackets instead of () to describe points is
a complete surprise to me.  I'm new to perl and I've never seen
that syntax used in any of the perldocs I've read (so far).
What's the difference in this context between [] and ()?

Look at this example. The points option allows you to change them later
by modifying the arrayref.  The difference from the rect is you can have
as many point pairs as you want.  In general, Lines on a canvas is a general purpose
curve.  The curve can be smoothed or angular (different canvas widgets handle
smotthing differently, Tk and Zinc will let you smooth any set of line points to make 
bezier curves.) 

I just posted a recent question about making polylines on a Goo::Canvas,
and it was a bit tricky, because it didn't setup the points option by default,
requiring you to create the polyline with the points undef, and then set the points
option after creation

my $line = Goo::Canvas::Polyline->new(                                                    
        $root, FALSE,                                                                     
         undef, # points need to be set after creation                                    
        'stroke-color' => 'black',                                                        
        'line-width' => 3,                                                                
        'start-arrow' => TRUE,                                                            
        'end-arrow' => TRUE,                                                              
        'arrow-tip-length' => 3,                                                          
        'arrow-length' => 4,                                                              
        'arrow-width' => 3.5                                                              
    );                                                                                    
                                                                                          
# setting after line creation, sets the 'points' property by name                         
$line->set(points => $points);                                                            
                                    
But.............
Gnome2::Canvas::Line makes it easier by setting the points option as default.

#!/usr/bin/perl
use warnings;
use strict;
use Gtk2 -init;
use Gnome2::Canvas;

my $window   = Gtk2::Window->new;
$window->signal_connect( destroy => sub { exit } );

my $scroller = Gtk2::ScrolledWindow->new;
my $canvas   = Gnome2::Canvas->new();
$scroller->add( $canvas );
$window->add( $scroller );
$window->set_default_size( 500, 500 );

#if scrollregion is smaller than default size, odd
# coordinate problems occur
#$canvas->set_scroll_region( -20, -20, 400, 400 ); #bad

$canvas->set_scroll_region( 0, 0, 700, 700 );
$window->show_all;

my $root = $canvas->root;

#$root->move(-50,-50);

my $text = Gnome2::Canvas::Item->new(
    $root, 'Gnome2::Canvas::Text',
   x          => 20,
   y          => 15,
   fill_color => 'black',
   font       => 'Sans 14',
   anchor     => 'GTK_ANCHOR_NW',
   text       => 'Click to add waypoint'
);

$canvas->signal_connect (event => \&event_handler);

my $points = [10,10,100,100,250,50];

my $line2= Gnome2::Canvas::Item->new ($root,
                'Gnome2::Canvas::Line',
                points => $points,
                fill_color => "red",
                width_units => 3.0,
                cap_style => 'projecting',
                join_style => 'miter',
                );

my $p = $line2->get('points');
print "@$p\n";
my ($x,$y) = @$p;


Gtk2->main;

##############################

sub event_handler{
     my ( $widget, $event ) = @_;
#     print $widget ,' ',$event->type,"\n";

    if ( $event->type eq "button-press" ) {
        print 'x->',$event->x,'  ','y->',$event->y,"\n";

      my ($x,$y) = ($event->x,$event->y);
#      my (undef, $x, $y, $state) =  $event->window->get_pointer;
     
      print "$x  $y\n";
     
        push @$points,$event->x , $event->y;
        $line2->set(points=>$points);

        my $p = $line2->get('points');
        print "@$p\n";
  
    }    
}

__END__


zentara

-- 
I'm not really a human, but I play one on earth.
http://zentara.net/CandyGram_for_Mongo.html 



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