GUI的图像问题

时间:2014-05-09 22:14:26

标签: java swing user-interface

嘿伙计们我是初学程序员,现在正在研究GUI。我知道如何将图像添加到JFrame的内容窗格并让它显示,但我要做的是当我在我的gui中玩这个游戏时,我输入的每个房间都必须在GUI中显示一个新图像。下面是我的createRooms方法。

    private void createRooms(){

    Room outside, theater, pub, lab, office, transporter1, transporter2;

    // create the rooms
    outside = new Room("outside the main entrance of the university");

    theater = new Room("in a lecture theater");
    pub = new Room("in the campus pub");
    lab = new Room("in a computing lab");
    office = new Room("in the computing admin office");
    transporter1 = new TransporterRoom("in space");
    transporter2 = new TransporterRoom("in a weird dimension");

    // initialise room exits
    outside.setExit("east", theater);
    outside.setExit("south", lab);
    outside.setExit("west", transporter2);

    theater.setExit("west", outside);

    //             transporter2.getExit();

    pub.setExit("east", outside);

    lab.setExit("north", outside);
    lab.setExit("east", office);

    office.setExit("west", lab);

    currentRoom = outside;  // start game outside

}

我想要做的是为每个房间添加一个imageIcon,我不完全确定如何做到这一点。提前致谢,如果您需要我的代码,请告诉我们!

编辑:这是房间类的代码,感谢codeNinja指出

private String description;
private HashMap<String, Room> exits;        // stores exits of this room.
public static ArrayList<Room> rooms = new ArrayList<Room>();




/**
 * Create a room described "description". Initially, it has
 * no exits. "description" is something like "a kitchen" or
 * "an open court yard".
 * @param description The room's description.
 */
public Room(String description) 
{
    this.description = description;
    exits = new HashMap<String, Room>();
    image = new ImageIcon("images");
    rooms.add(this);
}

/**
 * Define an exit from this room.
 * @param direction The direction of the exit.
 * @param neighbor  The room to which the exit leads.
 */
public void setExit(String direction, Room neighbor) 
{
    exits.put(direction, neighbor);
}

/**
 * @return The short description of the room
 * (the one that was defined in the constructor).
 */
public String getShortDescription()
{
    return description;
}

/**
 * Return a description of the room in the form:
 *     You are in the kitchen.
 *     Exits: north west
 * @return A long description of this room
 */
public String getLongDescription()
{
    return ("You are " + description + ".\n" + getExitString());
}

/**
 * Return a string describing the room's exits, for example
 * "Exits: north west".
 * @return Details of the room's exits.
 */
private String getExitString()
{
    String returnString = "Exits:";
    Set<String> keys = exits.keySet();
    for(String exit : keys) {
        returnString += " " + exit;
    }
    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.
 * @param direction The exit's direction.
 * @return The room in the given direction.
 */
public Room getExit(String direction) 
{
    return exits.get(direction);
}

}

1 个答案:

答案 0 :(得分:0)

有两种方法可以做到这一点。 首先,您可以将JLabel添加到contentPane并将图像设置为该JLabel。但这不是最好的方法因为如果你想把组件放在它上面你必须使用绝对布局。 第二个是将JPanel放在框架中并实现paintComponent方法。

JPanel imagePanel= new JPanel(){
   @Override
   public void painComponent(Graphics g){
      super.painComponent(g);
      g.drawImage(yourPrefImage,0,0,sizeOfPanelX(Frame),sizeOfPanelY(Frame),null);
   } 
}