<html><head><meta name="color-scheme" content="light dark"></head><body><pre style="word-wrap: break-word; white-space: pre-wrap;">import javax.swing.*;
import java.awt.*;

/** An instance is a turtle on a JFrame. All turtles use the same JFrame.&lt;br&gt;&lt;br&gt;
    A turtle is a spot on the JFrame that you can move from place
    to place*/
public class Turtle {
    private static JFrame jframe;    // The frame in which all turles move
    private static JPanel panel;     // The panel in the jframe
    private static Graphics graphics;// The graphics associated with the panel
    
    /** The panel on which the turtle moves is of size (width, height). */
    public static int width= 500; 
    /** The panel on which the turtle moves is of size (width, height). */
    public static int height= 500;
     
    private int x, y;  // Current turtle point is (x,y)
    private int angle; // Current turtle angle: 0 &lt;= angle &lt; 360
                       // 0: east, 90:north, 180: west, 270 south
    private boolean penDown= true; // = "the pen is down"
    private Color turtleColor= Color.black;  // color of the turtle
    
    /** Constructor: a black turtle starting at the middle of the
                     panel with angle 0 (looking east).
        East (right) is angle 0; north (left), 90; west (left), 180;
        South (down). 270. The pen is down.*/
    public Turtle() {
        this(width/2, height/2, 0);
    }
    
    /** Constructor: a black turtle starting at (x,y) with angle ang.
        East (right) is angle 0; north (left), 90; west (left), 180;
        South (down), 270. The pen is down. */
    public Turtle(int x, int y, int ang) {
        if (jframe == null) {
            jframe= new JFrame("Turtle window");
            panel= new JPanel();
            panel.setPreferredSize(new Dimension(width, height));
            jframe.getContentPane().add(panel, BorderLayout.CENTER);
            jframe.pack();
            jframe.show();
            graphics= panel.getGraphics();
            clear();
        }
        
        this.x= x;
        this.y= y;
        angle= ang;
    }
    
    /** = x-coordinate of the turtle */
    public int getX() {
        return x;   
    }
    
    /** = width of panel */
    public int getWidth() {
        return width;   
    }
    
    /** = height of the panel */
    public int getHeight() {
        return height;   
    }
    
    /** = y-coordinate of the panel */
    public int getY() {
        return y;   
    }
 
    /** = angle of the turtle */
    public int getAngle() {
        return angle;   
    }
    
    /** Set the angle to a */
    public void setAngle(int a) {
        angle= mod(a, 360);
    }
    
    /** Add a to the angle */
    public void addAngle(int a) {
        angle= mod (angle + a, 360);
    }
    
    /** Lift pen */
    public void liftPen() {
        penDown= false;   
    }
    
    /** Put pen down */
    public void putPenDown() {
        penDown= true;   
    }
    
    /** Set the color of the turtle to c */
    public void setColor(Color c) {
        turtleColor= c;
    }
    
    /** = the current color */
    public Color getColor() {
        return turtleColor;
    }
    
    /** Move the turtle to (x,y), without drawing,
        and face it at angle ang */
    public void moveTo(int x, int y, int ang) {
        Color save= graphics.getColor();
        graphics.setColor(turtleColor);
        this.x= x;
        this.y= y;
        angle= ang;
        graphics.setColor(save);
    }
    
    /** Draw a circle with center at current pen position of diameter d */
    public void drawCircle(int d) {
        Color save= graphics.getColor();
        graphics.setColor(turtleColor);
        graphics.drawOval(x-d/2, y-d/2, d, d);
        graphics.setColor(save);
    }
    
    /** Fill a circle with center at current pen position of diameter d */
    public void fillCircle(int d) {
        Color save= graphics.getColor();
        graphics.setColor(turtleColor);
        graphics.fillOval(x-d/2, y-d/2, d, d);
        graphics.setColor(save);
    }
    
    /** Draw a rectangle with center at current pen position of width w, height h */
    public void drawRectangle(int w, int h) {
        Color save= graphics.getColor();
        graphics.setColor(turtleColor);
        graphics.drawRect(x-w/2, y-h/2, w, h);
        graphics.setColor(save);
    }
    
    /** Fill a rectangle with center at current pen position of width w, height h */
    public void fillRectangle(int w, int h) {
        Color save= graphics.getColor();
        graphics.setColor(turtleColor);
        graphics.fillRect(x-w/2, y-h/2, w, h);
        graphics.setColor(save);
    }
    
    /** Move the turtle d units in its current direction.
        If the pen is down, a line will be drawn; if the pen
        is up, it won't be drawn.*/
    public void move(int d) {
        double rAngle= (angle * Math.PI) / 180;
        int newX= (int) Math.round(x + Math.cos(rAngle) * d);
        int newY= (int) Math.round(y - Math.sin(rAngle) * d);
        if (penDown) {
            Color save= graphics.getColor();
            graphics.setColor(turtleColor);
            graphics.drawLine(x, y, newX, newY);
            graphics.setColor(save);
        }
        x= newX;
        y= newY;
    }
    
    /** = x mod y (for y != 0) */
    private int mod(int x, int y) {
        int ans= x % y;
        if (ans &lt; 0) 
            ans= ans + y;
        return ans;
    }
    
    /** Clear the screen (make it all white) */
    public void clear() {
        Color c= graphics.getColor();
        graphics.setColor(Color.white);
        graphics.fillRect(0, 0, width, height);
        graphics.setColor(c);
    }
    
    /** Pause the program for delay microseconds */
    public void pause(int delay) {
        try { Thread.currentThread().sleep(delay); }
        catch (InterruptedException e) { }
    }
}</pre></body></html>