简单的海龟绘图语言(Design a Turtle Graphics language which can be used to draw "L" graphics)

// Design a Turtle Graphics language which can be used to draw "L" graphics
// Java how to program, 5/e, Exercise 7.21
import  javax.swing. * ;
import  java.awt. * ;
import  java.awt.event. * ;
public   class  TurtleGraphics  extends  JApplet  implements  ActionListener  {
 
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};
 
int pace=5;
 
int[][] positions;
 
int row,column;
 
boolean penLocation=false, moveLeft,moveRight,moveDownwards,moveUpwards,moveDirection; 
 JLabel instructionsLabel,outputLabel;
 JTextArea instructions;
 JTextArea graphicsDrawn;
 JButton drawButton;
 
 
public void init()
 
{
     
     Container  container
=getContentPane();
     container.setLayout(
new FlowLayout());
     
     instructionsLabel
= new JLabel("Instructions to the Turtle:");
     container.add(instructionsLabel);
     
     instructions
=new JTextArea(3,9);
     container.add(instructions);
     
for (int i=0;i<commands.length;i++)
         
{
         instructions.append(commands[i]
+" ");
         }

     drawButton
=new JButton("Draw Turtle Graphics");
     container.add(drawButton);
     drawButton.addActionListener(
this);
     
     outputLabel
=new JLabel("Turtle Graphics drawn is as follows:");
     container.add(outputLabel);
     
     graphicsDrawn
=new JTextArea();
     container.add(graphicsDrawn);
     graphicsDrawn.setFont(
new Font("Monospaced",Font.PLAIN,12));
     
     positions
=new int[20][20];
     
for (int i=0;i<20;i++)
         
for(int j=0;j<20;j++)
             positions[i][j]
=0;
     
 }

 
 
public void actionPerformed (ActionEvent event)
 
{
     row
=0;
     column
=0;
     moveDirection
=false;
     moveRight
=true;
     graphicsDrawn.setText(
"");
     
for (int i=0;i<commands.length;i++)
         turtleBehaviour(commands[i]);

 }

 
 
public void turtleBehaviour(int argument)
 
{
     
switch (argument)
     
{
     
case 1:
         penLocation
=false;
         
break;
     
case 2:
         penLocation
=true;
         
break;
     
case 3:// turn right
         if (moveDirection==moveRight)
         
{
             moveDirection
=moveDownwards=true;
             moveLeft
=moveRight=moveUpwards=false;
         }

         
else if(moveDirection==moveLeft)
         
{
             moveDirection
=moveUpwards=true;
             moveLeft
=moveRight=moveDownwards=false;
             
         }

         
else if(moveDirection==moveUpwards)
         
{
             moveDirection
=moveRight=true;
             moveLeft
=moveUpwards=moveDownwards=false;
         }

         
else if(moveDirection==moveDownwards)
         
{
             moveDirection
=moveLeft=true;
             moveUpwards
=moveRight=moveDownwards=false;
         }

         
         
break;
     
case 4:// turn left
         if (moveDirection==moveRight)
         
{
             moveDirection
=moveUpwards=true;
             moveLeft
=moveRight=moveDownwards=false;
         }

         
else if(moveDirection==moveLeft)
         
{
             moveDirection
=moveDownwards=true;
             moveLeft
=moveRight=moveUpwards=false;
             
         }

         
else if(moveDirection==moveUpwards)
       
{
             moveDirection
=moveLeft=true;
             moveRight
=moveUpwards=moveDownwards=false;
         }

         
else if(moveDirection==moveDownwards)
         
{
             moveDirection
=moveRight=true;
             moveUpwards
=moveLeft=moveDownwards=false;
         }

         
         
break;
     
case 5:  //move forward
         if (moveRight)
             
{
             moveDirection
=moveRight;
             
if (penLocation)
                 
for (int j=0;j<pace;j++)
                     positions[row][column
+j]=1;
             column
+=pace-1;
                          }

         
else if(moveLeft)
        
{moveDirection=moveLeft;
             
if (penLocation)
                 
for (int j=0;j<pace;j++)
                     positions[row][column
-j]=1;
             column
-=pace-1;
         }

         
else if(moveDownwards)
         
{moveDirection=moveDownwards;
             
if (penLocation)
              
for (int i=0;i<pace;i++)
                     positions[row
+i][column]=1;
                 row
+=pace-1;
                 }

         
else if(moveUpwards)
        
{moveDirection=moveUpwards;
             
if (penLocation)
                 
for (int i=0;i<pace;i++)
                     positions[row
-i][column]=1;
                 row
-=pace-1;
         }

         
break;
     
case 6://print turtle graphics
         graphicsDrawn.append("Turtle begins to draw:/n/n");
         
for (int i=0;i<20;i++)
             
{
             
for (int j=0;j<20;j++)
             
{
             
if (positions[i][j]==0)
                 graphicsDrawn.append(
"-");
             
else
                 graphicsDrawn.append(
"@");
             }

             graphicsDrawn.append(
"/n");
             }

         graphicsDrawn.append(
"/n/n");
         
break;
     
case 9://terminate the program
         graphicsDrawn.append("Program terminated!/n");
         
break;
     
     }

        
     }


 }

 

A snapshot of  the applet's execution:

TurtleGraphics

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值