为少儿进行java编程启蒙设计的Turtle类

为少儿进行java编程启蒙设计的Turtle类

import java.awt.*;

/**
* 这个类是用于少儿进行java编程启蒙的在,在类中定义了如下属性和方法.
* x,y对应x和y坐标
* angle对应和x轴的角度,
* pencolor为画笔颜色
* penwidth 为笔触大小
* penstatus 为笔的状态,为true表示落笔了,为false表示抬笔.
*/
public class Turtle {

private int x;
private int y;
private int angle;
private Color penColor;
private boolean penstatus;

static {
//设置直角坐标系
StdDraw.setXscale(-100,100);
StdDraw.setYscale(-100, 100);

}

public Turtle() {
x = 5;
y = 5;
angle = 45;
penColor = StdDraw.BLACK;

StdDraw.setPenRadius(0.01);
StdDraw.setPenColor(penColor);

}

/**
* Construct a new Turtle with the specified parameters. The
* new Turtle’s pen will be up.
*
* @param initX the x coordinate for the new Turtle.
* @param initY the y coordinate for the new Turtle.
* @param initAngle the angle for the new Turtle.
* @param initColor the color of the new Turtle’s pen.
*/
public Turtle(int initX, int initY, int initAngle, Color initColor) {
x=initX;
y=initY;
angle=initAngle;
penColor = initColor;
penPosition = PEN_UP;
}

/**
* Move this Turtle forward by the specified number of
* screen pixels.
*
* @param pixels the number of screen pixes by which
* to move this Turtle forward.
*/
public void moveForward(int pixels) {
double oldx = x;
double oldy = y;

double radAngle = Math.toRadians(angle);
x = x + (int)Math.round(Math.cos(radAngle) * pixels);
y = y – (int)Math.round(Math.sin(radAngle) * pixels);
System.out.println(oldx+ “,” + oldy);
System.out.println(x + “,” + y);
StdDraw.line(oldx, oldy, x, y);
}

/**
* Rotate this Turtle counter-clockwise by the specified
* number of degrees.
*
* @param degrees the number of degrees by which to rotate
* this Turtle.
*/
public void rotate(int degrees) {
int newAngle = angle + degrees;
angle = newAngle % 360;
}

/**
* Put this Turtle’s pen down. When this Turtle’s pen is
* down it will draw a line in its color as it moves
* forward.
*/
public void putPenDown() {
penPosition = PEN_DOWN;
}

/**
* Pick this Turtle’s pen up. When this Turtle’s pen is
* up it will not draw a line as it moves forward.
*/
public void pickPenUp() {
penPosition = PEN_UP;
}

/**
* Ask this Turtle if it’s pen is up or down. The value
* returned will be either PEN_UP or PEN_DOWN.
*
* @return PEN_DOWN if this Turtle’s pen is up or
* PEN_UP if this Turtle’s pen is up.
*/
public boolean getPenPosition() {
return penPosition;
}

/**
* Get the x coordinate of this Turtle.
*
* @return the x coordinate of this Turtle.
*/
public int getX() {
return x;
}

/**
* Get the y coordinate of this Turtle.
*
* @return the y coordinate of this Turtle.
*/
public int getY() {
return y;
}

/**
* Get the color of this Turtle’s pen. The color is
* returned as a reference to a Color object.
*
* @return a reference to a Color object representing
* the Color of this Turtle’s pen.
*/
public Color getColor() {
return penColor;
}

/**
* Get the the angle to which this Turtle is turned.
* The angle of the Turtle is measured counter-clockwise
* from horizontal.
*
* @return the angle to which this Turtle is turned.
*/
public int getAngle() {
return angle;
}

//only test
public static void main(String[] args){

Turtle t = new Turtle();
t.moveForward(10);

}
}

李兴球

李兴球的博客是Python创意编程原创博客