import java.awt.*;public class LogoCanvas extends Canvas {  Image im;  int w, h;  double x=100, y=100, ang;  boolean penDown, autoRepaint=true;  Rectangle clipRect=new Rectangle();    protected Rectangle turtleRect() {    return new Rectangle((int)x-4, (int)y-4, 10, 10);  }    public void penUp() {    penDown = false;  }  public void penDown() {    penDown = true;  }  public void back(double d) {    forward(-d);  }  public void forward(double d) {    double px=x, py=y;    Rectangle r = turtleRect();    x += d*Math.cos(ang);    y += d*Math.sin(ang);    if (penDown) {      Graphics g = im.getGraphics();      g.setColor(getForeground());      g.setClip(r.union(turtleRect()));      g.drawLine((int)px, (int)py, (int)x, (int)y);    }    clipRect = turtleRect().union(clipRect);    if (autoRepaint) repaint();  }  public void right(double d) {    ang += d*Math.PI/180;    if (autoRepaint) repaint();  }  public void left(double d) {    ang -= d*Math.PI/180;    if (autoRepaint) repaint();  }    public void clear() {    Graphics g = im.getGraphics();    g.setColor(getBackground());    g.fillRect(0,0,w,h);    clipRect = new Rectangle(0,0,w,h);    repaint();  }    public void update(Graphics g) { paint(g); }  public void paint(Graphics g) {    if (im==null) setSize(getSize());    if (g.getClip() == null) g.setClip(clipRect);    else if (g.getClip() instanceof Rectangle) g.setClip(clipRect.union((Rectangle)g.getClip()));    else g.setClip(new Rectangle(0,0,w,h));    g.drawImage(im,0,0,this);    g.setColor(getBackground());    if (penDown) g.setColor(new Color(255, 223, 0));    else g.setColor(Color.yellow);    g.fillOval((int)x-4, (int)y-4, 9, 9);    g.setColor(getForeground());    g.setColor(Color.black);    g.drawOval((int)x-4, (int)y-4, 9, 9);    g.drawLine((int)x, (int)y, (int)(x+3*Math.cos(ang)), (int)(y+3*Math.sin(ang)));    clipRect = turtleRect();  }  public void setSize(Dimension d) { setSize(d.width, d.height); }  public void setSize(int x, int y) {    if (im == null) {      if (x<=0) x = 300;      if (y<=0) y = 300;      im = createImage(x, y);    }    else if (im.getWidth(this) < x || im.getWidth(this) < y) {      Image im2 = createImage(x, y);      Graphics g = im2.getGraphics();      g.drawImage(im,0,0,this);      im = im2;    }    w=x; h=y;     super.setSize(x, y);  }}