Spark LineGrid Component

Since I had a day off, I decided to work on converting my old Flex Examples to the new Flex 4.  As stated in a previous post, the new Flex 4 breaks Degrafa and Degrafa seems to be defunct now.  Most of the drawing functions are handled with spark and sprite functions, but little things like arcs and line repeaters are not.  So where before it was easy enough to create a repeating line in Degrafa, now I have to do a loop to create the lines needed.  Well, in this case, instead of drawing lines, I’m drawing rectangles.  While it might seem simple at first just to draw a bunch of rectangles together to create a grid, there is a little trickiness to it.  You can have to account for the size of the total grid, based on the number of columns, rows and the grid box size.  And you also have to use both the column and row count value to draw the correct number of rows and columns and at the right size.  It is all just a matter of thinking about it for a few moments and then having an “ah ha!” moment.  Another little quirk with the components is that when you add a visual element, it puts it in the center of the group component.  On the Egg Timer, I just left it there for now because it worked for what I needed.  I might change it later.  But for the LineGrid, it made for it not to line up correctly.  So, before I start drawing the rectangles, I had to determine the actual 0,0 location first.  It also took a couple tries to make sure I was using the right zero.


// Find the actual 0,0 start
var actualZeroX:int = -1 * ((columnCount * boxSize) * .5);
var actualZeroY:int = -1 * ((rowCount * boxSize) * .5);

One good thing about the new components, is that when you use the declarations, it is the same as creating a set function, which means less code.  I simply added declarations for the column and row counts, then put those values where I need them.  When I use the LineGrid component I add the values I need just like any other attribute.


<components:LineGrid id="lineGrid1" rowCount="5" columnCount="5" boxSize="50" lineThickness="4" lineColor="0x009900" />

Here is the example showing a grid with equal columns and rows and then two other grids with column or rows determined by the size of the application.

Get Adobe Flash player

Autoscroll Textarea

One of the ways I like to debug my Flex applications is putting a TextArea item that is visible when needed to display certain items that usually would be display in a trace.  The benefit of this little trick is that I can see what is going on without having to go through Flex Builder.  Also, I happened to create a web service debugging application and it displayed the error and result text from the web service.  When displaying either debug or general result information, it is helpful if the TextArea scrolls up to show the latest information added.  I found code on the to do this in Flex 3, but things changed in Flex 4.  So I did a little searching and found some code that had a little too much code.  There is a warning at the bottom of that post which states the API at the time was in Beta.  Maybe adding event listeners is the more correct way to automatically scroll the TextArea, but it certainty isn’t the quickest way.  Since the Flex 3 version was fairly simple, I took what I saw there and applied it in the way the old method worked.

The first thing is that the scroller is now it is own component within the TextArea.  So, you first have to access that withing the TextArea.  Then you need to choose either the vertical or horizontal scrollbar.  Since I set the width and the words automatically wrap, I only need the vertical scrollbar.  Then you get the value of the scrollbar, which you can read or set.  The key is to set the scrollbar to its maximum position, which in the case of a vertical scoller would show the bottom of the TextArea.  You change this value with the valueCommit function, that allows you to change things after a value is added.  Just don’t change the content value or you go into a loop.


<s:TextArea id="fooText" width="200" height="200"
valueCommit="fooText.scroller.verticalScrollBar.value = fooText.scroller.verticalScrollBar.maximum" />

Unfortunately, it doesn’t seem to scroll completely down to the bottom completely.  I’ve created a little application to show text being added to the TextArea.  Every 10 seconds a random set of words will be added to each of the TextAreas.  As the amount of text increases, the TextArea with the dynamic scroller will scroll down, while the other TextArea just increases the scroller size.  To generate the random words, I used a few quotes from the Mark Twain quote site.  From those quotes, I used a script to convert the phrase words into an array. I modified the examples in order to pass in the array that I want to add the words to and get back a new array of words.  From there I used a basic random number in range script to get a random number of words.  Once I had the random number of words, I just grabbed that many words in a loop from the Array.  Finally, I adjusted the first word to make sure it had the first letter as upperCase and the rest of the words to lowerCase.


public static function getArrayFromString(inputPhrase:String, sourceArray:Array):Array {
var reg:RegExp = /\W/i;
var wordsAsArray:Array = inputPhrase.replace(reg, "").split(" ");

for(var w:int=0; w < wordsAsArray.length; w++){
sourceArray.push(wordsAsArray[w].toString());
}

return sourceArray;
}

public static function upperCase(str:String) : String {
var firstChar:String = str.substr(0, 1);
var restOfString:String = str.substr(1, str.length);

return firstChar.toUpperCase() + restOfString.toLowerCase();
}

Here is the example. When the text expands past the available area for the first time, bot TextAreas look the same.  When the next random phrase is created, then the difference is more apparent.

Get Adobe Flash player