Arbeitsblatt 1 – EAD II Turtlegraphik Vorbedingung Starten Sie BlueJ und stellen Sie sicher, dass die Multimediaklassen von Mark Guzdial der Bibliothek hinzugefügt wurden. Dazu öffnen Sie unter BlueJ -> Preferences den Tab “Bibliotheken” (oder Libraries). Wenn dort ein Pfad zu den bookClasses eingetragen ist, ist alles ok. Wenn nicht, müssen Sie sich die Klassen als zip-Datei von der Webseite laden und über die Preferences zur Bibliothek hinzufügen. Aufgabe 1 Laden Sie sich das Programm “Turtle-Programm1” von der Webseite und öffnen Sie es in BlueJ. Testen Sie es. Fügen Sie eine Methode hinzu: public void zeichneRechteck(int a, int b) Testen Sie die Methode. Aufgabe 2 Fügen Sie eine weitere Turtle zur Welt hinzu. Erstellen Sie dafür eine neue Instanzvariable t2 und verwenden Sie folgende Methode: public void neueTurtle() { t2 = new Turtle(30, 50, welt1); System.out.println(t2); } Aufgabe 3 Schreiben Sie eine Methode, die eine Rosette zeichnet. Diese besteht aus mehreren Sechsecken, die um 30 Grad gedreht nacheinander gezeichnet werden. Sie können die Aufgabe aufteilen und eine Methode zeichneSechseck() erstellen, die genau ein Sechseck an der aktuellen Position und Blickrichtung der Turtle zeichnet. Diese Methoden können Sie dann in einer weiteren Methode zeichneRosette() aufrufen, die so viele Sechsecke zeichnet, wie nötig sind, um eine Rosette zu erhalten. Überlegen Sie sich, ob Sie Parameter für die Methoden hinzufügen wollen und wenn ja, welche. Hinweis: In den Turtlegraphiken werden ganzzahlige Werte angegeben, das Zeichnen erfolgt aber über Sinus- und Cosinusfunktionen, die mit double-Werten arbeiten. Das führt dazu, dass bei einer ausreichend großen Zahl von Wiederholungen die Turtle nicht immer an der selben x,yPosition steht. Das kann man ausgleichen, indem man sich die Koordinaten der Startposition merkt und nach jedem Sechseck testet, ob die Turtle wieder auf der Ausgangsposition angekommen ist. Ist das nicht der Fall, errechnet man die Differenz und bewegt die Turtle auf die richtige Position. Aufgabe 4 Schreiben Sie eine Methode, die das Muster aus Bild 2 zeichnet. Es handelt sich um ein Dreieck, das in jedem Durchgang etwas größer wird (ähnlich wie eine Spirale) und zusätzlich noch leicht gedreht ist. Sie sollten zuerst die Befehle für die „Dreiecksspirale“ schreiben und überprüfen, ob das Ergebnis Ihren Erwartungen entspricht. Anschließend fügen Sie die Drehung hinzu. Aufgabe 5 Zeichnen Sie eines der folgenden Muster (1-40) mit verschiedenen Farben und/oder Strichstärken. Tips: Zeichnen Sie die Figuren mit einem Stift auf ein Blatt Papier und achten Sie darauf, wie Sie den Stift bewegen und drehen. Schreiben Sie nicht die ganze Methode auf einmal sondern testen Sie immer wieder, was der gerade hinzugefügte Befehl verändert. Zusatzaufgabe Erstellen Sie weitere Graphiken aus den folgenden Beispielen, auch unter Verwendung weiterer Methoden, die in der Klasse SimpleTurtle zur Verfügung gestellt werden. Class Turtle java.lang.Object SimpleTurtle Turtle public class Turtleextends SimpleTurtle Class that represents a turtle which is similar to a Logo turtle. This class inherts from SimpleTurtle and is for students to add methods to. Copyright Georgia Institute of Technology 2004 Author: Barb Ericson [email protected] Constructor Summary Turtle(int x, int y, ModelDisplay modelDisplayer) Constructor that takes the x and y and a model display to draw it on Turtle(int x, int y, Picture picture) Constructor that takes the x and y and a picture to draw on Turtle(ModelDisplay modelDisplay) Constructor that takes the model display Turtle(Picture p) Constructor that takes a picture to draw on Method Summary static void main(java.lang.String[] args) Methods inherited from class SimpleTurtle , backward, backward, clearPath, drawInfoString, drop, forward, forward, getBodyColor, getDistance, getHeading, getHeight, getInfoColor, getModelDisplay, getName, getPen, getPenColor, getPenWidth, getPicture, getShellColor, getShowInfo, getWidth, getXPos, getYPos, hide, isPenDown, isVisible, moveTo, paintComponent, penDown, penUp, setBodyColor, setColor, setHeading, setHeight, setInfoColor, setModelDisplay, setName, setPen, setPenColor, setPenDown, setPenWidth, setPicture, setShellColor, setShowInfo, setVisible, setWidth, show, toString, turn, turnLeft, turnRight, turnToFace, turnToFace, updateDisplay Methods inherited from class java.lang.Object clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait Constructor Detail Turtle public Turtle(int x, int y, ModelDisplay modelDisplayer) Constructor that takes the x and y and a model display to draw it on Parameters: x - the starting x position y - the starting y position modelDisplayer - the thing that displays the model Turtle public Turtle(int x, int y, Picture picture) Constructor that takes the x and y and a picture to draw on Parameters: x - the starting x position y - the starting y position picture - the picture to draw on Turtle public Turtle(ModelDisplay modelDisplay) Constructor that takes the model display Parameters: modelDisplay - the thing that displays the model Turtle public Turtle(Picture p) Constructor that takes a picture to draw on Parameters: p - the picture to draw on Method Detail main public static void main(java.lang.String[] args) Class SimpleTurtle java.lang.Object SimpleTurtle public class SimpleTurtleextends java.lang.Object Class that represents a Logo-style turtle. The turtle starts off facing north. A turtle can have a name, has a starting x and y position, has a heading, has a width, has a height, has a visible flag, has a body color, can have a shell color, and has a pen. The turtle will not go beyond the model display or picture boundaries. You can display this turtle in either a picture or in a class that implements ModelDisplay. Copyright Georgia Institute of Technology 2004 Author: Barb Ericson [email protected] Constructor Summary SimpleTurtle(int x, int y) Constructor that takes the x and y position for the turtle SimpleTurtle(int x, int y, ModelDisplay display) Constructor that takes the x and y position and the model displayer SimpleTurtle(int x, int y, Picture picture) Constructor that takes the x and y position and the picture to draw on SimpleTurtle(ModelDisplay display) Constructor that takes a model display and adds a turtle in the middle of it SimpleTurtle(Picture picture) Constructor that takes the picture to draw on and will appear in the middle Method Summary void backward() Method to go backward by 100 pixels void backward(int pixels) Method to go backward a given number of pixels void clearPath() Method to clear the path (history of where the turtle has been) void drawInfoString(java.awt.Graphics g) Method to draw the information string void drop(Picture dropPicture) Method to draw a passed picture at the current turtle location and rotation in a picture or model display void forward() Method to move the turtle foward 100 pixels void forward(int pixels) Method to move the turtle forward the given number of pixels java.awt.Color getBodyColor() Method to get the body color double getDistance(int x, int y) Get the distance from the passed x and y location double getHeading() Method to get the current heading int java.awt.Color ModelDisplay java.lang.String Pen java.awt.Color int Picture java.awt.Color boolean getHeight() Method to return the height of this object getInfoColor() Method to get the information color getModelDisplay() Method to get the model display for this simple turtle getName() Method to get the name of the turtle getPen() Method to get the pen getPenColor() Method to get the pen color getPenWidth() Method to get the pen width getPicture() Method to get the picture for this simple turtle getShellColor() Method to get the shell color getShowInfo() Method to get value of show info int getWidth() Method to return the width of this object int getXPos() Method to get the current x position int getYPos() Method to get the current y position void hide() Method to hide the turtle (stop showing it) This doesn't affect the pen status boolean isPenDown() Method to check if the pen is down boolean isVisible() Method to get the value of the visible flag void moveTo(int x, int y) Method to move to turtle to the given x and y location void paintComponent(java.awt.Graphics g) Method to paint the turtle void penDown() Method to set the pen down void penUp() Method to lift the pen up void setBodyColor(java.awt.Color color) Method to set the body color which will also set the pen color void setColor(java.awt.Color color) Method to set the color of the turtle. void setHeading(double heading) Method to set the heading void setHeight(int theHeight) Method to set the height of this object void setInfoColor(java.awt.Color color) Method to set the information color void setModelDisplay(ModelDisplay theModelDisplay) Method to set the model display for this simple turtle void setName(java.lang.String theName) Method to set the name of the turtle void setPen(Pen thePen) Method to set the pen void setPenColor(java.awt.Color color) Method to set the pen color void setPenDown(boolean value) Method to set the pen down boolean variable void setPenWidth(int width) Method to set the pen width void setPicture(Picture pict) Method to set the picture for this simple turtle void setShellColor(java.awt.Color color) Method to set the shell color void setShowInfo(boolean value) Method to show the turtle information string void setVisible(boolean value) Method to set the visible flag void setWidth(int theWidth) Method to set the width of this object void show() Method to show the turtle (doesn't affect the pen status java.lang.String void toString() Method to return a string with information about this turtle turn(double degrees) Method to turn the turtle the passed degrees use negative to turn left and pos to turn right void turnLeft() Method to turn left void turnRight() Method to turn right void turnToFace(int x, int y) Method to turn towards the given x and y void turnToFace(SimpleTurtle turtle) Method to turn to face another simple turtle void updateDisplay() Method to update the display of this turtle and also check that the turtle is in the bounds Methods inherited from class java.lang.Object clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait Constructor Detail SimpleTurtle public SimpleTurtle(int x, int y) Constructor that takes the x and y position for the turtle Parameters: x - the x pos y - the y pos SimpleTurtle public SimpleTurtle(int x, int y, ModelDisplay display) Constructor that takes the x and y position and the model displayer Parameters: x - the x pos y - the y pos display - the model display SimpleTurtle public SimpleTurtle(int x, int y, Picture picture) Constructor that takes the x and y position and the picture to draw on Parameters: x - the x pos y - the y pos picture - the picture to draw on SimpleTurtle public SimpleTurtle(ModelDisplay display) Constructor that takes a model display and adds a turtle in the middle of it Parameters: display - the model display SimpleTurtle public SimpleTurtle(Picture picture) Constructor that takes the picture to draw on and will appear in the middle Parameters: picture - the picture to draw on Method Detail backward public void backward() Method to go backward by 100 pixels backward public void backward(int pixels) Method to go backward a given number of pixels Parameters: pixels - the number of pixels to walk backward clearPath public void clearPath() Method to clear the path (history of where the turtle has been) drawInfoString public void drawInfoString(java.awt.Graphics g) Method to draw the information string Parameters: g - the graphics context drop public void drop(Picture dropPicture) Method to draw a passed picture at the current turtle location and rotation in a picture or model display Parameters: dropPicture - the picture to drop forward public void forward() Method to move the turtle foward 100 pixels forward public void forward(int pixels) Method to move the turtle forward the given number of pixels Parameters: pixels - the number of pixels to walk forward in the heading direction getBodyColor public java.awt.Color getBodyColor() Method to get the body color Returns: the body color getDistance public double getDistance(int x, int y) Get the distance from the passed x and y location Parameters: x - the x location y - the y location getHeading public double getHeading() Method to get the current heading Returns: the heading in degrees getHeight public int getHeight() Method to return the height of this object Returns: the height in pixels getInfoColor public java.awt.Color getInfoColor() Method to get the information color Returns: the color of the information string getModelDisplay public ModelDisplay getModelDisplay() Method to get the model display for this simple turtle Returns: the model display if there is one else null getName public java.lang.String getName() Method to get the name of the turtle Returns: the name of this turtle getPen public Pen getPen() Method to get the pen Returns: the pen getPenColor public java.awt.Color getPenColor() Method to get the pen color Returns: the pen color getPenWidth public int getPenWidth() Method to get the pen width Returns: the width of the pen in pixels getPicture public Picture getPicture() Method to get the picture for this simple turtle Returns: the picture for this turtle (may be null) getShellColor public java.awt.Color getShellColor() Method to get the shell color Returns: the shell color getShowInfo public boolean getShowInfo() Method to get value of show info Returns: true if should show info, else false getWidth public int getWidth() Method to return the width of this object Returns: the width in pixels getXPos public int getXPos() Method to get the current x position Returns: the x position (in pixels) getYPos public int getYPos() Method to get the current y position Returns: the y position (in pixels) hide public void hide() Method to hide the turtle (stop showing it) This doesn't affect the pen status isPenDown public boolean isPenDown() Method to check if the pen is down Returns: true if down else false isVisible public boolean isVisible() Method to get the value of the visible flag Returns: true if visible else false moveTo public void moveTo(int x, int y) Method to move to turtle to the given x and y location Parameters: x - the x value to move to y - the y value to move to paintComponent public void paintComponent(java.awt.Graphics g) Method to paint the turtle Parameters: g - the graphics context to paint on penDown public void penDown() Method to set the pen down penUp public void penUp() Method to lift the pen up setBodyColor public void setBodyColor(java.awt.Color color) Method to set the body color which will also set the pen color Parameters: color - the color to use setColor public void setColor(java.awt.Color color) Method to set the color of the turtle. This will set the body color Parameters: color - the color to use setHeading public void setHeading(double heading) Method to set the heading Parameters: heading - the new heading to use setHeight public void setHeight(int theHeight) Method to set the height of this object Parameters: theHeight - the height in pixels setInfoColor public void setInfoColor(java.awt.Color color) Method to set the information color Parameters: color - the new color to use setModelDisplay public void setModelDisplay(ModelDisplay theModelDisplay) Method to set the model display for this simple turtle Parameters: theModelDisplay - the model display to use setName public void setName(java.lang.String theName) Method to set the name of the turtle Parameters: theName - the new name to use setPen public void setPen(Pen thePen) Method to set the pen Parameters: thePen - the new pen to use setPenColor public void setPenColor(java.awt.Color color) Method to set the pen color Parameters: color - the color for the pen ink setPenDown public void setPenDown(boolean value) Method to set the pen down boolean variable Parameters: value - the value to set it to setPenWidth public void setPenWidth(int width) Method to set the pen width Parameters: width - the width to use in pixels setPicture public void setPicture(Picture pict) Method to set the picture for this simple turtle Parameters: pict - the picture to use setShellColor public void setShellColor(java.awt.Color color) Method to set the shell color Parameters: color - the color to use setShowInfo public void setShowInfo(boolean value) Method to show the turtle information string Parameters: value - the value to set showInfo to setVisible public void setVisible(boolean value) Method to set the visible flag Parameters: value - the value to set it to setWidth public void setWidth(int theWidth) Method to set the width of this object Parameters: theWidth - in width in pixels show public void show() Method to show the turtle (doesn't affect the pen status toString public java.lang.String toString() Method to return a string with informaiton about this turtle Overrides: toString in class java.lang.Object Returns: a string with information about this object turn public void turn(double degrees) Method to turn the turtle the passed degrees use negative to turn left and pos to turn right Parameters: degrees - the amount to turn in degrees turnLeft public void turnLeft() Method to turn left turnRight public void turnRight() Method to turn right turnToFace public void turnToFace(int x, int y) Method to turn towards the given x and y Parameters: x - the x to turn towards y - the y to turn towards turnToFace public void turnToFace(SimpleTurtle turtle) Method to turn to face another simple turtle updateDisplay public void updateDisplay() Method to update the display of this turtle and also check that the turtle is in the bounds