ztriangle0


0001  #!/usr/bin/perl
0002  use warnings;
0003  use strict;
0004  use Tk; 
0005  use Tk::Zinc;
0006  
0007  my $mw = MainWindow->new;
0008  $mw->geometry("700x600");
0009  $mw->resizable(0,0);
0010  
0011  my $zinc = $mw->Zinc(-width => 700, -height => 565,
0012                  -backcolor => 'black',
0013  		-borderwidth => 3, -relief => 'sunken')->pack;
0014  
0015  # Then we create a gray filled rectangle, in which we will display explain text.
0016  $zinc->add('rectangle', 1 , [200, 400, 490, 490],
0017  	   -linewidth => 2,
0018  	   -filled => 1,
0019  	   -fillcolor => 'SkyBlue',
0020  	   );
0021  my $text = $zinc->add('text', 1,
0022  		      -position => [350, 445],
0023  		      -anchor => 'center',
0024  		      );
0025  
0026  $zinc->add('rectangle', 1 , [250,275,450,325], #corners (xpos1,ypos1,xpos2,ypos2)
0027  	   -linewidth => 2,
0028  	   -filled => 1,
0029  	   -fillcolor => 'Orange',
0030  	   );
0031  
0032  my $wintext = $zinc->add('text', 1,
0033  		      -position => [350, 300],
0034  		      -anchor => 'center',
0035  		      );
0036  
0037  #create triangles
0038  #create first triangle, then clone and translate
0039  #maintain an array of positions
0040  
0041  my ($x1,$y1,$x2,$y2,$x3,$y3) = my @initcoords = (0,0,20,0,10,40);
0042  
0043  my @pos1 = (130,20); #initial positions to translate to
0044   
0045  my $tr1 = $zinc->add('triangles', 1,
0046                       [@initcoords], #(x1,y1,x2,y2,x3,y3)
0047                       -fan => 1,
0048                       -colors => 'Orange',
0049                       -visible => 1,
0050                       );
0051  $zinc->translate($tr1,@pos1); 
0052  
0053  # Display comment
0054  &comment("Hit Tab to begin, and then a few more times.");
0055  &wincomment("READY");
0056  
0057  # Create Tk binding 
0058  $mw->Tk::bind('<Tab>', \&start);
0059  
0060  my $closebutton = $mw->Button(text => 'Exit', -command => sub{Tk::exit(0)})
0061                 ->pack; 
0062  MainLoop;
0063  
0064  sub start {
0065     # set binding to stop and exit
0066      $mw->Tk::bind('<Escape>', \&stopaction);
0067  
0068      &comment("Hit Escape to stop.");
0069      &startaction();
0070  }
0071  
0072  sub stopaction{
0073      &comment("Hit Tab to begin, then hit again a few times.");
0074      &wincomment("READY");
0075      # Callback bound to '<Key>' event when pressed
0076      $mw->Tk::bind('<Tab>', \&start);
0077  
0078  #thrown in to demonstrate shape-shifting
0079  $zinc->coords($tr1,[100,100,100,60,60,60]); #changes shape of triangle
0080                                              #not position
0081  }
0082  
0083  sub startaction {
0084  # move triangles
0085  $zinc->translate($tr1,0,10);  #moves down 10 pixels
0086  }
0087  
0088  # Just display comment 
0089  sub comment {
0090      my $string = shift;
0091      $zinc->itemconfigure($text, -text => $string);
0092  }
0093  
0094  # display winning comment 
0095  sub wincomment {
0096      my $string = shift;
0097      $zinc->itemconfigure($wintext, -text => $string);
0098  }