Climbing的专栏

Life is limited, but art is long

huang ClimbingID:hpdlzu80100
[修改头像]
1560次访问,排名2万外好友5人,关注者6
热爱生活,积极向上,流自己的汗,吃自己的饭!
hpdlzu80100的文章
原创 23 篇
翻译 0 篇
转载 2 篇
评论 0 篇
最近评论
软件项目交易
订阅我的博客
XML聚合  FeedSky
订阅到鲜果
订阅到Google
订阅到抓虾
订阅到BlogLines
订阅到Yahoo
订阅到GouGou
订阅到飞鸽
订阅到Rojo
订阅到newsgator
订阅到netvibes
文章分类
收藏
    相册
    存档

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

    新一篇: 武士之旅小游戏(Chess Board game: Tour of knight)

    //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

     

    发表于 @ 2008年04月21日 15:49:00|评论(loading...)|编辑

    旧一篇: 掷双骰儿游戏的公平性分析(Evaluate the justice of Craps game)

    评论:没有评论。

    发表评论  


    登录
    Csdn Blog version 3.1a
    Copyright © Climbing