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! -
/* 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
10 5491
made a few modifications to your program - as you type right the T moves across the pattern - is this what you require? -
/* 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();
-
}
-
}
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.
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?
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,down it will replace the 0 with a T. Here's a Copy of the program agian. - /* /* 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()
-
}
I have added some code to do LEFT, RIGHT, UP, DOWN -
/* /* 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()
-
}
-
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
Horace1 thanks for you help so far... i have another question here is the code i have for my pen up and pen down. -
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...still looking at it for now if you dont post ill repost if i figure something out. input appreaciated! thanks!
Nevermind. I figured it out with some help from a friend. Thanks for the first input!
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.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: dbrown2 |
last post by:
I typically use IDLE for editing and debug. On Windows at least, IDLE
and the standard turtle graphics module do not mix. I think both use
Tkinter.
For now I use IPython and Jedit when using...
|
by: Brent W. Hughes |
last post by:
If the Turtle gets himself into a large (or infinite) loop, is there any way
to stop him other than Ctrl-Alt-Del?
Brent
|
by: Brent W. Hughes |
last post by:
I'm using the turtle graphics module and am trying to teach it to my kids.
The slowness of the turtle in drawing things is good at first because it's
fun watching it do its thing. But now we'd...
|
by: jevitop |
last post by:
Hi, im looking for help on how to make a turtle graphics program. My
head will explode trying to figure this out.
if anyone can help me please do it, i'll appriciate it!!!
Can anyone...
|
by: Dick Moores |
last post by:
I accidentally stumbled across the Turtle Graphics module (turtle.py)
the other day and have been having some fun with it.
Now I'm wondering if there is a way to build into a script the saving...
|
by: tomy |
last post by:
Hi All
I am a newbie to turtle graphics in python, so sorry if you find this
question too easy.
How can I get smoother lines in turtle graphics?
I am using python on windows.
Thanks in advance
|
by: hello123 |
last post by:
I saw this program from a website... Any solution ?
Turtle Graphics: The Logo language, which is particularly popular among personal computer user, made the concept of turtle graphics famous. ...
|
by: Alexander.Oot |
last post by:
I think the speed function may be broken from the turtle graphics
package
"from turtle import *
speed('fastest')
forward(50)"
|
by: Allen |
last post by:
I'm using the turtle module in Python. Is there a way to save the turle
state at any moment for recursive algorithms to easily return the turtle
to an earlier point for another branch/etc?
...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: emmanuelkatto |
last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud.
Please let me know.
Thanks!
Emmanuel
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: Hystou |
last post by:
There are some requirements for setting up RAID:
1. The motherboard and BIOS support RAID configuration.
2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
| |