We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpSyntax Questions › [Solved]How to get a Hashmap value
Page Index Toggle Pages: 1
[Solved]How to get a Hashmap value ? (Read 913 times)
[Solved]How to get a Hashmap value ?
Sep 4th, 2009, 2:10am
 
Hello,

When trying to build a scrolling ticker, I store 32*32px images in an array letterImages and then I store the corresponding letters in a hashmap :

Code:
 



HashMap hm = new HashMap();
hm.put("a", 1); // image containing the "a" symbol
hm.put("b", 2);
hm.put("c", 3);
...


Then i want to to display the image 2 for exemple, that should display letter B image :
Code:


pg.image (letterImages[<!--  What goes here ?  -->], x, y, w, h);


For example I would like to display letter B this way :

Code:
letterImages[hm.get('b')]; 



But it don't work. Any help appreciated
Re: [Total newbie]]How to get a Hashmap value ?
Reply #1 - Sep 4th, 2009, 2:21am
 
> hm.put("a", 1); // image containing the "a" symbol

no it's not. all you're doing there is storing the integer "1" against the key "a", there's no image there. what are your images called?

generally you add objects to a hashmap as above

hm.put("a", imageA);

and then get them back out again using the key supplied in the put

image = (Type)hm.get("a");

where Type should be the type of object you stored, probably PImage in this case. you need this because they are stored within the hashmap as generic objects so you need to tell it the type on extraction otherwise it won't know and you won't be able to access your methods.

> letterImages[hm.get('b')];

oh, ok, it appears you have an array of images elsewhere and the hashmap is just storing the index. i wouldn't do it that way but... the obvious thing that's wrong in the above line is that the single quotes should be doubles - that's a String, not a character.

next time post more code so we don't have to guess what you're doing 8).
Re: [Total newbie]]How to get a Hashmap value ?
Reply #2 - Sep 4th, 2009, 3:36am
 
Just a note to you and other people: no need to prefix the subjects with [newbie] or similar. Lot of people here are newbies, and we don't think a question is too dumb: if it asks elementary things, we know we answer a newbie, and are kind anyway... Smiley
Another hint is that newbies always ask questions in the Syntax section, even if the question isn't about syntax (your is OK here!).

koogy answered well to your question. Basically, you should do as advised, ie. put the PImages directly in the HashMap, it is perfectly suited to this usage.
Something like:
Code:
HashMap hm = new HashMap();
for (int i = 1; i <= 26; i++)
{
 String c = new String(Character.toChars(64 + i)); // Not very orthodox...
 hm.put(c, loadImage(c + ".png"));
}
hm.put(" ", "space.png";
String message = "Hello world";
for (int i = 0; i < message.length(); i++)
{
 String c = message.substring(i, i + 1).toUpperCase();
 PImage pi = (PImage) hm.get(c);
 if (pi != null) // Found
 {
   image(pi, 10 + 32 * i, 10);
 }
}
Re: [Total newbie]]How to get a Hashmap value ?
Reply #3 - Sep 4th, 2009, 12:14pm
 
Thanks a bunch for those explanations.

Page Index Toggle Pages: 1