学以致⽤——Java源码——海龟绘图⼩程序2018版
(TurtlesGraphics)
程序功能:
⽀持⽤户在画板上按以下绘图指令绘制轨迹(类似于⼩海龟在画板上按东、南、西、北⽅向⾏⾛时形成的任意轨迹):
1:抬起画笔
2:落下画笔
3:右转
4:左转
5:画笔向前移动n步(第⼀次移动时默认为向右移动,n为⾃定义)
6:绘制图形
7:擦除图形
9:退出程序
参考⽂章:
简单的海龟绘图语⾔(Design a Turtle Graphics language which can be used to draw "L" graphics),
运⾏效果⽰例:
辅助⼯具:
Excel中使⽤条件格式化,可以快速设计要使⽤海龟绘图语⾔(本程序)绘制的图形,并可帮助我们输⼊指令(前进⽅向,前进步数)。
运⾏过程⽰例:
********欢迎使⽤海龟绘图语⾔********
请按照以下说明输⼊绘图指令:
1:抬起画笔
2:落下画笔
3:右转
3:右转
4:左转
5:画笔向前移动n步(第⼀次移动时默认为向右移动,n为⾃定义)6:绘制图形
7:擦除图形
9:退出程序
指令(当前位置:【1,1】,当前朝向:东):5
请输⼊移动步数:6
已向前移动6步,当前位置:【1,7】,当前朝向:东
指令(当前位置:【1,7】,当前朝向:东):3
朝向已更改为:南
指令(当前位置:【1,7】,当前朝向:南):5
请输⼊移动步数:1
已向前移动1步,当前位置:[2,7],当前朝向:南
指令(当前位置:【2,7】,当前朝向:南):2
画笔已落下。
指令(当前位置:【2,7】,当前朝向:南):4
朝向已更改为:东
指令(当前位置:【2,7】,当前朝向:东):5
请输⼊移动步数:5
已向前移动5步,当前位置:【2,12】,当前朝向:东
指令(当前位置:【2,12】,当前朝向:东):3
朝向已更改为:南
指令(当前位置:【2,12】,当前朝向:南):5
请输⼊移动步数:3
已向前移动3步,当前位置:[5,12],当前朝向:南
指令(当前位置:【5,12】,当前朝向:南):4
朝向已更改为:东
指令(当前位置:【5,12】,当前朝向:东):5
请输⼊移动步数:4
已向前移动4步,当前位置:【5,16】,当前朝向:东
指令(当前位置:【5,16】,当前朝向:东):3
朝向已更改为:南
指令(当前位置:【5,16】,当前朝向:南):5
请输⼊移动步数:6
已向前移动6步,当前位置:[11,16],当前朝向:南
指令(当前位置:【11,16】,当前朝向:南):3
朝向已更改为:西
指令(当前位置:【11,16】,当前朝向:西):5
请输⼊移动步数:13
已向前移动13步,当前位置:[11,3],当前朝向:西
指令(当前位置:【11,3】,当前朝向:西):3
朝向已更改为:北
指令(当前位置:【11,3】,当前朝向:北):5
请输⼊移动步数:6
已向前移动6步,当前位置:[5,3],当前朝向:北
指令(当前位置:【5,3】,当前朝向:北):3
朝向已更改为:东
指令(当前位置:【5,3】,当前朝向:东):5
请输⼊移动步数:4
已向前移动4步,当前位置:【5,7】,当前朝向:东
指令(当前位置:【5,7】,当前朝向:东):3
朝向已更改为:南
指令(当前位置:【5,7】,当前朝向:南):3
朝向已更改为:西
朝向已更改为:西
指令(当前位置:【5,7】,当前朝向:西):3
朝向已更改为:北
指令(当前位置:【5,7】,当前朝向:北):5
请输⼊移动步数:3
已向前移动3步,当前位置:[2,7],当前朝向:北
指令(当前位置:【2,7】,当前朝向:北):6
代码如下:
import java.util.Scanner;
//JHTP Exercise 7.10: Sales Commissions
//by pandenghuang@163
/
*7.21 (Turtle Graphics) The Logo language made the concept of turtle graphics famous. Imagine a mechanical  *  turtle that walks around the room under the control of a Java application. The turtle holds a pen in one
*  of two positions, up or down. While the pen is down, the turtle traces out shapes as it moves, and while
*    the pen is up, the turtle moves about freely without writing anything. In this problem, you’ll simulate
*    the operation of the turtle and create a computerized sketchpad.
Use a 20-by-20 array floor that’s initialized to zeros. Read commands from an array that contains them.
Keep track of the current position of the turtle at all times and whether the pen is currently up or down. Assume that the turtle always starts at position (0, 0) of the floor with its pen up. The set of turtle commands
your application must process are shown in Fig. 7.29.
Command -> Meaning
1 ->  Pen up
2  -> Pen down
3 ->  Turn right
4  -> Turn left
5,10 ->  Move forward 10 spaces (replace 10 for a different number of spaces)
6  -> Display the 20-by-20 array
9 ->  End of data (sentinel)
Suppose that the turtle is somewhere near the center of the floor. The following “program” would draw
and display a 12-by-12 square, leaving the pen in the up position:
2
5,12
3
5,12
3
5,12
3
5,12
1
6
9
As the turtle moves with the pen down, set the appropriate elements of array floor to 1s. When the 6 command (display the array) is given, wherever there’s a 1 in the array, display an asterisk or any
character you choose. Wherever there’s a 0, display a blank.
Write an application to implement the turtle graphics capabilities discussed here. Write several turtle
graphics programs to draw interesting shapes. Add other commands to increase the power of your turtle
graphics language.*/
public class TurtleGraphics  {
//private static int commands[]={1,5,2,5,3,5,4,5,3,5,5,3,5,5,5,3,5,5,3,5,4,5,1,6,9}; //绘制“凸”字形
//private static int commands[]={2,5,12,3,5,12,3,5,12,3,5,12,1,6,9}; //绘制“凸”字形
private static int pace=5;
private static int[][] positions = new int[20][20];
private static int row,column;
private static boolean penLocation; //画笔位置(抬起或落下,默认为抬起)
private static String currentDirection = "东"; //初始朝向为东
private static boolean moveLeft,moveRight,moveDownwards,moveUpwards;  //画笔移动⽅向(东、南、西、北)
private static boolean moveLeft,moveRight,moveDownwards,moveUpwards;  //画笔移动⽅向(东、南、西、北) private static int instruction; //⽤户输⼊的单条指令
private static Scanner input = new Scanner(System.in);
public static void main(String[] args)
{
row=0;
column=0;
moveRight=true;
System.out.println("********欢迎使⽤海龟绘图语⾔********");
System.out.printf("%n请按照以下说明输⼊绘图指令:%n"
+ "1:抬起画笔%n"
+ "2:落下画笔%n"
+ "3:右转%n"
+ "4:左转%n"
+ "5:画笔向前移动n步(第⼀次移动时默认为向右移动,n为⾃定义)%n"
+ "6:绘制图形%n"
+ "7:擦除图形%n"
+ "9:退出程序%n%n");
do {
System.out.printf("指令(当前位置:【%d,%d】,当前朝向:%s):",row+1, column+1, currentDirection);
instruction =Int();
if (instruction == 5) {
System.out.printf("请输⼊移动步数:");
pace =Int();
turtleBehaviour(instruction); //如果指令是5,先设置步长,再执⾏前移动作
continue;
}
else turtleBehaviour(instruction);
} while (instruction != 9);
}
public static void turtleBehaviour(int argument)
{
switch (argument)
{
case 1: //抬起画笔
penLocation=false;
System.out.println("画笔已抬起。");
break;
case 2: //落下画笔
penLocation=true;
System.out.println("画笔已落下。");
break;
case 3:// 右转
if (moveRight)  //如果当前移动⽅向是右移,则右转向后移动⽅向变为向下移动
{
moveDownwards=true; //向下移动为true
moveLeft=moveRight=moveUpwards=false; //其他移动⽅向为false
currentDirection="南";
System.out.printf("朝向已更改为:%s%n",currentDirection);
}
else if(moveLeft) //如果当前移动⽅向是左移,则右转向后移动⽅向变为向上移动
{
moveUpwards=true;
moveLeft=moveRight=moveDownwards=false;
currentDirection="北";
System.out.printf("朝向已更改为:%s%n",currentDirection);
}
else if(moveUpwards) //如果当前移动⽅向是上移,则右转向后移动⽅向变为向右移动
{
moveRight=true;
moveLeft=moveUpwards=moveDownwards=false;
moveLeft=moveUpwards=moveDownwards=false;
currentDirection="东";
System.out.printf("朝向已更改为:%s%n",currentDirection);
}
else if(moveDownwards) //如果当前移动⽅向是下移,则右转向后移动⽅向变为向左移动
{
moveLeft=true;
moveUpwards=moveRight=moveDownwards=false;
currentDirection="西";
System.out.printf("朝向已更改为:%s%n",currentDirection);
}
break;
case 4:// 左转
if (moveRight)
{
moveUpwards=true;
moveLeft=moveRight=moveDownwards=false;
currentDirection="北";
System.out.printf("朝向已更改为:%s%n",currentDirection);
}
else if(moveLeft)
{
moveDownwards=true;
moveLeft=moveRight=moveUpwards=false;
currentDirection="南";
System.out.printf("朝向已更改为:%s%n",currentDirection);
}
else if(moveUpwards)
{
moveLeft=true;
moveRight=moveUpwards=moveDownwards=false;有趣的java小程序
currentDirection="西";
System.out.printf("朝向已更改为:%s%n",currentDirection);;
}
else if(moveDownwards)
{
moveRight=true;
moveUpwards=moveLeft=moveDownwards=false;
currentDirection="东";
System.out.printf("朝向已更改为:%s%n",currentDirection);
}
break;
case 5:  //向前移动
if (moveRight)
{
currentDirection="东";
if (penLocation)
for (int j=column;j<column+pace;j++)
positions[row][j]=1;
column+=pace;
System.out.printf("已向前移动%d步,当前位置:【%d,%d】,当前朝向:东%n",pace, row+1, column+1);                          }
else if(moveLeft)
{currentDirection="西";;
if (penLocation)
for (int j=column;j>column-pace;j--)
positions[row][j]=1;
column-=pace;
System.out.printf("已向前移动%d步,当前位置:[%d,%d],当前朝向:西%n",pace, row+1, column+1);
}
else if(moveDownwards)
{currentDirection="南";;
if (penLocation)

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。