<?php
abstract class DrawingObject
{
protected $_x = 0;
protected $_y = 0;
protected $_borderColor = 'ff0000';
protected $_backgroundColor = 'ffffff';
abstract public function draw();
public function __construct()
{
$this->_x = rand(0, 400);
$this->_y = rand(0, 400);
}
public function setBorderColor($newColor)
{
$this->_borderColor = $newColor;
}
public function getBorderColor()
{
return $this->_borderColor;
}
public function setBackgroundColor($newColor)
{
$this->_backgroundColor = $newColor;
}
public function getBackgroundColor()
{
return $this->_backgroundColor;
}
public function setPos($x, $y)
{
$this->_x = (int) $x;
$this->_y = (int) $y;
}
public function getPos()
{
return array($this->_x, $this->_y);
}
}
class Circle extends DrawingObject
{
public function draw()
{
// Logik, Kreis zu zeichnen
echo '<div style="
text-align: center;
background: #' . $this->_backgroundColor. ';
line-height: 200px;
position: absolute;
top: ' . $this->_y . 'px;
left: ' . $this->_x . 'px;
border: 1px solid #' . $this->_borderColor. ';
-moz-border-radius: 200px;
width: 200px;
height: 200px;
">Ich bin ein Kreis</div>';
}
}
class Rectangle extends DrawingObject
{
public function draw()
{
// Logik, Rechteck zu zeichnen
echo '<div style="
text-align: center;
background: #' . $this->_backgroundColor. ';
line-height: 100px;
position: absolute;
top: ' . $this->_y . 'px;
left: ' . $this->_x . 'px;
border: 1px solid #' . $this->_borderColor. ';
width: 300px;
height: 100px;
">Ich bin ein Rechteck</div>';
}
}
class Scene
{
protected $_drawingObjects = array();
public function add(DrawingObject $do)
{
$this->_drawingObjects[] = $do;
}
public function render()
{
foreach ($this->_drawingObjects as $do) {
$do->draw();
}
}
}
$scene = new Scene();
$scene->add(new Circle());
$c2 = new Circle();
$c2->setBorderColor('00ff00');
$c2->setBackgroundColor('eeffee');
$scene->add($c2);
$scene->add(new Circle());
$scene->add(new Rectangle());
$scene->render();