Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (541)
Games in Android Showcase (133)
games submitted by our members
Games in WIP (603)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
Pages: [1]
  ignore  |  Print  
  Yes I made a mess, out of hashtables  (Read 1818 times)
0 Members and 1 Guest are viewing this topic.
Offline Coinerson

Junior Devvie




Introducing the world's cutest zombie, Timmy


« Posted 2006-08-10 01:31:19 »

1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
public class PlayerSprite extends Critter { 
   
   private long updateTime;
   private Hashtable characterAttribs;
   private Hashtable characterAnims;
   private String currentCharacter;
 
    public PlayerSprite(String startingCharacter, Hashtable animTable) {
      super((ImageSequence)animTable.get(startingCharacter).get("north"));
      characterAttribs = new Hashtable<String, String[]>();
      characterAnims = new Hashtable();
     
      currentCharacter = startingCharacter;
      characterAnims = animTable;
     
      String[] catAttribs = {"Mountains", "Field", "Tree"};
      String[] turtleAttribs = {"Lava", "Cold Water", "Ocean Water"};
      String[] snakeAttribs = {"Desert", "Swamp Water", "Trees"};
      String[] rabbitAttribs = {"Field", "Underground", "Tundra"};
      String[] penguinAttribs = {"Tundra", "Cold Water", "Ocean Water"};
      String[] salamanderAttribs = {"Lava", "Cold Water", "Swamp Water"};
      String[] mouseAttribs = {"Underground", "Desert", "Mountains"};
      String[] lizardAttribs = {"Trees", "Desert", "Lava"};
      String[] frogAttribs = {"Trees", "Underground", "Swamp Water"};
      String[] foxAttribs = {"Field", "Tundra", "Mountains"};
     
      //insert character data
      characterAttribs.put("Cat", catAttribs);
      characterAttribs.put("Turtle", turtleAttribs);
      characterAttribs.put("Snake", snakeAttribs);
      characterAttribs.put("Rabbit", rabbitAttribs);
      characterAttribs.put("Penguin", penguinAttribs);
      characterAttribs.put("Salamander", salamanderAttribs);
      characterAttribs.put("Mouse", mouseAttribs);
      characterAttribs.put("Lizard", lizardAttribs);
      characterAttribs.put("Frog", frogAttribs);
      characterAttribs.put("Fox", foxAttribs);
   }



animTableComes carrying the keys "Cat" ect... as you see where i am creating attrib arrays. Each key value has another hashtable which stores a string key for the animation and the values are image sequences. Here is my error:

1  
2  
3  
4  
5  
6  
7  
8  
9  
adsl-71-146-102-201:~/ My Apps/Java/2D/Animal Game johncarlyle$ javac PlayerSprite.java
PlayerSprite.java:19: cannot find symbol
symbol  : method get(java.lang.String)
location: class java.lang.Object
                super((ImageSequence)animTable.get((String)startingCharacter).get("north"));
                                                  ^
Note: PlayerSprite.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
1 error


Not quite sure what it means. This is the first half of my question...
Offline Coinerson

Junior Devvie




Introducing the world's cutest zombie, Timmy


« Reply #1 - Posted 2006-08-10 01:38:06 »

(This doesnt fit on one post)

Oh right the intent, is to get the starting characters northern movment animation from the hashtable. Might as well post my resource manager so you can see if i packed the hashtable badly.



resource manager: it is init'd then loadFirstMap()  is called the variable first carries "Cat"



1  
2  
3  
4  
5  
6  
7  
8  
9  
10  
11  
12  
13  
14  
15  
16  
17  
18  
19  
20  
21  
22  
23  
24  
25  
26  
27  
28  
29  
30  
31  
32  
33  
34  
35  
36  
37  
38  
39  
40  
41  
42  
43  
44  
45  
46  
47  
48  
49  
50  
51  
52  
53  
54  
55  
56  
57  
58  
59  
60  
61  
62  
63  
64  
65  
66  
67  
68  
69  
70  
71  
72  
73  
74  
75  
76  
77  
78  
79  
80  
81  
82  
83  
84  
85  
86  
87  
88  
89  
90  
91  
92  
93  
94  
95  
96  
97  
98  
99  
100  
101  
102  
103  
104  
105  
106  
107  
108  
109  
110  
111  
112  
113  
114  
115  
116  
117  
118  
119  
120  
121  
122  
123  
124  
125  
126  
127  
128  
129  
130  
131  
132  
133  
134  
135  
136  
137  
138  
139  
140  
141  
142  
143  
144  
145  
146  
147  
148  
149  
150  
151  
152  
153  
154  
155  
156  
157  
158  
159  
160  
161  
162  
163  
164  
165  
166  
167  
168  
169  
170  
171  
172  
173  
174  
175  
176  
177  
178  
179  
180  
181  
182  
183  
184  
185  
186  
187  
188  
189  
190  
191  
192  
193  
194  
195  
196  
197  
198  
199  
200  
201  
202  
203  
204  
205  
206  
207  
208  
209  
210  
211  
212  
213  
214  
215  
216  
217  
218  
219  
220  
221  
222  
223  
224  
225  
226  
227  
228  
229  
230  
231  
232  
233  
234  
235  
236  
237  
238  
239  
240  
241  
242  
243  
244  
245  
246  
247  
248  
249  
250  
251  
252  
253  
254  
255  
256  
257  
258  
259  
260  
261  
262  
263  
264  
265  
266  
267  
268  
269  
270  
271  
272  
273  
274  
275  
276  
277  
278  
279  
280  
281  
282  
import java.awt.*;
import java.awt.geom.AffineTransform;
import java.io.*;
import java.awt.image.BufferedImage;
import java.io.IOException;
import javax.imageio.ImageIO;
import java.net.*;
import java.util.*;
   

public class ResourceManager {

   private ArrayList<BufferedImage> tiles;
   private int currentMap;
   private GraphicsConfiguration gc;
   private ImageSequence anims[];
   private BufferedImage spriteSheet;
   private ImageLoader loader;
   private Hashtable characterAnimations;
   
   
   public ResourceManager(GraphicsConfiguration gc)   {
      this.gc = gc;
      characterAnimations = new Hashtable();
     
      loader = new ImageLoader();
      loadTileImages();
      loadImages();
   }
   
   public Hashtable getAnims()   {
      return characterAnimations;
   }
   
   public TileMap loadFirstMap()   {
      currentMap = 0;
      return loadNextMap();
   }
   
   public TileMap loadNextMap()   {
      TileMap map = null;
      while (map == null)   {
         currentMap++;
         try   {
            map = loadMap("Resources/Maps/Map" + currentMap + ".txt");
         }
         catch (IOException ex)   {
            if (currentMap == 1)   {
               return null;
            }
            currentMap = 0;
            map = null;
         }
      }
      System.out.println(map);
      return map;
   }
   
   public TileMap reloadMap()   {
      try   {
         return loadMap("Resources/Maps/Map" + currentMap + ".txt");
      }
      catch (IOException ex)   {
         ex.printStackTrace();
         return null;
      }
   }

   private TileMap loadMap(String filename) throws IOException   {
      ArrayList<String> lines = new ArrayList<String>();
      int width = 0;
      int height = 0;
      BufferedReader reader;
     
        try {
            ClassLoader classLoader = getClass().getClassLoader();
            URL url = classLoader.getResource(filename);
         if (url == null)   {
            throw new IOException("No such map:" + filename);
         }
         reader = new BufferedReader(new InputStreamReader(url.openStream()));
            
        } catch (Exception e) {
         throw new IOException();
        }
     
      LinkedList<String> party = new LinkedList<String>();
      String first = null;
     
      while (true)   {
         String line = reader.readLine();
         if (line == null)   {
            reader.close();
            break;
         }
         if (!line.startsWith("#"))   {
            lines.add(line);  
            width = Math.max(width, line.length());
         }
         else   {
            if (line.startsWith("# Party"))   {
               line = line.substring(line.indexOf(":")+2);
               
               //parse the party line
               int firstComma = line.indexOf(',');
               int secondComma = line.indexOf(',', firstComma+1);
               firstComma++;
               secondComma++;
     
               first = line.substring(1, firstComma-1);
               String second = line.substring(firstComma+1, secondComma-1);
               String third = line.substring(secondComma+1, line.length()-1);
               
               party.add(first);
               party.add(second);
               party.add(third);
            }
         }
      }
      height = lines.size();
      TileMap newMap = new TileMap(width, height, party);
      if (first == null)   {
         newMap.setPlayer(new PlayerSprite("Cat", characterAnimations));
      }
      else   {
         newMap.setPlayer(new PlayerSprite(first, characterAnimations));
      }
     
   
      return newMap;
   }
   
   
   private void addSprite(TileMap map, String hostSprite, int tileX, int tileY)   {
   }
   
   public void loadTileImages()   {
      ImageLoader loader = new ImageLoader();
      tiles = new ArrayList<BufferedImage>();
      char ch = 'A';
      while (true)   {
         String name = "tile_" + ch + ".png";
         ClassLoader classLoader = getClass().getClassLoader();
         URL url = classLoader.getResource("Resources/Images/Background Tiles/" + name);
         if (url == null)   {
            break;
         }
         tiles.add(loader.loadImage("Resources/Images/Background Tiles/" + name));
         ch++;
      }
   }
   
   public String numberToAnimal(int i)   {
      if (i == 0)   {
         return "cat";
      }
      if (i == 1)   {
         return "bird";
      }
      if (i == 2)   {
         return "fox";
      }
      if (i == 3)   {
         return "frog";
      }
      if (i == 4)   {
         return "liazrd";
      }
      if (i == 5)   {
         return "mouse";
      }
      if (i == 6)   {
         return "salamander";
      }
      if (i == 7)   {
         return "penguin";
      }
      if (i == 8)   {
         return "rabbit";
      }
      if (i == 9)   {
         return "snake";
      }
      if (i == 10) {
         return "turtle";
      }
      return null;
   }
   
   private void loadImages()   {
      spriteSheet = loader.loadImage("Resources/Images/sheet.png");
     
      for (int i = 0 ; i < 11 ; i++ )   {
         Hashtable iAnims = new Hashtable();
         BufferedImage savedImage = spriteSheet;
         for (int x = 0 ; x < 18 ; x++)   {
            if (x == 0)      {
               savedImage = spriteSheet.getSubimage(x*36, i*36+179, 36, 36);
            }
            if (x == 1)      {
               ImageSequence seq =  new ImageSequence();
               seq.addFrame(savedImage, 150);
               seq.addFrame(spriteSheet.getSubimage(x*36, i*36+179, 36, 36), 150);
               iAnims.put("north", seq);
            }
            if (x == 2)      {
               savedImage = spriteSheet.getSubimage(x*36, i*36+179, 36, 36);
            }
            if (x == 3)      {
               ImageSequence seq =  new ImageSequence();
               seq.addFrame(savedImage, 150);
               seq.addFrame(spriteSheet.getSubimage(x*36, i*36+179, 36, 36), 150);
               iAnims.put("north east", seq);
            }
            if (x == 4)      {
               savedImage = spriteSheet.getSubimage(x*36, i*36+179, 36, 36);
            }
            if (x == 5)      {
               ImageSequence seq =  new ImageSequence();
               seq.addFrame(savedImage, 150);
               seq.addFrame(spriteSheet.getSubimage(x*36, i*36+179, 36, 36), 150);
               iAnims.put("east", seq);
            }
            if (x == 6)      {
               savedImage = spriteSheet.getSubimage(x*36, i*36+179, 36, 36);
            }
            if (x == 7)      {
               ImageSequence seq =  new ImageSequence();
               seq.addFrame(savedImage, 150);
               seq.addFrame(spriteSheet.getSubimage(x*36, i*36+179, 36, 36), 150);
               iAnims.put("south east", seq);
            }
            if (x == 8)      {
               savedImage = spriteSheet.getSubimage(x*36, i*36+179, 36, 36);
            }
            if (x == 9)      {
               ImageSequence seq =  new ImageSequence();
               seq.addFrame(savedImage, 150);
               seq.addFrame(spriteSheet.getSubimage(x*36, i*36+179, 36, 36), 150);
               iAnims.put("south", seq);
            }
            if (x == 10)   {
               savedImage = spriteSheet.getSubimage(x*36, i*36+179, 36, 36);
            }
            if (x == 11)   {
               ImageSequence seq =  new ImageSequence();
               seq.addFrame(savedImage, 150);
               seq.addFrame(spriteSheet.getSubimage(x*36, i*36+179, 36, 36), 150);
               iAnims.put("south west", seq);
            }
            if (x == 12)   {
               savedImage = spriteSheet.getSubimage(x*36, i*36+179, 36, 36);
            }
            if (x == 13)   {
               ImageSequence seq =  new ImageSequence();
               seq.addFrame(savedImage, 150);
               seq.addFrame(spriteSheet.getSubimage(x*36, i*36+179, 36, 36), 150);
               iAnims.put("west", seq);
            }
            if (x == 14)   {
               savedImage = spriteSheet.getSubimage(x*36, i*36+179, 36, 36);
            }
            if (x == 15)   {
               ImageSequence seq =  new ImageSequence();
               seq.addFrame(savedImage, 150);
               seq.addFrame(spriteSheet.getSubimage(x*36, i*36+179, 36, 36), 150);
               iAnims.put("north west", seq);
            }
            if (x == 16)   {
               savedImage = spriteSheet.getSubimage(x*36, i*36+179, 36, 36);
            }
            if (x == 17)   {
               ImageSequence seq =  new ImageSequence();
               seq.addFrame(savedImage, 150);
               seq.addFrame(spriteSheet.getSubimage(x*36, i*36+179, 36, 36), 150);
               iAnims.put("sleeping", seq);
            }
         }
         characterAnimations.put(numberToAnimal(i), iAnims);
      }
   }
}

Offline CaptainJester

JGO Knight


Medals: 12
Projects: 2
Exp: 14 years


Make it work; make it better.


« Reply #2 - Posted 2006-08-10 02:06:20 »

When you are doing a cast and then a method call on the same casted object, you need to put brackets around it.
1  
2  
//wrong
super((ImageSequence)animTable.get(startingCharacter).get("north"));


1  
2  
3  
//right
       \/                         \/
super( ( (ImageSequence)animTable ).get(startingCharacter).get("north"));

Games published by our own members! Check 'em out!
Legends of Yore - The Casual Retro Roguelike
Offline Coinerson

Junior Devvie




Introducing the world's cutest zombie, Timmy


« Reply #3 - Posted 2006-08-12 06:50:15 »

1  
super( ( (ImageSequence) animTable ).get( startingCharacter ).get( "north" ) );


I know what I am trying to do but I can't wrap my tiny brain around it... here is my error.

1  
2  
3  
4  
5  
6  
7  
8  
9  
PlayerSprite.java:19: inconvertible types
found   : java.util.Hashtable
required: ImageSequence
                super( ((ImageSequence) animTable).get( startingCharacter ).get( "north" ) );
                                        ^
Note: PlayerSprite.java uses unchecked or unsafe operations.
Note: Recompile with -Xlint:unchecked for details.
1 error
adsl-71-146-101-161:~/ My


Its giving me the nested hashtable not the image sequence within the nested hashtable
Offline purpleguitar

Junior Devvie





« Reply #4 - Posted 2006-08-12 12:25:37 »

1  
2  
3  
4  
PlayerSprite.java:19: inconvertible types
found   : java.util.Hashtable
required: ImageSequence
                super( ((ImageSequence) animTable).get( startingCharacter ).get( "north" ) );


This code contains an attempt to convert animTable to an ImageSequence.  As I understand it, you want to convert the contents of the hashtable to ImageSequences.  Take a look at the first parenthesized expression:

1  
(ImageSequence)animTable


This is analogous to:

1  
(int)x


That is, it converts animTable to an ImageSequence.  If you want to convert the result of "animTable.get(startingCharacter)" to an ImageSequence, you need to form it thusly

1  
(ImageSequence)animTable.get(startingCharacter)


which yields the following complete statement:

1  
super( ( (ImageSequence)animTable.get( startingCharacter)).get( "north" ) );


In the future, I recommend the use of generics to make life easier and reduce the number of parentheses.
Offline Coinerson

Junior Devvie




Introducing the world's cutest zombie, Timmy


« Reply #5 - Posted 2006-08-13 00:26:52 »

1  
2  
3  
4  
5  
PlayerSprite.java:21: cannot find symbol
symbol  : method get(java.lang.String)
location: class ImageSequence
                super( ( (ImageSequence)animTable.get( startingCharacter)).get( "north" ) );
                       ^


I am asking ImageSequence to get()? There are way too many ( and ) in there. What was this method you suggested I use instead of this ? Also whats wrong with the current code... again.
Offline purpleguitar

Junior Devvie





« Reply #6 - Posted 2006-08-14 11:58:38 »

1  
2  
3  
4  
5  
PlayerSprite.java:21: cannot find symbol
symbol  : method get(java.lang.String)
location: class ImageSequence
                super( ( (ImageSequence)animTable.get( startingCharacter)).get( "north" ) );
                       ^


I am asking ImageSequence to get()?

Why are you asking me?  It's your code.  Seriously, if you don't know what is in which table, then you probably need to refactor your solution.  In my experience, the use of cascading hashtables like this is usually a sign of poorly designed object interactions.  I don't mean to be overly harsh here, but rather I'm trying to steer you towards a better solution.

For example, why use a hashtable to store four animations and key them as "north", "south", etc.?  How about introducing an interface instead:

1  
2  
3  
4  
5  
interface CharacterAnimation {
  ImageSequence northAnim();
  ImageSequence southAnim();
  etc.
}


Now, write the rest of your code to refer to CharacterAnimation, and you have reduced the coupling between components.  Interfaces allow you to add a layer of abstraction such that you can replace the implementation without changing the interface.

More to the point:  consider the following code

1  
2  
3  
4  
5  
JPanel p = new JPanel();
Hashtable t = new Hashtable();
t.put( "foo", new JButton());

p.add(  (JButton) t.get("foo") );


The table maps strings to jbuttons, and so if I want to get at the JButton mapped by "foo", I must cast the result of the expression t.get("foo").  I could use more parentheses:

1  
p.add( (JButton) (t.get("foo") );


Maybe that approach makes more sense to you, but it is semantically equivalent to the other block of code.

Quote
There are way too many ( and ) in there.

Nope.  Unless I am misunderstanding the nature of your code, my previous example has the minimal number of parentheses.

Quote
What was this method you suggested I use instead of this ? Also whats wrong with the current code... again.

Recommendation #1: Refactor the code.  Rethink your object model.  Focus on the dynamic model:  what objects are responsible for processing what messages?  Test each class independently, then bring them together.
or
Recommendation #2:  Use the code I already provided.

What is wrong with your current code is that you were casting the table itself to a new class, not the result of the "get" expression.  It would be as if I used my jbutton example above and tried to do this:
1  
((JButton)t).get("foo")


The key is the first part:  ((JButton)t).  This casts the table into a JButton, or attempts to.  It cannot be done, since these two types are -- to use the phrase the compiler helpfully gave you -- inconvertible.
Offline Coinerson

Junior Devvie




Introducing the world's cutest zombie, Timmy


« Reply #7 - Posted 2006-08-16 01:01:42 »

Alright, took a different direcion, and it works perfectly fine. Not sure why I tried nested hashtables in the first place, I don't like single ones... Thanks for all your help.
Pages: [1]
  ignore  |  Print  
 
 
You cannot reply to this message, because it is very, very old.

 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

Mr.CodeIt (24 views)
2014-12-23 03:34:11

rwatson462 (54 views)
2014-12-15 09:26:44

Mr.CodeIt (45 views)
2014-12-14 19:50:38

BurntPizza (86 views)
2014-12-09 22:41:13

BurntPizza (110 views)
2014-12-08 04:46:31

JscottyBieshaar (80 views)
2014-12-05 12:39:02

SHC (91 views)
2014-12-03 16:27:13

CopyableCougar4 (98 views)
2014-11-29 21:32:03

toopeicgaming1999 (157 views)
2014-11-26 15:22:04

toopeicgaming1999 (154 views)
2014-11-26 15:20:36
Resources for WIP games
by kpars
2014-12-18 10:26:14

Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!