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?

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);
    }

	
}

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.

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?

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.......

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.