66-460
Introduction to Graphical Human-Machine Interfaces
Programming Project 2: FAQ


Frequently Asked Questions

Some of you have been asking me questions (and good ones, too!) about the Java project. Below, I am providing the questions, and their answers, so that all of you can benefit.


The Questions

  1. I am a little bit confused about the first objective of the project: the objective to create the UI for the l-system applet. The applet that you gave use seems to take care of the most of the requirements for the user interface; are we to just refine the existing applet to accomplish this goal?

  2. Also, after the user chooses generate from the l-system screen is it supposed to be set up so the string is generated and is sent to TurtleCanvas class so that the object can be displayed on the screen?

  3. When the user chooses generate, should the web page change to a new page with the tree-like object on it? Is this going to be similar to the turtledraw applet that you gave us? Are we just supposed to add the graphics part to this applet? This way the user can change the angle and other factors so that the tree can be redrawn right? When the user chooses generate, instead of displaying the string as it does now, it should be passed to the renderLsystem procedure right? What is the best way to do this: create an instance of the TurtleCanvas and call TurtleCanvas.renderLsystem?

  4. At the end of the spec. sheet you gave an example of an l-system with an Axiom of F, and a rule of F->F[+F]F[-F][F]. Isn't illegal to have F as part of the predicate (to the left of the '->' sign) since it is part of the turtle alphabet? If so do we have to account for this problem?

  5. What exactly are the Draw and Move buttons on the Contol panel for. Is Move used to move the turtle one unit in the current direction? If so what is the distinction between this and the Draw button? Does the draw button redraw the l-system acknowledging some of the changes that the user has made to the length, width, angle, x/yScales???

  6. One real quick question about the project is that in all of the code there is a lot of variables that start with an underscore ("_"). Is there any specific significance of the variables that start with the underscore? Are they just the variables that are public member variables in the class in question?

  7. Is it important to show the string generated by the lsystem? Or is it just sufficient to display the picture that you get from manipulating this string?

  8. Is the show turtle button supposed to do anything other than display the current attributes associated with the current turtle: i.e. the x,y coordinates, the angle, X-Scale, etc....?

  9. If the user specifies an order of 0, that generates just F. Do we draw anything for that, ie go forward one, or is that not really a generation?

  10. When we generate the string, do we do as follows:
    1. Call lsystem.generate() order times.
    2. Get the result using lsystem.getPath().
    3. Then iterate through the string and draw result.

  11. OR, do we have to draw the result of EACH path between 0 and order, ie, if order is 3 and f->FF, we draw one for FF, one for FFFF, and one for FFFFFF, as opposed to just the last, FFFFFF?

  12. The code seems to create turtles in several different places and then never used, eg in the TurtleCanvas constructor you create a turtle but then never do anything with it. In addition, one is created in renderLsystem. Is this necessary?

  13. Lastly, since the turtle is created INSIDE renderLsystem, but we get the drawing length from the user in our applet class, ie OUTSIDE the TurtleCanvas class, I'm not sure how to set the length of the turtle before drawing. I would like to just add another "set" function to TurtleCanvas that sets a new variable called length and then it can use that inside renderLsystem. Or is there some way you had envisioned that I'm not thinking of.

  14. For the following alphabet symbols: /(x) \(x) <(x) >(x) w(x) Will the ('s, )'s, etc be INCLUDED in the string or are just the x? In the former case, can we assume that input is valid, ie a (x) will ALWAYS follow a /, \, <, >, or w? Or should we take this into account and simply skip to the next valid symbol or something like that?

  15. I have another question about the project. Is there an easy way to get the program to redraw the L-system. The reason that I ask is that when you move the scroll bar so that the picture is not visible on the screen and you move back the picture on the canvas is no longer there. Can you override the paint function somehow to fix this problem. I noticed that your sample program, where you can push the buttons to paint on the canvas does, not handle this problem. Is it as simple as creating a paint procedure that basically redraws the l-system. In other words calls render-lsystem? Will the paint function automatically be called when the scrollbar is moved?

  16. On the project spec you have the following: <(x) darken turtle pen color... >(x) darken turtle pen color... Is one of these actually supposed to be lighten?? If so, which one? Or are they supposed to do the same thing?

  17. You mentioned early today that there is a symbol that allows for the changing of the lengthy in the alphabet. However, I do not see one in the project spec. Should one be added?

  18. I just wanted to point out that I have found a bug in the l-system generator code, or at least I believe I have. Try the string F[cFF0000+F] and generate an l-system of second order. It treats the FFs in the cFF0000 as occurrences of the axiom and not as the hexadecimal F that it actually represents. Is this indeed a bug or is this not a valid string or I am thinking wrong.

  19. In the project specs you mention 2 different turtle operations: +/- turn left/right \ or / (x) roll left/right

    What is the difference between turn and roll? Are they both controlled by the _angle member of the Turtle class? Which controls in TurtleDraw control panel correspond to these actions?

  20. What's the function of the turtle stack and how exactly do we go about building it (or using it)?

  21. The cRRGGBB token is supposed to change the turtle's pen color to the RGB value specified by RRGGBB.

    How do we convert RRGGBB string to the instance of Color class?

  22. Is there an easy way to convert a string to a floating point number? For the purpose of accepting a real number as either the rotate angle or the start angle. Is there something comparable to the parseInt function?

  23. I've been working some more on the project and I have the set turtle pen color working (cXXXXXX) by using the colorFromString method in the DrawingCanvas class. Now I'm trying to work on the darken and lighten pen color commands <(X) and >(X). My question is, are we supposed to use the adjustColor method in the DrawingCanvas class? If so, how do we specify that component (the first parameter to the method)? Are we supposed to just lighten or darken one of the colors, (red green or blue) or the entire string?

  24. How do you specify the radix in the call to: public Integer valueOf (String s, int radix); Specifically how do you specify the radix so that conversion from hex to decimal can be made?

  25. How picky do we have to be about errors in the actual rules. For instance, if one of the rules contains the following: /(40) but the user forgets the second parentheses: /(40 Should our system be good enough to handle this? Or if the user is trying to change the color and they don't give exactly 6 digits: F->c12345 If we do need to handle these errors, where would be the best place to handle this? Should we check before the rule is added or when the user goes to generate the L-system?

  26. i have a problem using the stacks. no matter what i do, the turtles doesn't' get initialize to its former selves

    here's part of my code:

    case '[' : { (Turtle) turtleStack.push((Turtle) turtle); break; } case ']' : { turtle = new Turtle((Turtle)turtleStack.pop()); break; } i tried all sorts of combinations of putting the object, (Turtle) in different places of the pop and push calls, but I still can't get it to work right.

The Questions with Answers

  1. I am a little bit confused about the first objective of the project: the objective to create the UI for the l-system applet. The applet that you gave use seems to take care of the most of the requirements for the user interface; are we to just refine the existing applet to accomplish this goal? Well, in my applets I do have most of the interface. But, I don't have the entire interface. For example, the TurtleDraw applet has interface components for the turtle angle, width, and length. And also components for the start angle and x- and y- scales. The LsystemGen applet on the other hand has components for the specifying an L-system, it's axioms, productions, and order (number of iterations). What you need is an interface that contains all of these components in one applet. There are two ways to go about this take one of the applets I gave you and extend it, or just start from scratch, create your own applet, and take code from the other two applets as needed to create the UI you want.
  2. Also, after the user chooses generate from the l-system screen is it supposed to be set up so the string is generated and is sent to TurtleCanvas class so that the object can be displayed on the screen?

    yes; that is the idea. There are a number of ways to do this too. The LsystemGen applet contains the code to generate the string, but not draw it. What you need is an applet like the LsystemGen applet but with your modified UI *plus* the TurtleCanvas code from the TurtleDraw applet. Your UI of course must have a canvas if the user is to see the drawing. Then once you generate the string, you pass the string to the TurtleCanvas routine renderLsystem() which you also myst add code to to make the turtle respond to the meanings of the terminals in the turtle alphabet.
  3. When the user chooses generate, should the web page change to a new page with the tree-like object on it? Is this going to be similar to the turtledraw applet that you gave us? Are we just supposed to add the graphics part to this applet? This way the user can change the angle and other factors so that the tree can be redrawn right? When the user chooses generate, instead of displaying the string as it does now, it should be passed to the renderLsystem procedure right? What is the best way to do this: create an instance of the TurtleCanvas and call TurtleCanvas.renderLsystem?

    One way to do this is to replace the big TextArea in the LsystemGen class with a TurtleCanvas. So instead of displaying the string, you just generate the tree for it. In this case, ou would generate your string and call renderLsystem() passing the string to it. You should not need to go to a new page to display it. It should all go on the same page. But I don't want to constrain your UI to any one way. You are certainly free to try to design the UI as you see fit. Using the applets as a guide for how to implement other UIs. Ideally, the user should be able to first specify the L-system (axiom and productions), then specify turtle parameters (angle increment, width, and length), and then specify things like the x- and y- scalings. This way the user could change different parameters for the turtles or the scaling using the same L-system and redraw it or generate it as needed. What I have found is that some L-systems are to small and need more scaling, while others are not so small and do fine with no scaling. So it might be that the user wants to generate and display the tree, but it looks funny, so change the scaling and re-display it. It might not be necessary to re-generate the string each time just re-display it. Note also that TurtleDraw always starts drawing from the left side from the center. If you want to change it so that it starts drawing from the origin or the bottom of the page that is certainly possible, you will have to look at where xoff and yoff are calculated.
  4. At the end of the spec. sheet you gave an example of an l-system with an Axiom of F, and a rule of F->F[+F]F[-F][F]. Isn't illegal to have F as part of the predicate (to the left of the '->' sign) since it is part of the turtle alphabet? If so do we have to account for this problem?

    I can see why this confusing. With context free grammars the start symbol is a non-terminal S -> ab | aSb A -> a B -> b will generate all strings with equal number of a's and b's with a's followed by b's: ab, aabb, aaabbb, etc However, with L-systems, it is ok to have the start axiom be a terminal because we can stop the generation at any step. It is ok to have F as a predicate. It is not illegal in the sense that you are able to apply productions for each F in the string and generate as many higher order strings as desired. The L-system above happens to use F on the left side and the right side. But it does not have to be this way, consider this one: Axiom: X Rules: F -> FF X -> F[+X]F[-X]+X Angle: 20 degrees Whenever we stop generating strings, we still have X's in the string, however, when you render it, the turtle just ignores the X and moves to the next symbol, so in this case only the [, ], +, -, and F make the turtle do anything.
  5. What exactly are the Draw and Move buttons on the Contol panel for. Is Move used to move the turtle one unit in the current direction? If so what is the distinction between this and the Draw button? Does the draw button redraw the l-system acknowledging some of the changes that the user has made to the length, width, angle, x/yScales??? The Move and Draw and other buttons on that line simply make the turtle do the operation using the current values of the turtle parameters. Move moves the turtles location by one turtle length. It does not draw; draw on the other hand moves the turtle and draws by one length. So, for example, when you encounter the 'f' symbol you need to make the turtle jump (or move) without drawing. Upon encountering an 'F' in the string you will draw. The turtle parameters are not updated until you press "Update Turtle Parms" for length, width, angle. The other parms x- y- scale and start angle are not updated unless you press "Update Values" once updated the buttons will use the new values.
  6. One real quick question about the project is that in all of the code there is a lot of variables that start with an underscore ("_"). Is there any specific significance of the variables that start with the underscore? Are they just the variables that are public member variables in the class in question? The ones with _ in them are just variables that are part of the class. Variables without _ are supposed to just be local variables. This was the convention I found in some other code that I got off the net. Sorry for the confusion. You don't have to do it that way :) Also it looks like I am inconsistent; sometimes I prefix class variables with the underscore and sometimes I don't. Sorry about that!
  7. Is it important to show the string generated by the lsystem? Or is it just sufficient to display the picture that you get from manipulating this string? You don't have to show the string. The picture is what we need to see.
  8. Is the show turtle button supposed to do anything other than display the current attributes associated with the current turtle: i.e. the x,y coordinates, the angle, X-Scale, etc....? No, it was just a debugging aid I put in when i was writing the applet, I wasn't sure the turtle values were getting set right. So it just prints out the current values........
  9. If the user specifies an order of 0, that generates just F. Do we draw anything for that, ie go forward one, or is that not really a generation? Well, 0 is not really a generation, but it is a starting point, e.g., F which would just be a line (f means move F means draw) so the turtle would draw one unit. You should probably set it up so that whatever the user puts in be it 0, 1, 5 or 10, you just generate the string and draw it. If he puts in 0 you can still draw it, although, it is not very useful to just generate 0 :) You can also choose to require the user to input an order greater than zero. In fact, I think in one of the applets I have code that validates the values entered. Feel free to constrain the user's input as you see fit.
  10. When we generate the string, do we do as follows:
    1. Call lsystem.generate() order times.
    2. Get the result using lsystem.getPath().
    3. Then iterate through the string and draw result.
    This is what I had in mind. You simply generate the string by calling generate() order times. Then pass that string to the renderLsystem() routine so that it is drawn. The user may then want to change the x- and y- scales, turtle parameters, or what not, but redraw the same string. It might not be necessary to re-generate the string, but you will probably have to call the render routine in order to re-draw with the chosen parameters. Only if the user adds new productions or rules should you have to re-generate the string
  11. OR, do we have to draw the result of EACH path between 0 and order, ie, if order is 3 and f->FF, we draw one for FF, one for FFFF, and one for FFFFFF, as opposed to just the last, FFFFFF? It is not a requirement that you draw each iteration, but you are certainly welcome to do so! It would make a very interesting applet. It is certainly possible to draw each result from 0, 1, ... order-n, order which gives it sort of an animated look at how the L-system is evolving. It is possible to put each iteration in a Java 'thread' and make it do that. If you are curious you may wish to look at the threads primer in the Java tutorial that shows how to do this sort of thing. An easier way might be to add a button called next, or something, each time you present next it generates the next iteration and draws it.
  12. The code seems to create turtles in several different places and then never used, eg in the TurtleCanvas constructor you create a turtle but then never do anything with it. In addition, one is created in renderLsystem. Is this necessary? Yes you are right. In the TurtleDraw applet, the turtle is not a part of the TurtleCanvas. Instead, the turtle is created as part of the ControlPanel so that when the user modifies turtle parameters it is to change the turtle. When it comes time to draw, see drawTurtle() another turtle is created using the current parms, and the graphics context is obtained from the controller class. It does not have to be this way. You could encapsulate the turtle inside the TurtleCanvas and then provide an interface for changing the turtle parameters via the canvas. If done correctly there would be no need for creating a turtle outside the canvas. Since the renderLsystem() routine draws the entire tree, it makes sense that the turtle go in there (and actually you have to maintain the turtle stack there also) You are free to put the turtle where you think it goes. Since the renderLsystem() call renders teh entire string, it might be easiest to have that routine create the turtle locally and draw it. But obviously you need to provide a way for the user to specify the turtle parms (length, width, angle) and have it affect the turtle inside the render routine. This could be done if you create methods for setting the turtle parms and somehow have it effect the turtle you create inside the renderLsystem.
  13. Lastly, since the turtle is created INSIDE renderLsystem, but we get the drawing length from the user in our applet class, ie OUTSIDE the TurtleCanvas class, I'm not sure how to set the length of the turtle before drawing. I would like to just add another "set" function to TurtleCanvas that sets a new variable called length and then it can use that inside renderLsystem. Or is there some way you had envisioned that I'm not thinking of. Yes, you can just add set routines to the TurtleCanvas class and then call those to change the values prior to rendering the L-system. When you initialize the turtle for the first time ( the call to new Turtle() in renderLsystem()) you need to make sure you extract those values
  14. For the following alphabet symbols: /(x) \(x) <(x) >(x) w(x) Will the ('s, )'s, etc be INCLUDED in the string or are just the x? In the former case, can we assume that input is valid, ie a (x) will ALWAYS follow a /, \, <, >, or w? Or should we take this into account and simply skip to the next valid symbol or something like that? The parentheses are part of the string. Consider this L-system: Axiom: A Rules: F -> FF A -> w(3)Fw(2)[+A][-A]w(1)FA Order: 6 Start angle: 270 Rotate angle: 20 we get: 0: A 1: w(3)Fw(2)[+A][-A]w(1)FA 2: w(3)FFw(2)[+w(3)Fw(2)[+A][-A]w(1)FA] [-w(3)Fw(2)[+A][-A]w(1)FA] w(1)FFw(3)Fw(2)[+A][-A]w(1)FA etc... This one is an interesting one, you might want to try it! You might have to play with the x- and y- scales to see what it looks like better. You can ASSUME that those are always followed by (x) but remember the data is only as good as the user enters it. Since the user has to specify the rules, they could make a mistake and leave off the closing paren by mistake. So that might make your program do something weird.
  15. I have another question about the project. Is there an easy way to get the program to redraw the L-system. The reason that I ask is that when you move the scroll bar so that the picture is not visible on the screen and you move back the picture on the canvas is no longer there. Can you override the paint function somehow to fix this problem. I noticed that your sample program, where you can push the buttons to paint on the canvas does, not handle this problem. Is it as simple as creating a paint procedure that basically redraws the l-system. In other words calls render-lsystem? Will the paint function automatically be called when the scrollbar is moved? Yes, what you need to do is override the paint() routine of the TurtleCanvas: void public paint (Graphics g) { // here we can re-generate the L-system // to do this you need the "path" string somehow // and then call renderLsystem() // the implementation of this is left as an exercise ;) } here is a little explanation on what is happening. This is taken from the Java tutorial section on Drawing: http://java.sun.com/nav/read/Tutorial/ui/overview/drawing.html Programs can draw only when the AWT tells them to. The reason is that each occurrence of a Component drawing itself must execute without interruption. Otherwise, unpredictable results could occur, such as a button being drawn halfway, and then being interrupted by some lengthy animation. The AWT orders drawing requests by making them run in a single thread. A Component can use the repaint() method to request to be scheduled for drawing. The AWT requests that a Component draw itself by invoking the Component's update() method. The default (Component) implementation of the update() method simply clears the Component's background (drawing a rectangle over the component's clipping area in the Component's background color) and then calling the Component's paint() method. The default implementation of the paint() method does nothing. the AWT causes a component to repaint itself by callnig the update() method. In a 'smart' drawing program, you would only re-draw the parts of the window that really needed to be re-drawn. So in these cases, we implement both update() and paint() and make paint() call update. We then make update() only re-draw the parts of the screen that are necessary to be re-drawn. But this is kind of hard to do with the L-system, so for this project we can just override paint as above and not worry about update. For more details see: Overview of Graphics http://java.sun.com/nav/read/Tutorial/ui/drawing/overview.html Eliminating Flashing http://java.sun.com/nav/read/Tutorial/ui/drawing/noFlashing.html
  16. On the project spec you have the following: <(x) darken turtle pen color... >(x) darken turtle pen color... Is one of these actually supposed to be lighten?? If so, which one? Or are they supposed to do the same thing? One of them should be lighten and the other should be darken. My mistake! it does no really matter which one does which as one does darken and the other does lighten, how about: >(x) for darken <(x) for lighten
  17. You mentioned early today that there is a symbol that allows for the changing of the lengthy in the alphabet. However, I do not see one in the project spec. Should one be added? No it is not necessary to add one. I have seen several turtle alphabets and just thought our alphabet had one.
  18. I just wanted to point out that I have found a bug in the l-system generator code, or at least I believe I have. Try the string F[cFF0000+F] and generate an l-system of second order. It treats the FFs in the cFF0000 as occurrences of the axiom and not as the hexadecimal F that it actually represents. Is this indeed a bug or is this not a valid string or I am thinking wrong. yeah, the lsystem generator is not as robust as we would like. When it encounters the F, it doesn't remember that it just saw a 'c' when applying rules. So, it treats the Fs as part of the hex number as if they were F's to be replaced with the successor to a rule. There is a workaround. Specify the hexadecimal colors using lowercase letters instead. E.g., instead of FF0000 use ff0000 I think it will then work correctly.
  19. In the project specs you mention 2 different turtle operations: +/- turn left/right \ or / (x) roll left/right

    What is the difference between turn and roll? Are they both controlled by the _angle member of the Turtle class? Which controls in TurtleDraw control panel correspond to these actions?

    In the TurtleDraw control panel, you can set: length: the length the turtle moves/draws width: the width of the line drawn angle: how many degrees the turtle moves during a 'turn' (this is the second row of the control panel with the UpdateTurtleParms button) Whenever you turn (+ or - ) we make the turtle rotate by 'angle' degrees. So this actually changes the orientation of the turtle. This is the Rotate Left and Rotate Right buttons. Rolling does not actually change the turtle's orientation; it updates 'angle' to a new value: \(x) roll the turtle left (increase current angle) by x (degrees) /(x) roll the turtle right (decrease current angle) by x (degrees) So when you roll you need to update the _rotateAngle variable appropriately so that the next time you turn (rotate) left or right, you use the new _rotateAngle value. (Or whatever variable you happen to use for the rotate angle)
  20. What's the function of the turtle stack and how exactly do we go about building it (or using it)? Note in the turtle alphabet, these two symbols: [ which means PUSH ] which means POP the idea is that you can push the turtle state onto a stack and let the l-system go off for a while until it sees a pop which takes it right back to the previous state that was pushed. This is useful for making branching structures (l-systems can be used to draw plants, trees, etc) Java provides a simple stack class, if you import: import java.util.Stack; you use it like this: // note this assumes you have a valid turtle which you new'ed somewhere Turtle turtle; // ... turtle = new Turtle ( ... ); // Create a stack Stack turtleStack = new Stack(); // push a turtle onto the stack turtleStack.push (turtle); // pop a turtle off the stack turtle = (Turtle) turtleStack.pop();
  21. The cRRGGBB token is supposed to change the turtle's pen color to the RGB value specified by RRGGBB.

    How do we convert RRGGBB string to the instance of Color class?

    The RR GG and BB are hex values; e.g., you might encounter cff0000 which means change the turtle's pen color to RED. so you need to extract the RRGGBB portion into a string: so that some string, say colorString = "ff0000" Note that Color has the following constructors: Color (int rgb); // color as single (decimal number) Color (int r, int g, int b); // you need decimal values for r, g, b so you need some way of either converting the colorString into it's equivalent integer value; or you need to convert the RR, GG, and BB portions of the color string into 3 different ints. How you do that is up to you. here is one more hint: you can use the Java Integer class to convert string representations of integers to Integer instances: public Integer valueOf (String s, int radix); note valueOf() may throw the exception NumerFormatException you need to catch it like I have done in the example LsystemGen applet you can then convert that Integer instance into an int: public int intValue (); Once you have the int value, you can create the Color. Fore more about Integer see: java.lang.Integer
  22. I have one more quick question about the project. Is there an easy way to convert a string to a floating point number? For the purpose of accepting a real number as either the rotate angle or the start angle. Is there something comparable to the parseInt function? Yes, the Float class (java.lang.Float) can do this. here is a simple way: // Note myText is a TextField component String s = myText.getText(); float f1 = (float) 0.0; try { f1 = Float.valueOf(s).floatValue(); } catch(NumberFormatException e) { // let user know they did not enter a float correctly // for that field getAppletContext().showStatus ("MYTEXT must be a float"); }
  23. I've been working some more on the project and I have the set turtle pen color working (cXXXXXX) by using the colorFromString method in the DrawingCanvas class. Now I'm trying to work on the darken and lighten pen color commands <(X) and >(X). My question is, are we supposed to use the adjustColor method in the DrawingCanvas class? If so, how do we specify that component (the first parameter to the method)? Are we supposed to just lighten or darken one of the colors, (red green or blue) or the entire string? Let's say you find in your string >(20) this means darken by 20%. So suppose color=rrggbb in the RGB notation. You need to darken each of rr, gg, and bb by 20%. Note that eacah rr and gg and bb go from 0 (darkest) to 255 (lightest) So when you darken by 20% you want to actually decrease each of rr, gg, and bb by (1-percentage) For lighten you multiply each of rr, gg, and bb by (1+percentage) You have to make sure that after you compute the new values for each of rr, gg, and bb that the value is still between 0 and 255, if not yo need to correct it. Oh, did I give you adjustColor() well then yes, it is easy: // darken current red value int newRR = adjustColor (currentRR, percent, "darken"); you will have to call adjustColor for each of rr, gg, and bb and then create a new Color to update the turtle pen color with
  24. How do you specify the radix in the call to: public Integer valueOf (String s, int radix); Specifically how do you specify the radix so that conversion from hex to decimal can be made? The radix is the same as the 'base' so pass 16 as the radix: String hexVal = "0000ff"; int hex = 0; try { hex = Integer.valueOf (hexVal, 16).intValue(); } catch (NumberFormatException e) { System.out.println ("Not a hex number: " + hexVal); } System.out.println ("hexVal = " + hexVal + " which is " + hex + " in decimal" );
  25. How picky do we have to be about erors in the actual rules. For instance, if one of the rules contains the following: /(40) but the user forgets the second parentheses: /(40 Should our system be good enough to handle this? Or if the user is trying to change the color and they don't give exactly 6 digits: F->c12345 If we do need to handle these errors, where would be the best place to handle this? Should we check before the rule is added or when the user goes to generate the L-system? There are two ways to do this: ( (1) validate the rules when they are entered to make sure they are not malformed before even generating the L-system; you would want to let the user know what is wrong with the rule with an error message or some kind of feedback (2) do checking while you are scanning the generated string; in this case you might stop rendering if you find an error, or just skip ahead to a symbol that you can start drawing from again What you do is up to you. For example, you might be able to guess: /(40 since a percentage has only 1 or 2 digits, you could just pretend you saw the closing parentheses. Or you could just report an error, 'missing close paren' and stop rendering or you could skip ahead until you find a symbol that matches one of the rules. e.g, : /(40f[+F] you are looking for a right paren, but there is not one. You could just ignore the /(40 and start at the [ For the case of F->c12345 you would not want to do anything to the color since you need 6 digits, so you might stop rendering and report an error or just skip it and move to the next valid character. Kind of like what a good parser does. ideally you want to catch errors early on, so checking it when rules are entered is probably the best way. Then you do not have to do any checking during the scanning of the generated string during rendering.

The Questions with Answers

  1. I am a little bit confused about the first objective of the project: the objective to create the UI for the l-system applet. The applet that you gave use seems to take care of the most of the requirements for the user interface; are we to just refine the existing applet to accomplish this goal? Well, in my applets I do have most of the interface. But, I don't have the entire interface. For example, the TurtleDraw applet has interface components for the turtle angle, width, and length. And also components for the start angle and x- and y- scales. The LsystemGen applet on the other hand has components for the specifying an L-system, it's axioms, productions, and order (number of iterations). What you need is an interface that contains all of these components in one applet. There are two ways to go about this take one of the applets I gave you and extend it, or just start from scratch, create your own applet, and take code from the other two applets as needed to create the UI you want.
  2. Also, after the user chooses generate from the l-system screen is it supposed to be set up so the string is generated and is sent to TurtleCanvas class so that the object can be displayed on the screen?

    yes; that is the idea. There are a number of ways to do this too. The LsystemGen applet contains the code to generate the string, but not draw it. What you need is an applet like the LsystemGen applet but with your modified UI *plus* the TurtleCanvas code from the TurtleDraw applet. Your UI of course must have a canvas if the user is to see the drawing. Then once you generate the string, you pass the string to the TurtleCanvas routine renderLsystem() which you also myst add code to to make the turtle respond to the meanings of the terminals in the turtle alphabet.
  3. When the user chooses generate, should the web page change to a new page with the tree-like object on it? Is this going to be similar to the turtledraw applet that you gave us? Are we just supposed to add the graphics part to this applet? This way the user can change the angle and other factors so that the tree can be redrawn right? When the user chooses generate, instead of displaying the string as it does now, it should be passed to the renderLsystem procedure right? What is the best way to do this: create an instance of the TurtleCanvas and call TurtleCanvas.renderLsystem?

    One way to do this is to replace the big TextArea in the LsystemGen class with a TurtleCanvas. So instead of displaying the string, you just generate the tree for it. In this case, ou would generate your string and call renderLsystem() passing the string to it. You should not need to go to a new page to display it. It should all go on the same page. But I don't want to constrain your UI to any one way. You are certainly free to try to design the UI as you see fit. Using the applets as a guide for how to implement other UIs. Ideally, the user should be able to first specify the L-system (axiom and productions), then specify turtle parameters (angle increment, width, and length), and then specify things like the x- and y- scalings. This way the user could change different parameters for the turtles or the scaling usnig the same L-system and redraw it or generate it as needed. What I have found is that some L-systems are to small and need more scaling, while others are not so small and do fine with no scaling. So it might be that the user wants to generate and display the tree, but it looks funny, so change the scaling and re-display it. It might not be necessary to re-generate the string each time just re-display it. Note also that TurtleDraw always starts drawing from the left side from the center. If you want to change it so that it starts drawing from the origin or the bottom of the page that is certainly possible, you will have to look at where xoff and yoff are calculated.
  4. At the end of the spec. sheet you gave an example of an l-system with an Axiom of F, and a rule of F->F[+F]F[-F][F]. Isn't illegal to have F as part of the predicate (to the left of the '->' sign) since it is part of the turtle alphabet? If so do we have to account for this problem?

    I can see why this confusing. With context free grammars teh start symbol is a non-terminal S -> ab | aSb A -> a B -> b will generate all strings with equal number of a's and b's with a's followed by b's: ab, aabb, aaabbb, etc However, with L-systems, it is ok to have the start axiom be a terminal because we can stop the generation at any step. It is ok to have F as a predicate. It is not illegal in the sense that you are able to apply productions for each F in the string and generate as many higher order strings as desired. The L-system above happens to use F on the left side and the right side. But it does not have to be this way, consider this one: Axiom: X Rules: F -> FF X -> F[+X]F[-X]+X Angle: 20 degrees Whenever we stop generating strings, we still have X's in the string, however, when you render it, the turtle just ignores the X and moves to the next symbol, so in this case only the [, ], +, -, and F make the turtle do anything.
  5. What exactly are the Draw and Move buttons on the Contol panel for. Is Move used to move the turle one unit in the current direction? If so what is the distinction between this and the Draw button? Does the draw button redraw the l-system acknowledging some of the changes that the user has made to the length, width, angle, x/yScales??? The Move and Draw and other buttons on that line simply make the turtle do the operation using the current values of the turtle parmameters. Move moves the turtles location by one turtle length. It does not draw; draw on the other hand moves the turtle and draws by one length. So, for example, when you encounter the 'f' symbol you need to make the turtle jump (or move) without drawing. Upon encountering an 'F' in the string you will draw. The turtle parameters are not updated until you press "Update Turtle Parms" for length, width, angle. The other parms x- y- scale and start angle are not updated unless you press "Update Values" once updated the buttons will use hte new values.
  6. One real quick question about the project is that in all of the code there is a lot of variables that start with an underscore ("_"). Is there any specific signigicance of the variables that start with the underscore? Are they just the variables that are public member variables in the class in question? The ones with _ in them are just variables that are part of the class. Variables without _ are supposed to just be local variables. This was the convention I found in some other code that I got off the net. Sorry for the confusuion. You don't have to do it that way :) Also it looks like I am inconsistent; sometimes I prefix class variables with the underscore and sometimes I don't. Sorry about that!
  7. Is it important to show the string generated by the lsystem? Or is it just suffcient to display the picture that you get from manipulating this string? You don't have to show the string. The picture is what we need to see.
  8. Is the show turtle button supposed to do anything other than display the current attributes associated with the current turtle: i.e. the x,y corrdinates, the angle, XScale, etc....? No, it was just a debugging aid I put in when i was writing the applet, I wasnt sure the turtle values were getting set right. So it just prints out the current values........
  9. If the user specifies an order of 0, that generates just F. Do we draw anything for that, ie go forward one, or is that not really a generation? Well, 0 is not really a generation, but it is a starting point, e.g., F which would just be a line (f means move F means draw) so the turtle would draw one unit. You should probably set it up so that whatever the user puts in be it 0, 1, 5 or 10, you just generate the string and draw it. If he puts in 0 you can still draw it, although, it is not very useful to just generate 0 :) You can also choose to require the user to input an order greater than zero. In fact, I think in one of the applets I have code that validates the values entered. Feel free to constrain the user's input as you see fit.
  10. When we generate the string, do we do as follows:
    1. Call lsystem.generate() order times.
    2. Get the result using lsystem.getPath().
    3. Then iterate through the string and draw result.
    This is what I had in mind. You simply generate the string by calling generate() order times. Then pass that string to the renderLsystem() routine so that it is drawn. The user may then want to change the x- and y- scales, turtle parameters, or what not, but redraw the same string. It miight not be necessary to re-generate the string, but you will probably have to call the render routine in order to re-draw with the chosen parameters. Only if the user adds new productions or rules should you have to re-generate the string
  11. OR, do we have to draw the result of EACH path between 0 and order, ie, if order is 3 and f->FF, we draw one for FF, one for FFFF, and one for FFFFFF, as opposed to just the last, FFFFFF? It is not a requirement that you draw each iteration, but you are certainly welcome to do so! It would make a very interesting applet. It is certainly possible to draw each result from 0, 1, ... order-n, order which gives it sort of an animated look at how the L-system is evolving. It is possible to put each iteration in a Java 'thread' and make it do that. If you are curious you may wish to look at the threads primer in the java tutorial that shows how to do this sort of thing. An easier way might be to add a button called next, or something, each time you present next it generates the next iteration and draws it.
  12. The code seems to create turtles in several different places and then never used, eg in the TurtleCanvas constructor you create a turtle but then never do anything with it. In addition, one is created in renderLsystem. Is this necessary? Yes you are right. In the TurtleDraw applet, the turtle is not a part of the TurtleCanvas. Instead, the turtle is created as part of the ControlPanel so that when the user modifies turtle parameters it is to change the turtle. When it comes time to draw, see drawTurtle() another turtle is created using the current parms, and the graphics context is obtained from the controller class. It does not have to be this way. You could encapsulate the turtle inside the TurtleCanvas and then provide an interface for changing the turtle parameters via the canvas. If done correctly there would be no need for creating a turtle outside the canvas. Since the renderLsystem() routine draws the entire tree, it makes sense that the turtle go in there (and actually you have to maintain the turtle stack there also) You are free to put the turtle where you think it goes. Since the renderLsystem() call renders teh entire string, it might be easiest to have that routine create the turtle locally and draw it. But obviously you need to provide a way for the user to specify the turtle parms (length, width, angle) and have it affect the turtle inside the render routine. This could be done if you create methods for setting the turtle parms and somehow have it effect the turtle you create inside the renderLsystem.
  13. Lastly, since the turtle is created INSIDE renderLsystem, but we get the drawing length from the user in our applet class, ie OUTSIDE the TurtleCanvas class, I'm not sure how to set the length of the turtle before drawing. I would like to just add another "set" function to TurtleCanvas that sets a new variable called length and then it can use that inside renderLsystem. Or is there some way you had envisioned that I'm not thinking of. Yes, you can just add set routines to the TurtleCanvas class and then call those to change the values prior to rendering the L-system. When you initialize the turtle for the furst time ( the call to new Turtle() in renderLsystem()) you need to make sure you extract those values
  14. For the following alphabet symbols: /(x) \(x) <(x) >(x) w(x) Will the ('s, )'s, etc be INCLUDED in the string or are just the x? In the former case, can we assume that input is valid, ie a (x) will ALWAYS follow a /, \, <, >, or w? Or should we take this into account and simply skip to the next valid symbol or something like that? The parentheses are part of the string. Consider this L-system: Axiom: A Rules: F -> FF A -> w(3)Fw(2)[+A][-A]w(1)FA Order: 6 Start angle: 270 Rotate angle: 20 we get: 0: A 1: w(3)Fw(2)[+A][-A]w(1)FA 2: w(3)FFw(2)[+w(3)Fw(2)[+A][-A]w(1)FA] [-w(3)Fw(2)[+A][-A]w(1)FA] w(1)FFw(3)Fw(2)[+A][-A]w(1)FA etc... This one is an interesting one, you might wanna try it! You might have to play with the x- and y- scales to see what it looks like better. You can ASSUME that those are always followed by (x) but remember the data is only as good as the user enters it. Since the user has to specify the rules, they could make a mistake and leave off the closing paren by mistake. So that might make your program do something weird.
  15. I have another question about the project. Is there an easy way to get the program to redraw the L-system. The reason that I ask is that when you move the scroll bar so that the picture is not visible on the screen and you move back the picture on the canvas is no longer there. Can you override the paint function somehow to fix this problem. I noticed that your sample program, where you can push the buttons to paint on the cavas does, not handle this problem. Is it as simple as creating a paint procedure that basically redraws the l-system. In other words calls render-lsystem? Will the paint function automatically be called when the scrollbar is moved? Yes, what you need to do is override the paint() routine of the TurtleCanvas: void public paint (Graphics g) { // here we can re-generate the L-system // to do this you need the "path" string somehow // and then call renderLsystem() // the implementation of this is left as an exercise ;) } here is a little explanation on what is happening. This is taken from the Java tutorial section on Drawing: http://java.sun.com/nav/read/Tutorial/ui/overview/drawing.html Programs can draw only when the AWT tells them to. The reason is that each occurrence of a Component drawing itself must execute without interruption. Otherwise, unpredictable results could occur, such as a button being drawn halfway, and then being interrupted by some lengthy animation. The AWT orders drawing requests by making them run in a single thread. A Component can use the repaint() method to request to be scheduled for drawing. The AWT requests that a Component draw itself by invoking the Component's update() method. The default (Component) implementation of the update() method simply clears the Component's background (drawing a rectangle over the component's clipping area in the Component's background color) and then calling the Component's paint() method. The default implementation of the paint() method does nothing. the AWT causes a component to repaint itself by callnig the update() method. In a 'smart' drawing program, you would only re-draw the parts of the window that really needed to be re-drawn. So in these casees, we implement both update() and paint() and make paint() call update. We then make update() only re-draw the parts of the screen that are necessary to be re-drawn. But this is kind of hard to do with the L-system, so for this project we can just override paint as above and not worry about update. For more details see: Overview of Graphics http://java.sun.com/nav/read/Tutorial/ui/drawing/overview.html Eliminating Flashing http://java.sun.com/nav/read/Tutorial/ui/drawing/noFlashing.html
  16. On the project spec you have the following: <(x) darken turtle pen color... >(x) darken turtle pen color... Is one of these actually supposed to be lighten?? If so, which one? Or are they supposed to do the same thing? One of them should be lighten and the other should be darken. My mistake! it does no really matter which one does which as one does darken and the other does lighten, how about: >(x) for darken <(x) for lighten
  17. You mentioned early today that there is a symbol that allows for the changing of the lengthy in the alphabet. However, I do not see one in the project spec. Should one be added? No it is not necessary to add one. I have seen several turtle alphabets and just thought our alphabet had one.
  18. I just wanted to point out that I have found a bug in the l-system generator code, or at least I believe I have. Try the string F[cFF0000+F] and generate an l-system of second order. It treats the FFs in the cFF0000 as occurrences of the axiom and not as the hexadecimal F that it actually represents. Is this indeed a bug or is this not a valid string or I am thinking wrong. yeah, the lsystem generator is not as robust as we would like. When it encounters the F, it doesn't remember that it just saw a 'c' when applying rules. So, it treats the Fs as part of the hex number as if they were F's to be replaced with the successor to a rule. There is a workaround. Specify the hexadecimal colors using lowercase letters instead. E.g., instead of FF0000 use ff0000 I think it will then work correctly.
  19. In the project specs you mention 2 different turtle operations: +/- turn left/right \ or / (x) roll left/right

    What is the difference between turn and roll? Are they both controlled by the _angle member of the Turtle class? Which controls in TurtleDraw control panel correspond to these actions?

    In the TurtleDraw control panel, you can set: length: the length the turtle moves/draws width: the width of the line drawn angle: how many degrees the turtle moves during a 'turn' (this is the second row of the control panel with the UpdateTurtleParms button) Whenever you turn (+ or - ) we make the turtle rotate by 'angle' degrees. So this actually changes the orientation of the turtle. This is the Rotate Left and Rotate Right buttons. Rolling does not actually change the turtle's orientation; it updates 'angle' to a new value: \(x) roll the turtle left (increase current angle) by x (degrees) /(x) roll the turtle right (decrease current angle) by x (degrees) So when you roll you need to update the _rotateAngle variable appropriately so that the next time you turn (rotate) left or right, you use the new _rotateAngle value. (Or whatever variable you happen to use for the rotate angle)
  20. What's the function of the turtle stack and how exactly do we go about building it (or using it)? Note in the turtle alphabet, these two symbols: [ which means PUSH ] which means POP the idea is that you can push the turtle state onto a stack and let the l-system go off for a while until it sees a pop which takes it right back to the previous state that was pushed. This is useful for making branching structures (l-systems can be used to draw plants, trees, etc) Java provides a simple stack class, if you import: import java.util.Stack; you use it like this: // note this assumes you have a valid turtle which you new'ed somehwere Turtle turtle; // ... turtle = new Turtle ( ... ); // Create a stack Stack turtleStack = new Stack(); // push a turtle onto the stack turtleStack.push (turtle); // pop a turtle off the stack turtle = (Turtle) turtleStack.pop();
  21. The cRRGGBB token is supposed to change the turtle's pen color to the RGB value specified by RRGGBB.

    How do we convert RRGGBB string to the instance of Color class?

    The RR GG and BB are hex values; e.g., you might encounter cff0000 which means change the turtle's pen color to RED. so you need to extract the RRGGBB portion into a string: so that some string, say colorString = "ff0000" Note that Color has the following constructors: Color (int rgb); // color as single (decimal number) Color (int r, int g, int b); // you need decimal values for r, g, b so you need some way of either converting the colorString into it's equivalent integer value; or you need to convert the RR, GG, and BB portions of the color string into 3 different ints. How you do that is up to you. here is one more hint: you can use the Java Integer class to convert string representations of integers to Integer instances: public Integer valueOf (String s, int radix); note valueOf() may throw the exception NumerFormatException you need to catch it like I have done in the example LsystemGen applet you can then convert that Integer instance into an int: public int intValue (); Once you have the int value, you can create the Color. Fore more about Integer see: java.lang.Integer
  22. I have one more quick question about the project. Is there an easy way to convert a string to a floating point number? For the purpose of accepting a real number as either the rotate angle or the start angle. Is there something comperable to the parseInt function? Yes, the Float class (java.lang.Float) can do this. here is a simple way: // Note myText is a TextField component String s = myText.getText(); float f1 = (float) 0.0; try { f1 = Float.valueOf(s).floatValue(); } catch(NumberFormatException e) { // let user know they did not enter a float correctly // for that field getAppletContext().showStatus ("MYTEXT must be a float"); }
  23. I've been working some more on the project and I have the set turtle pen color working (cXXXXXX) by using the colorFromString method in the DrawingCanvas class. Now I'm trying to work on the darken and lighten pen color commands <(X) and >(X). My question is, are we supposed to use the adjustColor method in the DrawingCanvas class? If so, how do we specify that component (the first parameter to the method)? Are we supposed to just lighten or darken one of the colors, (red green or blue) or the entire string? Let's say you find in your string >(20) this means darken by 20%. So suppose color=rrggbb in the RGB notation. You need to darkne each of rr, gg, and bb by 20%. Note that eacah rr and gg and bb go from 0 (darkest) to 255 (lightest) So when you darken by 20% you want to actually decrease each of rr, gg, and bb by (1-percentage) For lighten you multuply each of rr, gg, and bb by (1+percentage) You have to make sure that after you compute the new values for each of rr, gg, and bb that the value is still between 0 and 255, if not yo need to correct it. Oh, did I give you adjustColor() well then yes, it is easy: // darken current red value int newRR = adjustColor (currentRR, percent, "darken"); you will have to call adjustColor for each of rr, gg, and bb and then create a new Color to update the turtle pen color with
  24. How do you specify the radix in the call to: public Integer valueOf (String s, int radix); Specifically how do you specify the radix so that conversion from hex to decimal can be made? The radix is the same as the 'base' so pass 16 as the radix: String hexVal = "0000ff"; int hex = 0; try { hex = Integer.valueOf (hexVal, 16).intValue(); } catch (NumberFormatException e) { System.out.println ("Not a hex number: " + hexVal); } System.out.println ("hexVal = " + hexVal + " which is " + hex + " in decimal" );
  25. How picky do we have to be about erors in the actual rules. For instance, if one of the rules contains the following: /(40) but the user forgets the second parenthese: /(40 Should our system be good enough to handle this? Or if the user is trying to change the color and they don't give exactly 6 digits: F->c12345 If we do need to handle these errors, where would be the best place to handle this? Should we check before the rule is added or when the user goes to generate the L-system? There are two ways to do this: ( (1) validate the rules when they are entered to make sure they are not malformed before even generating the L-system; you would want to let the user know what is wrong with the rule with an error message or some kind of feedback (2) do checking while you are scanning the generated string; in this case you might stop rendering if you find an error, or just skip ahead to a symbol that you can start drawing from again What you do is up to you. For example, you might be able to guess: /(40 since a percentage has only 1 or 2 digits, you could just pretend you saw the closing parentheses. Or you could just report an error, 'missing close paren' and stop rendering or you could skip ahead until you find a symbol that matches one of the rules. e.g, : /(40f[+F] you are looking for a right paren, but there is not one. You could just ignore the /(40 and start at the [ For the case of F->c12345 you would not want to do anything to the color since you need 6 digits, so you might stop rendering and report an error or just skip it and move to the next valid character. Kind of like what a good parser does. ideally you want to catch errors early on, so checking it when rules are entered is probably the best way. Then you do not have to do any checking during the scanning of the generated strnig during rendering.
  26. i have a problem using the stacks. no matter what i do, the turtles doesn' get initialize to its former selves

    here's part of my code:

    case '[' : { (Turtle) turtleStack.push((Turtle) turtle); break; } case ']' : { turtle = new Turtle((Turtle)turtleStack.pop()); break; } i tried all sorts of combinations of putting the object, (Turtle) in different places of the pop and push calls, but I still can't get it to work right.
    You need to call the copy constructor, but it needs to be done at push time not pop time. When you push you just need to call push to push the current turtle on the stack, that will save the current state, but now you need to create another turtle to use from that point on. so you also need to create a new turtle, which is essentially a copy of the turtle just pushed: // for push turtleStack.push(turtle); turtle = new Turtle (turtle); break; The copy constructor does not need to be called when you pop: // for pop turtle = (Turtle) turtleStack.pop(); break; Remember that java relies on reference counting to keep track of objects. as soon as turtle is assigned to another turtle when we pop, whatever object turtle was pointing at is lost and will be reclaimed by the java garbage collector.