01 May 2013 - After the site upgrade, all passwords were reset and you will need to ask the site for a login reset on your first connection.
It's time to have some fun for the holiday season. We are
going to draw some stars in a Drawing Area. Stars will
have random colors, and random blinking cycle. Will will try to
write some object-oriented code to show good practices with
php-gtk.
For basic introduction to object syntax please refer to official php site.
First, create a basic class with some properties :
($x, $y) and size ($w, $h) as width and heightabstract ; if you try $obj=new Object, php will complaint about this. (Fatal error: Cannot instantiate abstract class Object)
draw() method as abstract because we can't draw anything now.
<?php
abstract class Object {
protected $x, $y, $w, $h;
abstract public function draw($drawing);
}
?>when you create some classes :
protected if you have no good reasons to make them private or public,public attribute visibilityprotected methods, avaid private methodspublic visibilitypublic visibility to.Now create a basic star with this piece of code :
Object classBasicStar constructorGtkDrawable has all needed primitives to make some drawings (lines, rectangles, circles ...). Here, we will use a GtkDrawingArea extention class to help drawing hiding some Gtk stuff (graphic context, colormap, event mask, pixmap, draw queue).<?php
class BasicStar extends Object{ // note 1
protected $color; // note 2
protected $radius; // note 2
function __construct($w, $h){ // note 4
$this->color = 'yellow';
$this->x = rand(0, $w); // note 3
$this->y = rand(0, $h); // note 3
$this->radius = rand(1, 3); // note 5
}
function draw($da){ // note 6
$da->draw_circle($this->color, $filled=true, $this->x-$this->radius, $this->y-$this->radius, $this->radius*2);
}
}
?>Let's create a graphical widget than can handle :
BasicStar classhere are some details :
ChristmasWidget inherits from a basic graphical widget containg some drawing facilities not shown here.$objects is an array where we will store our objects ; those objects must be drawables (should inherit from Object)($w, $h) to display in full window,draw() method ; objects can only draw on a DrawingArea ; $this is a DrawingiArea, so give it to graphical objects to render graphics.
<?php
class ChristmasWidget extends GraphCore{ // note 1
protected $objects; // note 2
public function __construct(){
parent::__construct(); // note 3
$this->objects = array(); // note 2
Gtk::timeout_add(350, array($this, 'timeout')); // note 4
}override this method and draw what you want.
function configure_event($widget, $event){ // note 5
$this->objects = array(); // note 6
parent::configure_event($widget, $event); // note 6
$w = $this->w(); // note 7
$h = $this->h();
$count = intval(($w+$h)/10); // note 9
for($i=0 ; $i<$count ; $i++) // note 8
$this->objects[] = new BasicStar($w, $h);
}
public function timeout(){ // note 10
$this->do_redraw();
return true;
}
function do_redraw(){ // note 11
if(!$this->realized()) // note 12
return;
foreach($this->objects as $obj){ // note 13
$obj->draw($this);
}
$this->refresh(); // note 14
}
}
?>Here every things is object, so let's create a new Window dedicated for our Christmas application demonstration. Here are all details :
GtkWindowChrismasDemo window class<?php
class ChrismasDemo extends GtkWindow{ //note 1
function __construct(){
parent::__construct(); // note 2
$this->connect_simple('destroy', array('gtk', 'main_quit')); // note 3
$this->set_title('Merry Christmas'); // note 4
$this->set_position(Gtk::WIN_POS_CENTER); // note 5
$this->set_size_request(600, 500); // note 6
$this->add (new ChristmasWidget()); // note 7
$this->show_all(); // note 8
}
}
$demo = new ChrismasDemo(); // note 9
Gtk::main(); // note 10
?>To make our stars blink, we need :
step) ; each time we call draw() method, this counter will grow up,cycle$color and $radius attributes are inherited from BasicStar, so we don't need to declare them again here ; if you do that that will not make some damages just consume some memory.Some details :
BlinkingStar,$cycle, $step have been explained below,$r is the last radius used when drawing at last call ; it will be used to clear star<?php
class BlinkingStar extends BasicStar{ // note 1
protected $cycle; // note 2
protected $step;
protected $colors = array(
'#fff589', '#f5ff89', '#fff089', '#b7b7ff',
'#fff589', '#f5ff89' ,'#f8debc'); // note 3
function __construct($w, $h){
parent::__construct($w, $h);
$this->color = $this->colors[rand(0, count($this->colors)-1)]; // note 3
$this->cycle = rand(20, 50);
$this->step = rand(0, $this->cycle);
}
function draw($da){
# introduce some random delay
if(rand(0, 10) > 3) // note 4
return;
if(isset($this->r)){ // note 5
$r = $this->r;
# clear star area
$da->draw_circle('black', $filled=true,
$this->x-$r, $this->y-$r, $r*2); // note 5
}
# draw a star with a radius changing step by step
$r = $this->step % $this->radius+1; // note 6
$da->draw_circle($this->color, $filled=true, $this->x-$r, $this->y-$r, $r*2); // note 6
$this->r = $r;
$this->step++;
}
}
?>You can get complete source code in attachment at the bottom of this page.
With object oriented design you can see how it is very easy to create some basics objects and extends them. With php-gtk you can make some graphical application : games, animation, simulations, map drawing, vector drawing. Object oriented design is really interesting with graphical features.
In this tutorial, we have created some graphicals objects with dedicated classes ; theses objects are managed with specialized widget ChristmasWidget and for completeness we have created our ChristmasWindow widget. Here is some details :
ChristmasWidget don't know how to draw a star, this widget task is only to manage a collection of graphical objects and call draw() method,
| Attachment | Size |
|---|---|
| Star tutorial source code | 5.35 KB |
| Star tutorial with moon class | 5.58 KB |
Recent comments
43 weeks 5 days ago
44 weeks 4 days ago
45 weeks 1 day ago
47 weeks 4 days ago
1 year 16 weeks ago
1 year 19 weeks ago
1 year 23 weeks ago
1 year 23 weeks ago
1 year 23 weeks ago
1 year 40 weeks ago