Turtle Graphics Program

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • JulianP
    New Member
    • Nov 2006
    • 11

    Turtle Graphics Program

    Hi I am working on a Turtle Program and I have put in all the fancy schmancy stuff in I just cant seem to figure out how to print the T, which is my turtle, on the screen. Here is the code for my program so far!

    Code:
    /* Julian Baranowski
    Date:12/7/2006
    Revised:
    Description: Turtle Graphics Program
    */
    
    
    import javax.swing.*;
    import java.awt.event.*;
    import java.awt.*;
    import java.io.*;
    
    public class P7_21 extends JFrame implements ActionListener
    {
    	int floor[][];
    	JTextArea output, input;
    	JButton enter,exit;
    	JScrollPane jsp;
    	JTextField ent;
    	Container container;
    	int move[] ={8,6,4,2};
    	int moved = 0;
    	final int LEFT = 4, RIGHT = 6, UP = 8, DOWN = 2;
    	String left,right,up,down;
    
    	public P7_21()
    	{
    		left="";
    		right="";
    		up="";
    		down="";
    		container= getContentPane();
    		container.setLayout(null);
    
    
    		floor = new int [20][20];
    		setSize(800,500);
    
    		output = new JTextArea(20,50);
    		output.setFont(new Font("Monospaced",Font.PLAIN,18));
    		//output.setText("TEST");
    		output.setBounds(5,5,350,300);
    		output.setEditable(false);
    		container.add(output);
    
    		input = new JTextArea(5,50);
    		input.setFont(new Font("Monospaced",Font.PLAIN,12));
    		jsp = new JScrollPane(input);
    		jsp.setBounds(430,5,360,300);
    		//container.add(jsp);
    
    		container.add(jsp);
    
    		ent = new JTextField();
    		ent.setBounds(300,350,200,20);
    		ent.setEditable(true);
    		ent.addActionListener(this);
    		container.add(ent);
    
    
    		enter = new JButton("Enter");
    		enter.addActionListener(this);
    		enter.setBounds(300,400,100,20);
    		container.add(enter);
    
    		exit = new JButton("Exit");
    		exit.addActionListener(this);
    		exit.setBounds(400,400,100,20);
    		getContentPane().add(exit);
    		container.add(exit);
    		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		this.dispose();
    
    		print();
    
    		setVisible(true);
    
    		//int floor = { {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}}; //(Finish next time)
    	}
    
    	public void actionPerformed(ActionEvent evt)
    	{
    
    		if(evt.getActionCommand().equals("Exit"))
    			{
    				int choice = JOptionPane.showConfirmDialog(null,
    					" You Pressed "+ evt.getActionCommand()+" is this what you want to do? ",
    					" Quit Frame ",
    					JOptionPane.YES_NO_OPTION);
    				if( choice == 0)
    				this.dispose();
    			}
    
    
    
    		if(evt.getSource()==enter || evt.getSource()==ent)
    		{
    			if(ent.getText().toLowerCase().compareTo("left")== 0)
    			{
    				left ="Key:4 (LEFT)\n";
    			System.out.println("Left");
    				input.append(left);
    				ent.setText("");
    			}//end left
    
    			if(ent.getText().toLowerCase().compareTo("right")==0)
    			{
    				right ="Key:6 (RIGHT)\n";
    			System.out.println("right");
    				input.append(right);
    				ent.setText("");
    			}//end right
    
    			if(ent.getText().toLowerCase().compareTo("up")==0)
    			{
    				up ="Key:8 (UP)\n";
    			System.out.println("Up");
    				input.append(up);
    				ent.setText("");
    			}//end up
    
    			if(ent.getText().toLowerCase().compareTo("down")==0)
    			{
    				down ="Key:2 (DOWN)\n";
    			System.out.println("Down");
    				input.append(down);
    				ent.setText("");
    			}//end down
    
    		}
    
    	}
    
    	void print()
    	{
    		for(int x=0;x<floor.length;x++)
    		{
    			for(int y=0;y<=floor[0].length;y++)
    			{
    				if(y==floor[0].length)
    				{
    					output.append("\n");
    					continue;
    				}//end if
    				if(floor[x][y]==0)
    					output.append("0");
    				else
    				if(floor[x][y]==1)
    					output.append("T");
    
    			}
    		}
    	}
    
    
    		public static void main(String args[])
    		{
    			new P7_21();
    		}
    }
    Any input is appreciated thanks!

    -Julian
  • horace1
    Recognized Expert Top Contributor
    • Nov 2006
    • 1510

    #2
    made a few modifications to your program - as you type right the T moves across the pattern - is this what you require?
    Code:
    /* Julian Baranowski
    Date:12/7/2006
    Revised:
    Description: Turtle Graphics Program
    */
    
    
    import javax.swing.*;
    import java.awt.event.*;
    import java.awt.*;
    import java.io.*;
    
    public class P7_21 extends JFrame implements ActionListener
    {
            int floor[][];
            JTextArea output, input;
            JButton enter,exit;
            JScrollPane jsp;
            JTextField ent;
            Container container;
            int move[] ={8,6,4,2};
            int moved = 0;
            int xpos=0, ypos=0;    // ** added
            final int LEFT = 4, RIGHT = 6, UP = 8, DOWN = 2;
            String left,right,up,down;
    
            public P7_21()
            {
                    left="";
                    right="";
                    up="";
                    down="";
                    container= getContentPane();
                    container.setLayout(null);
    
    
                    floor = new int [20][20];
                    setSize(800,500);
    
                    output = new JTextArea(20,50);
                    output.setFont(new Font("Monospaced",Font.PLAIN,18));
                    //output.setText("TEST");
                    output.setBounds(5,5,350,300);
                    output.setEditable(false);
                    container.add(output);
    
                    input = new JTextArea(5,50);
                    input.setFont(new Font("Monospaced",Font.PLAIN,12));
                    jsp = new JScrollPane(input);
                    jsp.setBounds(430,5,360,300);
                    //container.add(jsp);
    
                    container.add(jsp);
    
                    ent = new JTextField();
                    ent.setBounds(300,350,200,20);
                    ent.setEditable(true);
                    ent.addActionListener(this);
                    container.add(ent);
    
    
                    enter = new JButton("Enter");
                    enter.addActionListener(this);
                    enter.setBounds(300,400,100,20);
                    container.add(enter);
    
                    exit = new JButton("Exit");
                    exit.addActionListener(this);
                    exit.setBounds(400,400,100,20);
                    getContentPane().add(exit);
                    container.add(exit);
                    setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                    this.dispose();
    
                    print();
    
                    setVisible(true);
    
                    //int floor = { {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}}; //(Finish next time)
            }
    
            public void actionPerformed(ActionEvent evt)
            {
    
                    if(evt.getActionCommand().equals("Exit"))
                            {
                                    int choice = JOptionPane.showConfirmDialog(null,
                                            " You Pressed "+ evt.getActionCommand()+" is this what you want to do? ",
                                            " Quit Frame ",
                                            JOptionPane.YES_NO_OPTION);
                                    if( choice == 0)
                                    this.dispose();
                            }
    
    
    
                    if(evt.getSource()==enter || evt.getSource()==ent)
                    {
                             floor[xpos][ypos]=0;    // clear old T ** added
    
                            if(ent.getText().toLowerCase().compareTo("left")== 0)
                            {
                                    left ="Key:4 (LEFT)\n";
                            System.out.println("Left");
                                    input.append(left);
                                    ent.setText("");
                            }//end left
    
                            if(ent.getText().toLowerCase().compareTo("right")==0)
                            {
                                    right ="Key:6 (RIGHT)\n";
                            System.out.println("right");
                                    input.append(right);
                                    ent.setText("");
                                    if(ypos < 10 )ypos++;   // ** addded
                            }//end right
    
                            if(ent.getText().toLowerCase().compareTo("up")==0)
                            {
                                    up ="Key:8 (UP)\n";
                            System.out.println("Up");
                                    input.append(up);
                                    ent.setText("");
                            }//end up
    
                            if(ent.getText().toLowerCase().compareTo("down")==0)
                            {
                                    down ="Key:2 (DOWN)\n";
                            System.out.println("Down");
                                    input.append(down);
                                    ent.setText("");
                            }//end down
                       floor[xpos][ypos]=1;    // move T ** added
                        print();
                    }
    
            }
    
            void print()
            {
                    output.setText("");   // ** added
                    for(int x=0;x<floor.length;x++)
                    {
                            for(int y=0;y<=floor[0].length;y++)
                            {
                                    if(y==floor[0].length)
                                    {
                                            output.append("\n");
                                            continue;
                                    }//end if
                                    if(floor[x][y]==0)
                                            output.append("0");
                                    else
                                    if(floor[x][y]==1)
                                            output.append("T");
    
                            }
                    }
             //  paint();
            }
    
    
                    public static void main(String args[])
                    {
                            new P7_21();
                    }
    }

    Comment

    • JulianP
      New Member
      • Nov 2006
      • 11

      #3
      Thanks,
      I was looking for it to print a T when i put in left,right,up, or down.

      not sure if what you did actually does something... I can see where your headed but right now it doesn't display a T when I type in right. I am useing Textpad if that helps at all.

      Comment

      • r035198x
        MVP
        • Sep 2006
        • 13225

        #4
        Originally posted by JulianP
        Thanks,
        I was looking for it to print a T when i put in left,right,up, or down.

        not sure if what you did actually does something... I can see where your headed but right now it doesn't display a T when I type in right. I am useing Textpad if that helps at all.
        Sorry for being the dumb one here but could you please repeat again what you want to be printed where when what happens?

        Comment

        • JulianP
          New Member
          • Nov 2006
          • 11

          #5
          In my program I am setting up a JTextField whith an array of [20][20] and it fills it with 0's. Right now I want it to start in the top left corner and when you type in the command right,left,up,d own it will replace the 0 with a T. Here's a Copy of the program agian.

          Code:
          /* /* Julian Baranowski
          Date:12/7/2006
          Revised:
          Description: Turtle Graphics Program
          */
          
          
          import javax.swing.*;		// ** import statements
          import java.awt.event.*;	// ''
          import java.awt.*;			// ''
          import java.io.*;			// ''
          
          
          public class P7_211 extends JFrame implements ActionListener
          {
                  int floor[][];
                  JTextArea output, input;
                  JButton enter,exit;
                  JScrollPane jsp;
                  JTextField ent;
                  Container container;
                  int move[] ={8,6,4,2};
                  //int moved = 0;
                  int xpos=0, ypos=0;    // ** added
                  /*final*/ int LEFT = 4, RIGHT = 6, UP = 8, DOWN = 2;
                  String left,right,up,down;
          
                  public P7_211()
                  {
                          left="";
                          right="";
                          up="";
                          down="";
                          container= getContentPane();
                          container.setLayout(null);
          
          
                          floor = new int [20][20];
                          setSize(800,500);
          
                          output = new JTextArea(20,50);
                          output.setFont(new Font("Monospaced",Font.PLAIN,18));
                          //output.setText("TEST");
                          output.setBounds(5,5,350,300);
                          output.setEditable(false);
                          container.add(output);
                          // ** output textfield
          
                          input = new JTextArea(5,50);
                          input.setFont(new Font("Monospaced",Font.PLAIN,12));
                          jsp = new JScrollPane(input);
                          jsp.setBounds(430,5,360,300);
                          //container.add(jsp);
                          // ** input textfield
          
                          container.add(jsp);
          
                          ent = new JTextField();
                          ent.setBounds(300,350,200,20);
                          ent.setEditable(true);
                          ent.addActionListener(this);
                          container.add(ent);
                          // ** enter textfield
          
          
                          enter = new JButton("Enter");
                          enter.addActionListener(this);
                          enter.setBounds(300,400,100,20);
                          container.add(enter);
                          // ** enter button
          
                          exit = new JButton("Exit");
                          exit.addActionListener(this);
                          exit.setBounds(400,400,100,20);
                          getContentPane().add(exit);
                          container.add(exit);
                          setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                          this.dispose();
                          // ** exit button
          
                          print();
          
                          setVisible(true);
          
                          //int floor = { {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}}; // ** (not needed)
                  }
          
                  public void actionPerformed(ActionEvent evt)
                  {
          
                          if(evt.getActionCommand().equals("Exit"))
                                  {
                                          int choice = JOptionPane.showConfirmDialog(null,
                                                  " You Pressed "+ evt.getActionCommand()+" is this what you want to do? ",
                                                  " Quit Frame ",
                                                  JOptionPane.YES_NO_OPTION);
                                          if( choice == 0)
                                          this.dispose();
                                  }// ** end exit panel
          
          
          
                          if(evt.getSource()==enter || evt.getSource()==ent)
                          {
                                   floor[xpos][ypos]=0;    // clear old T ** added
          
                                  if(ent.getText().toLowerCase().compareTo("left")== 0)
                                  {
                                          left ="Key:4 (LEFT)\n";
                                  System.out.println("Left");
                                          input.append(left);
                                          ent.setText("");
                                  }// ** end left
          
                                  if(ent.getText().toLowerCase().compareTo("right")==0)
                                  {
                                          right ="Key:6 (RIGHT)\n";
                                  System.out.println("right");
                                          input.append(right);
                                          ent.setText("");
                                          if(ypos < 10 )ypos++;   // ** addded
                                          floor[xpos][ypos]=1;
                                          print();
                                  }// ** end right
          
                                  if(ent.getText().toLowerCase().compareTo("up")==0)
                                  {
                                          up ="Key:8 (UP)\n";
                                  System.out.println("Up");
                                          input.append(up);
                                          ent.setText("");
                                  }// ** end up
          
                                  if(ent.getText().toLowerCase().compareTo("down")==0)
                                  {
                                          down ="Key:2 (DOWN)\n";
                                  System.out.println("Down");
                                          input.append(down);
                                          ent.setText("");
                                  }// ** end down
                             floor[xpos][ypos]=1;    // move T ** added
                             print();
                          }// ** end action listener
          
                  }// ** end action performed
          
                  void print()
                  {
                          output.setText("");   // ** added
                          for(int xpos=0;xpos<floor.length;xpos++)
                          {
                                  for(int ypos=0;ypos<=floor[0].length;ypos++)
                                  {
                                          if(ypos==floor[0].length)
                                          {
                                                  output.append("\n");
                                                  continue;
                                          }// ** end if
                                          if(floor[xpos][ypos]==0)
                                                  output.append("0");
                                          else
                                          if(floor[xpos][ypos]==1)
                                                  output.append("T");
          
                                  }// ** end inner for
                          }// ** end outer for
                          //floor[xpos][ypos]=1;
                         // print();
                    //paint();
                  }// ** end print()
          
          
                          public static void main(String args[])
                          {
                                  new P7_21();
                          }// ** end main()
          }

          Comment

          • horace1
            Recognized Expert Top Contributor
            • Nov 2006
            • 1510

            #6
            I have added some code to do LEFT, RIGHT, UP, DOWN
            Code:
            /* /* Julian Baranowski
            Date:12/7/2006
            Revised:
            Description: Turtle Graphics Program
            */
            
            
            import javax.swing.*;        // ** import statements
            import java.awt.event.*;        // ''
            import java.awt.*;              // ''
            import java.io.*;               // ''
            
            
            public class P7_211 extends JFrame implements ActionListener
            {
                    int floor[][];
                    JTextArea output, input;
                    JButton enter,exit;
                    JScrollPane jsp;
                    JTextField ent;
                    Container container;
                    int move[] ={8,6,4,2};
                    //int moved = 0;
                    int xpos=0, ypos=0;    // ** added
                    /*final*/ int LEFT = 4, RIGHT = 6, UP = 8, DOWN = 2;
                    String left,right,up,down;
            
                    public P7_211()
                    {
                            left="";
                            right="";
                            up="";
                            down="";
                            container= getContentPane();
                            container.setLayout(null);
            
            
                            floor = new int [20][20];
                            setSize(800,500);
            
                            output = new JTextArea(20,50);
                            output.setFont(new Font("Monospaced",Font.PLAIN,18));
                            //output.setText("TEST");
                            output.setBounds(5,5,350,300);
                            output.setEditable(false);
                            container.add(output);
                            // ** output textfield
            
                            input = new JTextArea(5,50);
                            input.setFont(new Font("Monospaced",Font.PLAIN,12));
                            jsp = new JScrollPane(input);
                            jsp.setBounds(430,5,360,300);
                            //container.add(jsp);
                            // ** input textfield
            
                            container.add(jsp);
            
                            ent = new JTextField();
                            ent.setBounds(300,350,200,20);
                            ent.setEditable(true);
                            ent.addActionListener(this);
                            container.add(ent);
                            // ** enter textfield
            
            
                            enter = new JButton("Enter");
                            enter.addActionListener(this);
                            enter.setBounds(300,400,100,20);
                            container.add(enter);
                            // ** enter button
            
                            exit = new JButton("Exit");
                            exit.addActionListener(this);
                            exit.setBounds(400,400,100,20);
                            getContentPane().add(exit);
                            container.add(exit);
                            setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
                            this.dispose();
                            // ** exit button
            
                            print();
            
                            setVisible(true);
            
                            //int floor = { {0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0}}; // ** (not needed)
                    }
            
                    public void actionPerformed(ActionEvent evt)
                    {
            
                            if(evt.getActionCommand().equals("Exit"))
                                    {
                                            int choice = JOptionPane.showConfirmDialog(null,
                                                    " You Pressed "+ evt.getActionCommand()+" is this what you want to do? ",
                                                    " Quit Frame ",
                                                    JOptionPane.YES_NO_OPTION);
                                            if( choice == 0)
                                            this.dispose();
                                    }// ** end exit panel
            
            
            
                            if(evt.getSource()==enter || evt.getSource()==ent)
                            {
                                     floor[xpos][ypos]=0;    // clear old T ** added
            
                                    if(ent.getText().toLowerCase().compareTo("left")== 0)
                                    {
                                            left ="Key:4 (LEFT)\n";
                                    System.out.println("Left");
                                            input.append(left);
                                            ent.setText("");
                                            if(ypos > 0 )ypos--;   // ** addded
                                            floor[xpos][ypos]=1;
                                            print();
                                    }// ** end left
            
                                    if(ent.getText().toLowerCase().compareTo("right")==0)
                                    {
                                            right ="Key:6 (RIGHT)\n";
                                    System.out.println("right");
                                            input.append(right);
                                            ent.setText("");
                                            if(ypos < 10 )ypos++;   // ** addded
                                            floor[xpos][ypos]=1;
                                            print();
                                    }// ** end right
            
                                    if(ent.getText().toLowerCase().compareTo("up")==0)
                                    {
                                            up ="Key:8 (UP)\n";
                                    System.out.println("Up");
                                            input.append(up);
                                            ent.setText("");
                                            if(xpos > 0 )xpos--;   // ** addded
                                            floor[xpos][ypos]=1;
                                            print();
                                    }// ** end up
            
                                    if(ent.getText().toLowerCase().compareTo("down")==0)
                                    {
                                            down ="Key:2 (DOWN)\n";
                                    System.out.println("Down");
                                            input.append(down);
                                            ent.setText("");
                                            if(xpos < 10 )xpos++;   // ** addded
                                            floor[xpos][ypos]=1;
                                            print();
                                    }// ** end down
                               floor[xpos][ypos]=1;    // move T ** added
                               print();
                            }// ** end action listener
            
                    }// ** end action performed
            
                    void print()
                    {
                            output.setText("");   // ** added
                            for(int xpos=0;xpos<floor.length;xpos++)
                            {
                                    for(int ypos=0;ypos<=floor[0].length;ypos++)
                                    {
                                            if(ypos==floor[0].length)
                                            {
                                                    output.append("\n");
                                                    continue;
                                            }// ** end if
                                            if(floor[xpos][ypos]==0)
                                                    output.append("0");
                                            else
                                            if(floor[xpos][ypos]==1)
                                                    output.append("T");
            
                                    }// ** end inner for
                            }// ** end outer for
                            //floor[xpos][ypos]=1;
                           // print();
                      //paint();
                    }// ** end print()
            
            
                            public static void main(String args[])
                            {
                                    new P7_211();
                            }// ** end main()
            }

            Comment

            • r035198x
              MVP
              • Sep 2006
              • 13225

              #7
              Horace has it then.

              Comment

              • JulianP
                New Member
                • Nov 2006
                • 11

                #8
                Thank you so much now I just need to integrate a pen up and a pen down. (pen down draws and pen up doesnt) Thanks for your help so much! If I need help with the pen I will ask! Once agian thanks!!

                -Julian

                Comment

                • JulianP
                  New Member
                  • Nov 2006
                  • 11

                  #9
                  Horace1 thanks for you help so far... i have another question here is the code i have for my pen up and pen down.

                  Code:
                                           if(ent.getText().toLowerCase().compareTo("pen down")==0)
                  					   {
                  					           pd ="(PEN DOWN)\n";
                  					   System.out.println("Pen Down");
                  					           input.append(pd);
                  					           ent.setText("");
                  					           //if(xpos < 10 )xpos++;   // ** addded
                  					           floor[xpos][ypos]=1;
                  					           print();
                                          }// ** end pd
                  
                  
                                           if(ent.getText().toLowerCase().compareTo("pen up")==0)
                  					   {
                  					           pd ="(PEN UP)\n";
                  					   System.out.println("Pen Up");
                  					           input.append(pd);
                  					           ent.setText("");
                  					           //if(xpos < 10 )xpos++;   // ** addded
                  					           floor[xpos][ypos]=0;
                  					           print();
                                          }// ** end pu
                  not exactly sure what i need to change for the pen up for it so that i can move without it printing...stil l looking at it for now if you dont post ill repost if i figure something out. input appreaciated! thanks!

                  Comment

                  • JulianP
                    New Member
                    • Nov 2006
                    • 11

                    #10
                    Nevermind. I figured it out with some help from a friend. Thanks for the first input!

                    Comment

                    • r035198x
                      MVP
                      • Sep 2006
                      • 13225

                      #11
                      Originally posted by JulianP
                      Nevermind. I figured it out with some help from a friend. Thanks for the first input!
                      Good work then. Maybe if you could post the solution you got so that some of us slow learners can understand what you were trying to do.

                      Comment

                      Working...