29.01.2014 Aufrufe

Script Java

Script Java

Script Java

MEHR ANZEIGEN
WENIGER ANZEIGEN

Sie wollen auch ein ePaper? Erhöhen Sie die Reichweite Ihrer Titel.

YUMPU macht aus Druck-PDFs automatisch weboptimierte ePaper, die Google liebt.

11.4 Stack<br />

import java.awt.event.*;<br />

public class Turtle {<br />

private Graphics graphics;<br />

private Point position;<br />

private int orientation;<br />

public Turtle(Graphics g) {<br />

graphics = g;<br />

Rectangle clip = g.getClipBounds();<br />

position = new Point(clip.x + clip.width/2,clip.y + clip.height/2);<br />

orientation = 0;<br />

}<br />

public void forward(int distance) {<br />

int x = position.x +<br />

(int)Math.round(distance*Math.sin(Math.PI*orientation/180));<br />

int y = position.y -<br />

(int)Math.round(distance*Math.cos(Math.PI*orientation/180));<br />

graphics.drawLine(position.x, position.y, x, y);<br />

position.setLocation(x, y);<br />

}<br />

}<br />

public void right(int angle) {<br />

orientation = (orientation + angle) % 360;<br />

}<br />

public class Logo {<br />

private Sequence code;<br />

private Turtle turtle;<br />

public abstract class Command {<br />

abstract public void run();<br />

abstract public String toString();<br />

}<br />

public class Forward extends Command {<br />

private int argument;<br />

public Forward(int n) {argument = n;}<br />

public void run() {turtle.forward(argument);}<br />

public String toString() {return "forward "+argument+"\n";}<br />

}<br />

public class Back extends Command {<br />

private int argument;<br />

public Back(int n) {argument = n;}<br />

public void run() {turtle.forward(-argument);}<br />

public String toString() {return "back "+argument+"\n";}<br />

}<br />

public class Right extends Command {<br />

private int argument;<br />

public Right(int n) {argument = n;}<br />

public void run() {turtle.right(argument);}<br />

public String toString() {return "right "+argument+"\n";}<br />

}<br />

239

Hurra! Ihre Datei wurde hochgeladen und ist bereit für die Veröffentlichung.

Erfolgreich gespeichert!

Leider ist etwas schief gelaufen!