also wir benutzen turtle und paper, sehen wie folgt aus:
/************************************************
*Klasse fuer Schreib-/Zeichenblatt
* fuer z.B. Turtle Graphics
************************************************/
public class Paper {
//-------------------------------------Instanzvariablen
private int size; //quadratisch
private char[][] content;
//---------------------------------------------Methoden
public void clear(){
for (int i=0; i<size; i++)
for (int j=0; j<size; j++)
content[j] = ' ';
}
public Paper(){ //default Konstruktor
size = 10;
content = new char[size][size];
clear();
}
public Paper(int size){ //Konstruktor
this.size = size;
content = new char[size][size];
clear();
}
public int getSize(){
return size;
}
public void mark(int i, int j, char symbol){
//requires 0<=i,j<size
content[j] = symbol;
}
public void display(){
for (int i=0; i<size; i++){
for (int j=0; j<size; j++)
System.out.print(content[j]);
System.out.println();
}
}
}
/**********************************************************
/*Klasse fuer Turtle Graphics
/*benoetigt Objekt der Klasse Paper als Zeichenblatt
/* (wird in Konstruktor uebergeben !!)
**********************************************************/
public class Turtle {
//-------------------------------------Instanzvariablen
private Paper myPaper; //private nicht sehr nuetzlich
private int paperSize;
private int x, y;
private int direction;
private boolean penActive;
private char symbol;
private final int NORTH=0, EAST=1, SOUTH=2, WEST=3;
//------------------------------------------Konstruktor
// paper noetig - kein default !!!
public Turtle(Paper somePaper){
myPaper = somePaper;
paperSize = myPaper.getSize();
x = 0; y = 0;
direction = EAST;
penActive = false;
symbol = '*';
}
//--------------------------------------------Methoden
public void move(int distance) {
//requires: distance >= 0;
//requires: direction in 0..3;
//requires: 0 <= x,y < paperSize;
//movement stops at edge of paper
int startX = x, startY = y;
if (distance < 0){
System.out.println("distance < 0 ignored");
return;
}
switch (direction) {
case NORTH:
y = Math.max(y - distance, 0);
if (penActive)
for (int i = startY; i >= y; i--)
myPaper.mark(i,x,symbol);
break;
case SOUTH:
y = Math.min(y + distance, paperSize-1);
if (penActive)
for (int i = startY; i <= y; i++)
myPaper.mark(i,x,symbol);
break;
case WEST:
x = Math.max(x - distance, 0);
if (penActive)
for (int i = startX; i >= x; i--)
myPaper.mark(y,i,symbol);
break;
case EAST:
x = Math.min(x + distance, paperSize-1);
if (penActive)
for (int i = startX; i <= x; i++)
myPaper.mark(y,i,symbol);
break;
}
}
public void penUp() {
penActive = false;
}
public void penDown(){
penActive = true;
}
public void left() {
// requires: direction in 0..3;
switch (direction) {
case NORTH:
direction = WEST; break;
case WEST:
direction = SOUTH; break;
case SOUTH:
direction = EAST; break;
case EAST:
direction = NORTH; break;
}
}
public void right() {
// requires: direction in 0..3;
switch (direction) {
case NORTH:
direction = EAST; break;
case EAST:
direction = SOUTH; break;
case SOUTH:
direction = WEST; break;
case WEST:
direction = NORTH; break;
}
}
public void setSymbol(char someSymbol){
symbol = someSymbol;
}
public void setPaper(Paper somePaper){
myPaper = somePaper;
}
public Paper getPaper(){
return myPaper;
}
}
das ganze funktioniert eigentlich ganz einfach, model kann man sich so vorstellen: schildkröte kriecht am blatt herum, man kann sie bewegen, nach links oder rechts drehen und eben mit dem stift malen lassen oder auch nicht uns so sollen wir eben eine Klasse schreiben, die eine methode draw hat, in der man weihnachtsbäume zeichnen kann, in verschiedenen grössen, an verschiedenen stellen des blattes und verschiedene farbe, schwarz weiß eben.
baum sieht so aus
______*
_____***
____*****
___*******
______*