| | |
Storing image on a class
![]() |
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?
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)
public class Room { private String description; private HashMap <String, Room> exits; // stores exits of this room. /** * Create a room described "description" with a given image. * Initially, it has no exits. "description" is something like * "in a kitchen" or "in an open court yard". */ public Room(String description) { this.description = description; exits = new HashMap <String, Room> (); } /** * Define an exit from this room. */ public void setExit(String direction, Room neighbor) { exits.put(direction, neighbor); } /** * Return the description of the room (the one that was defined in the * constructor). */ public String getShortDescription() { return description; } /** * Return a long description of this room, in the form: * You are in the kitchen. * Exits: north west */ public String getLongDescription() { return "You are " + description + ".\n" + getExitString(); } /** * Return a string describing the room's exits, for example * "Exits: north west". */ private String getExitString() { String returnString = "Exits:"; for (Object key : exits.keySet()) { returnString += " " + key; } return returnString; } /** * Return the room that is reached if we go from this room in direction * "direction". If there is no room in that direction, return null. */ public Room getExit(String direction) { return (Room)exits.get(direction); } }
•
•
Join Date: Nov 2008
Posts: 332
Reputation:
Solved Threads: 53
0
#2 27 Days Ago
Add field in Room .
Extend the Room constructor , or write the new one, which takes into account a new parameter, image.
If you dane this, look Each neighbor already has its own image!
//
Good idea is overwrite method which represent all fields. In this case 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.
•
•
•
•
Image image;
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)
//
Good idea is overwrite method
•
•
•
•
public String toString()
•
•
•
•
description,exits,image
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; 27 Days Ago at 1:24 pm.
•
•
Join Date: Nov 2008
Posts: 332
Reputation:
Solved Threads: 53
0
#4 27 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?
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
Last edited by quuba; 27 Days Ago at 3:45 pm.
![]() |
Other Threads in the Java Forum
- Previous Thread: Textpad help!
- Next Thread: Reading in a *.csv file and loading the data into an Array
| Thread Tools | Search this Thread |
911 actionlistener addressbook android api append applet application array arrays automation binary blackberry block bluetooth character chat class client code component consumer csv database desktop developmenthelp eclipse error fractal ftp game givemetehcodez graphics gui html ide image integer j2me j2seprojects japplet java javaarraylist javac javaee javaprojects jni jpanel julia lego linked linux list loops mac map method methods mobile netbeans newbie number objects online oriented panel printf problem program programming project projects properties recursion replaydirector reporting researchinmotion rotatetext rsa scanner se server set singleton sms sort sql string swing test textfields threads time title tree tutorial-sample ubuntu update windows working





