Storing image on a class

Reply

Join Date: Dec 2007
Posts: 21
Reputation: Blondeamon is an unknown quantity at this point 
Solved Threads: 0
Blondeamon's Avatar
Blondeamon Blondeamon is offline Offline
Newbie Poster

Storing image on a class

 
0
  #1
29 Days Ago
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?

  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. }
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 332
Reputation: quuba is on a distinguished road 
Solved Threads: 53
quuba quuba is offline Offline
Posting Whiz
 
0
  #2
28 Days Ago
Add field
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
setExit(String direction, Room neighbor)
Each neighbor already has its own image!
//
Good idea is overwrite method
public String toString()
which represent all fields. In this case
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; 28 Days Ago at 1:24 pm.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 21
Reputation: Blondeamon is an unknown quantity at this point 
Solved Threads: 0
Blondeamon's Avatar
Blondeamon Blondeamon is offline Offline
Newbie Poster
 
0
  #3
28 Days Ago
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?
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 332
Reputation: quuba is on a distinguished road 
Solved Threads: 53
quuba quuba is offline Offline
Posting Whiz
 
0
  #4
28 Days Ago
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.
in case if exits of rooms are looped
for example exit rooms are cycled A,B,C,D,A.......
Last edited by quuba; 28 Days Ago at 3:45 pm.
Reply With Quote Quick reply to this message  
Reply

Message:



Other Threads in the Java Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC