I would really appreciate your help with this. I have an arrayList of objects that are to be represented by a treemap. I want to traverse
backwards through the arrayList and increase the size of each items bounds as I do so. The code appears to be traversing backwards through the arrayList but is increasing the size of the items in the wrong order! The top left rectangle in the treemap should be the biggest but at the moment it is the smallest. Perhaps my logic is flawed but if what I'm trying to do is possible I would love to know where my mistake is!
Here is my code (This method is called repeatedly for each new item to be added):
void addRank(TitleItem title){
int arraySize = ranks.size();
ranks.add(title);
println("SIZE: " + ranks.size());
ListIterator listIterator = ranks.listIterator();//DECREASES the size of
//each position after the first position, making first the largest
Gives me this error: The method get(int) in the type ArrayList is not applicable for the arguments (String)
So I changed it to:
void addRank(String title){ int arraySize = ranks.size(); ranks.add(title);
TitleItem item = (TitleItem) ranks.get(arraySize);//gets most recent entry to ArrayList
}
which gives me this error: ClassCastException java.lang.String cannot be cast to SketchName$TitleItem
Can anyone explain these errors and possible alternatives to me? From looking up the last error online it seems I can't cast a string variable to a class variable... but then how does it work in the simple treemap example in Ben Frys book "Visualizing Data"?
Would really appreciate any help as I'm still learning :) thanks in advance :D
I am a Processing beginner although I have some Java experience. At the moment I'm trying to implement a treemap that will visualise a set of rankings. The information is in a text file in the format: title -rank, title -rank, etc. all on one line. I read this into the program using loadStrings(). I have split the text so that title is in one array position and the corresponding rank is in the very next array position, parsed the rank into an integer and inserted the data into a hashmap. I have been using Ben Fry's simple treemap example from the book Visualizing Data as a basis. I thought I understood it, but now I'm trying to implement my own I keep getting confused
In the example, the Hashmap consists of object/string pairs but my hashmap has string/integer pairs. Do I need to make an object class for the title, the title and the rank or neither?? I'm assuming I'll need a class for the title but then I'm not sure what to do about its corresponding rank... should it be a field in the title class? The rank will determine the size of the rectangle to be drawn by the way, with one being the smallest and the lower ranks increasing in rectangle size. Would the title class look like this(rank doesn't do anything cause I don't know what to with it yet):
class TitleItem extends SimpleMapItem { String title; int rank;
fill(0); //the text is only drawn if the w and h of the text are //smaller than the w and h of the rect
if(w > textWidth(title) + 6) { if (h > textAscent() + 6) {// textAscent() used to determine height of font //above baseline textAlign(CENTER, CENTER); text(title, x + w/2, y + h/2); } } }