Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Trying to write a "Turtle Graphics" Applet using Arrays

 
Beth Taitano
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Using a 20x20 array, I am supposed to set another array with the commands so that an applet displays a 12x12 square of 1s (then another shape when I code it to). Can anyone help me, this is supposed to be beginner stuff, but I'm having a really hard time.
Start of code:
import java.awt.*;
public class Turtle extends JApplet{
public void init()
{
int array[][] = new int[20][20];
int commands[] = {2,5,12,3,5,12,3,5,12,3,5,12,1,6,9};
End of code
That's all the code I have, I'm having a hard time with the idea of directions...the commands mean the following:
1 - pen up (makes a mark)
2 - pen up (stops marking)
3 - turn right
4 - turn left
5, 10 - move forward 10 spaces (10 can be replaced with any int number in this program)
6 - print the 20x20 array
9 - end of data
Can anyone help?
This is supposed to be similar to a Turtle Program downloadable over the net. I'm having trouble with the code.
Thanks.
Sorry about the name thing.
[ May 19, 2002: Message edited by: Beth Taitano ]
 
Rob Ross
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Beth. Welcome to JavaRanch.
What code have you written so far? If you post it people might be able to give you some suggestions.
Also, please change your name to comply with the naming policy to which you agreed when you registered here..

For your publicly displayed name,
use a first name, a space, and a last name.

You can change your name here:
here

You can also find the naming policy here:
http://www.javaranch.com/name.jsp
Thank You!
 
Rob Ross
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It looks like what you want to do is override the applet's paint() method, and do your painting according to the rules in the array you create when you initialize the applet.
Have a look at this tutorial on working with graphics:
http://java.sun.com/docs/books/tutorial/uiswing/painting/index.html
Check out the Overiew of Custom Painting for information on the drawing primitives available.
Also, your command array needs to be a class member (not a local variable) so that other methods (such as paint() ) will be able to use it.
 
Beth Taitano
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Um, are you telling me that I'm trying to create my graphics instead of import them from another class in Java? I'm not sure if that's the point. Here's what the assignment says:
Use a 20x20 array floor that is initialized to zero. Read commands from an arrray that contains them. Keep track of the fcurrent position of the turtle at all times and whether the pen is currently up or down. Assume that the turtle always starts at position 0,0 of the floor with its pen up. The set of turtle commands was mentioned in my first post.
As the turtle moves with the pen down, set the appropriate elements of the floor to 1s. When the 6 command (print) is given, whereever there is a zero, display a blank. Whereever there is a one in the array, display an asterisk or some other character you choose. Wreite a java applet to implement the turtle graphics capabilities discussed. Display the turtle graphics in a JTextArea.
Does this make it clearer?
Thank you for helping me...I'm a math major and Programming 1 is a required course, but I have no clue about it!
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Your assignment seems pretty clear and basic (excepting what is probably a typo in your original post concerning the first command to the turtle).
Rob was trying to point you to information regarding how to display things in an applet through its paint method and the corresponding automatic Graphics object.
Where are you in your understanding of programming in Java? Do you know how to display anything in an applet? Have you ever created an applet before? Are you already familiar with displaying graphics (with the paint method) in an Applet?
As you are a mathematics major, is it safe to assume you aren't having trouble conceptualizing what a multi-dimensional array is? Have you worked with arrays in Java before?
 
Rob Ross
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Righto Dirk.
Also, I'm having trouble understanding how using a JTextArea is going to be easier than just drawing some graphics via the paint() method. If you use paint(), you can use the Graphics.drawString() method to draw your astericks or numbers or whatever other symbols you need, and the only computations you need to do are simple x,y calculations based on the rules stored in your command array.
If you use a JTextArea, first you're going want to use a JApplet ( no biggie there), but then you need to know about the content pane of your JApplet, layout managers, adding swing componets to a layout, how to size it, and then learn the API commands to draw text into the JTextArea.
If this really is a beginner's non-programming course I'm a little amazed at the level of difficulty of this assignment.
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
On that note, unless the instructor has stated that use of a JApplet and a JTextArea are required, then forget about them.
I have noticed that a few idiots... err instructors are enamored with Swing and feel the need to force Swing down new programmers' throats. Is Swing far superior to the standard awt package? In many regards, yes. Is it simpler, or even nearly as simple as the standard awt package? In many regards, no.
Good luck with the project, Beth. I'd recommend not using Swing, unless you must.
 
Beth Taitano
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for your input guys. I found a way to do this using an almost endless sequence of if/else statements. Now I'm having trouble displaying the array in JOptionPane. My instructor does, in fact stress the use of JOptionPane. But he tells us that there are other ways to input data and show data.
Other than that, I think I've figured it out. I didn't use swing like you said except to display...which is where I'm presently having problems.
Thank you!
Beth
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Beth,
It's nice to see that you're making progress.
You may be able to clean up your if-else statements by using a switch construct. From your initial post, it looks like you'd have seven initial conditions with the fifth one also specifying the number of units. Hopefully your for-looping abilities are well-polished enough for looping through a two-dimensional array.
It wasn't clear if you had a question concerning JOptionPane and displaying your results. If you do, I'd recommend first browsing Sun's documentation for JOptionPane, and after you've become cross-eyed, maybe someone here at the 'Ranch can help you to see straight again.
Good Luck.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic