944,144 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 479
  • Java RSS
Oct 31st, 2009
0

Storing image on a class

Expand Post »
Greetings


i have created the above class to create rooms and exits of the rooms. That way every room is linked to another room and i can move between them. Now what i want to do is to add an image to every room that is stored inside the class.

I suppose i must use the swing class but how do i store an image with every room i create? I suppose i should make a function that stores the path to the file system of the image inside the room class and i call that function through my main to define that argument for a specific object?

Java Syntax (Toggle Plain Text)
  1. public class Room
  2. {
  3. private String description;
  4. private HashMap <String, Room> exits; // stores exits of this room.
  5.  
  6.  
  7. /**
  8.   * Create a room described "description" with a given image.
  9.   * Initially, it has no exits. "description" is something like
  10.   * "in a kitchen" or "in an open court yard".
  11.   */
  12. public Room(String description)
  13. {
  14. this.description = description;
  15. exits = new HashMap <String, Room> ();
  16.  
  17. }
  18.  
  19. /**
  20.   * Define an exit from this room.
  21.   */
  22. public void setExit(String direction, Room neighbor)
  23. {
  24. exits.put(direction, neighbor);
  25. }
  26.  
  27. /**
  28.   * Return the description of the room (the one that was defined in the
  29.   * constructor).
  30.   */
  31. public String getShortDescription()
  32. {
  33. return description;
  34. }
  35.  
  36. /**
  37.   * Return a long description of this room, in the form:
  38.   * You are in the kitchen.
  39.   * Exits: north west
  40.   */
  41. public String getLongDescription()
  42. {
  43. return "You are " + description + ".\n" + getExitString();
  44. }
  45.  
  46. /**
  47.   * Return a string describing the room's exits, for example
  48.   * "Exits: north west".
  49.   */
  50. private String getExitString()
  51. {
  52. String returnString = "Exits:";
  53. for (Object key : exits.keySet()) {
  54. returnString += " " + key;
  55. }
  56. return returnString;
  57. }
  58.  
  59. /**
  60.   * Return the room that is reached if we go from this room in direction
  61.   * "direction". If there is no room in that direction, return null.
  62.   */
  63. public Room getExit(String direction)
  64. {
  65. return (Room)exits.get(direction);
  66. }
  67.  
  68.  
  69. }
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Blondeamon is offline Offline
23 posts
since Dec 2007
Oct 31st, 2009
0
Re: Storing image on a class
Add field
Quote ...
Image image;
in Room .
Extend the Room constructor , or write the new one, which takes into account a new parameter, image.
If you dane this, look
Quote ...
setExit(String direction, Room neighbor)
Each neighbor already has its own image!
//
Good idea is overwrite method
Quote ...
public String toString()
which represent all fields. In this case
Quote ...
description,exits,image
After instantiate for example Room kitchen = new Room(parameters);
you can simply check this room, printing kitchen to system console.
After adding new exits, too.
But attention. Keep in mind - in case if extis of rooms are looped, method toString will lead to a looping program.
Last edited by quuba; Oct 31st, 2009 at 1:24 pm.
Reputation Points: 123
Solved Threads: 106
Posting Pro
quuba is offline Offline
573 posts
since Nov 2008
Oct 31st, 2009
0
Re: Storing image on a class
I am sorry but i didnt understand the second part. I get the part of changing my constructor to take an image object but not the rest.

Wouldnt it be better if if had on my constructor as an extra argument just a string with the path of the file on my filesystem?
Reputation Points: 10
Solved Threads: 0
Newbie Poster
Blondeamon is offline Offline
23 posts
since Dec 2007
Oct 31st, 2009
0
Re: Storing image on a class
Quote ...
Wouldnt it be better if if had on my constructor as an extra argument just a string with the path of the file on my filesystem?
Loading images from filesystem throws I/O error.
I think , better way (for me) is load all images in initial phase of program. For example in specialized method loadImages() , that returns array of images. Both ways are equal.

Referring to my statement "method toString" your method getLongDescription() is better, meets the same functions and is loop-crash immune, I had not noticed it.
Quote ...
in case if exits of rooms are looped
for example exit rooms are cycled A,B,C,D,A.......
Last edited by quuba; Oct 31st, 2009 at 3:45 pm.
Reputation Points: 123
Solved Threads: 106
Posting Pro
quuba is offline Offline
573 posts
since Nov 2008

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Pls Help me to combine two program.
Next Thread in Java Forum Timeline: how to change postfix to infix expression





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC