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