在类中添加一个简单的GIF

时间:2013-09-02 19:00:40

标签: java class file-io gif

我在编程2课程中,教授希望我们创建一个最终会发展成游戏的课程。我遇到的问题是我不知道如何将文件添加到类中,这样当我使用测试器时,我没有错误。我不必对文件做任何事情。我只需要在myPlayer2之后在测试仪中制作几个实例,但我不知道该怎么做。我希望这和我的代码有意义。谢谢。

import java.io.File;



public class myPlayer {

private String name;
private String gender;
private String role;
private int health;
private String state;
private File sprite = new File("MarioSprite.gif");
/**
 * constructor for the game player
 * @param name-- the players name
 * @param gender -- the players gender
 * @param role -- role such as boss, healer, demon, knight
 * @param health -- percentage of health
 * @param state -- whether player is alive, dead, active, inactive, sleeping
 * @param sprite -- this is the sprite file for the class
 */

// the file name is MarioSprite.gif 

public myPlayer(){
    name = " unknown";
    gender = " unknown";
    role = " not defined";
    health =  0;
    state = " not defined";
    sprite = null;
}
public myPlayer(String name, String gender, String role, int health, String state, File sprite){
    this.name = name;
    this.gender = gender;
    this.role = role;
    this.health = health;
    this.state = state;
    this.sprite = sprite;
}
/**
 * @return the name
 */
public String getName() {
    return name;
}
/**
 * @param  the name to set
 */
public void setName(String name) {
    this.name = name;
}
/**
 * @return the gender
 */
public String getGender() {
    return gender;
}
/**
 * @param  the gender to set
 */
public void setGender(String gender) {
    this.gender = gender;
}
/**
 * @return the role
 */
public String getRole() {
    return role;
}
/**
 * @param  the role to set
 */
public void setRole(String role) {
    this.role = role;
}
/**
 * @return the health
 */
public int getHealth() {
    return health;
}
/**
 * @param  the health to set
 */
public void setHealth(int health) {
    this.health = health;
}
/**
 * @return the state
 */
public String getState() {
    return state;
}
/**
 * @param  the state to set
 */
public void setState(String state) {
    this.state = state;
}
/**
 * @return the sprite
 */
public File getSprite() {
    return sprite;
}
/**
 * @param  the sprite to set
 */
public void setSprite(File sprite) {
    this.sprite = sprite;
}
/* (non-Javadoc)
 * @see java.lang.Object#toString()
 */
@Override
public String toString() {
    return "myPlayer [name=" + name + ", gender=" + gender + ", role="
            + role + ", health=" + health + ", state=" + state
            + ", sprite=" + sprite + "]";
}
}

public class myPlayerTester {

/**
 * @param args
 */
public static void main(String[] args) {
    // TODO Auto-generated method stub      
    myPlayer myPlayer1, myPlayer2, myPlayer3, myPlayer4;

    myPlayer1 = new myPlayer();

    System.out.println(myPlayer1);

    myPlayer2 = new myPlayer("SteveO", "Male", "demon", 99, "live", null );

    System.out.println(myPlayer2);

    myPlayer3 = new myPlayer("Gandolf", "Female", "demon", 93, "live", null);

    System.out.println(myPlayer3);

    myPlayer4 = new myPlayer("Roger", "Female", "demon", 69, "inactive", null);

    System.out.println(myPlayer4);




}

}

1 个答案:

答案 0 :(得分:1)

您可以使用File

中的简单方法
        File file = new File("folder/folder/folder/file.gif");
        //absolute path
        file.getAbsolutePath();
        //path
        file.getPath();

在测试类上使用它,并将对象放在myPlayer2构造函数中。

在to toString()方法中,在变量

  

子画面

使用

  

sprite.getPath()或sprite.getAbsolutePath();

以字符串格式显示文件的显示路径。

如果您想使用流来读取字节,请尝试使用

InputStream in = new FileInputStream(new File("folder/folder/folder/file.gif"));