-1

所以我的朋友创建了这个枚举,我想缩短它。我想缩短字符串中的每个文件路径并引用它String pokemonPath = "Pictures/Menu/MainMenu/";,然后就像pokePath +" Cosmet.png但我不确定如何实现它。另外,如果有人对如何缩短它有任何想法,我也很想听听。ALos 它在它自己的类文件中,因此将常量放在顶部将无法正常工作,它给我一个错误。

我并没有真正使用枚举,所以我不知道如何有效地使用它们。

这是他的代码

import java.awt.Image;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

public enum Images {

// Menu stuff. I don't know how to organize it. You do it.
WelcomeBG("Pictures/Menu/MainMenu/WelcomeBG.png"), PokemonTitleMenuBG(
        "Pictures/Menu/MainMenu/PokemonTitleMenuBG.png"), PokemonMenuAll(
        "Pictures/Menu/PokemonMenu/PokemonMenuAll.png"), PokemonMenuContinue(
        "Pictures/Menu/PokemonMenu/PokemonMenuContinue.png"), PokemonMenuContinueSelected(
        "Pictures/Menu/PokemonMenu/PokemonMenuContinueSelected.png"), PokemonMenuAllSelected(
        "Pictures/Menu/PokemonMenu/PokemonMenuAllSelected.png"), TitleScreen(
        "Pictures/Menu/MainMenu/TitleScreen.png"),

Professor("Pictures/Menu/NPC/Professor.png"), ProfessorFlip(
        "Pictures/Menu/NPC/ProfessorFlip.png"),

FriendGirl("Pictures/Menu/PlayerPictures/FriendGirl.png"), FriendBoy(
        "Pictures/Menu/PlayerPictures/FriendBoy.png"), PlayerBoy(
        "Pictures/Menu/PlayerPictures/PlayerBoy.png"), PlayerGirl(
        "Pictures/Menu/PlayerPictures/PlayerGirl.png"), Male(
        "Pictures/Menu/PlayerPictures/Male.png"), Female(
        "Pictures/Menu/PlayerPictures/Female.png"), Credits(
        "Pictures/Menu/MainMenu/Credits.png"), Gender(
        "Pictures/Menu/PlayerPictures/Gender.png"),

// Player sprites

PlayerDown("Pictures/Sprites/Player/"), PlayerDown1(
        "Pictures/Sprites/Player/Down1.png"), PlayerDown2(
        "Pictures/Sprites/Player/Down2.png"), PlayerLeft(
        "Pictures/Sprites/Player/Left.png"), PlayerLeft1(
        "Pictures/Sprites/Player/Left1.png"), PlayerLeft2(
        "Pictures/Sprites/Player/Left2.png"), PlayerRight(
        "Pictures/Sprites/Player/Right.png"), PlayerRight1(
        "Pictures/Sprites/Player/Right1.png"), PlayerRight2(
        "Pictures/Sprites/Player/Right2.png"), PlayerUp(
        "Pictures/Sprites/Player/Up.png"), PlayerUp1(
        "Pictures/Sprites/Player/Up1.png"), PlayerUp2(
        "Pictures/Sprites/Player/Up2.png"),

// NPC Sprites
DadDown("Pictures/Sprites/NPC/Dad/DadDown.png"), DadRight(
        "Pictures/Sprites/NPC/Dad/DadRight.png"), BoyLeft(
        "Pictures/Sprites/NPC/Boy/BoyLeft.png"), BoyRight(
        "Pictures/Sprites/NPC/Boy/BoyRight.png"), BoyRight1(
        "Pictures/Sprites/NPC/Boy/BoyRight1.png"), BoyRight2(
        "Pictures/Sprites/NPC/Boy/BoyRight2.png"), BoyUp(
        "Pictures/Sprites/NPC/Boy/BoyUp.png"), BoyUp1(
        "Pictures/Sprites/NPC/Boy/BoyUp1.png"), BoyUp2(
        "Pictures/Sprites/NPC/Boy/BoyUp2.png"), DadUp(
        "Pictures/Sprites/NPC/Dad/DadUp.png"), DadLeft(
        "Pictures/Sprites/NPC/Dad/DadLeft.png"),

GirlDown("Pictures/Sprites/NPC/Girl/GirlDown.png"), GirlRight(
        "Pictures/Sprites/NPC/Girl/GirlRight.png"), GirlLeft(
        "Pictures/Sprites/NPC/Girl/GirlLeft.png"), GateDown(
        "Pictures/Sprites/NPC/Gate/GateDown.png"), GateLeft(
        "Pictures/Sprites/NPC/Gate/GateLeft.png"), GateLeft1(
        "Pictures/Sprites/NPC/Gate/GateLeft1.png"), GateLeft2(
        "Pictures/Sprites/NPC/Gate/GateLeft2.png"), GateRight(
        "Pictures/Sprites/NPC/Gate/GateRight.png"), GateRight1(
        "Pictures/Sprites/NPC/Gate/GateRight1.png"), GateRight2(
        "Pictures/Sprites/NPC/Gate/GateRight2.png"), BoyDown(
        "Pictures/Sprites/NPC/Boy/BoyDown.png"),

// NPC Effects
Exclamation("Pictures/Sprites/Misc/Exclamation.png"),

// Maps (the background, base thing)
Exitium("Pictures/Maps/Exitium.png"), Route1("Pictures/Maps/Route1.png"), House1Top(
        "Pictures/Maps/House1Top.png"), House1Bot(
        "Pictures/Maps/House1Bot.png"), MiracleForest1(
        "Pictures/Maps/MiracleForest1.png"), MiracleForest2(
        "Pictures/Maps/MiracleForest2.png"),

// Map decorations (the stuff you code on top of the map!)
House1Overlay("Pictures/Sprites/Structures/House1Overlay.png"), House2Overlay(
        "Pictures/Sprites/Structures/House2Overlay.png"), House3Overlay(
        "Pictures/Sprites/Structures/House3Overlay.png"), House4Overlay(
        "Pictures/Sprites/Structures/House4Overlay.png"), Lamppost(
        "Pictures/Sprites/Structures/Lamppost.png"), MailBox(
        "Pictures/Sprites/Structures/MailBox.png"), MailBox2(
        "Pictures/Sprites/Structures/MailBox2.png"), ColumnTrees(
        "Pictures/Sprites/Structures/ColumnTrees.png"), Sign(
        "Pictures/Sprites/Misc/Sign.png"), GreenColumnTrees(
        "Pictures/Sprites/Structures/GreenColumnTrees.png"), GreenColumnTrees2(
        "Pictures/Sprites/Structures/GreenColumnTrees2.png"), GreenColumnTrees3(
        "Pictures/Sprites/Structures/GreenColumnTrees3.png"), ForestEntrance(
        "Pictures/Sprites/Structures/ForestEntrance.png"), GreenTreeOverlay(
        "Pictures/Sprites/Structures/GreenTreeOverlay.png"), LeftEntrance(
        "Pictures/Sprites/Structures/LeftEntrance.png"), RightEntrance(
        "Pictures/Sprites/Structures/RightEntrance.png"),

// GUI Things???
Arrow("Pictures/Sprites/Misc/Arrow.png"), Hand(
        "Pictures/Sprites/Misc/Hand.png"), DialogBox(
        "Pictures/Sprites/Misc/DialogBox.png"), DialogBox2(
        "Pictures/Sprites/Misc/DialogBox2.png"), DialogBox3(
        "Pictures/Sprites/Misc/DialogBox3.png"), DialogBox4(
        "Pictures/Sprites/Misc/DialogBox4.png"),

// I dont even know
MainBattleBack("Pictures/Sprites/Battle/BattleStart/MainBattleBack.png"), Vs2(
        "Pictures/Sprites/Battle/BattleStart/Vs2.png"), MainHead(
        "Pictures/Sprites/Battle/BattleStart/MainHead.png"),

// Battle Menu GUI things??
GreenBackground("Pictures/Sprites/Battle/BattleMenu/GreenBackground.png"), BattleMenu2(
        "Pictures/Sprites/Battle/BattleMenu/BattleMenu2.png"), HpBarAlly(
        "Pictures/Sprites/Battle/BattleMenu/HpBarAlly.png"), HpBarEnemy(
        "Pictures/Sprites/Battle/BattleMenu/HpBarEnemy.png"), MoveMenu2(
        "Pictures/Sprites/Battle/BattleMenu/MoveMenu2.png"),

// Natalie
Natalie("Pictures/Menu/NPC/Natalie.png"), NatalieHead(
        "Pictures/Sprites/Battle/BattleStart/NatalieHead.png"), NatalieBattleBack(
        "Pictures/Sprites/Battle/BattleStart/NatalieBattleBack.png"),

// Flashing
PlayerFlashNormal(
        "Pictures/Sprites/Battle/BattleStart/Player_flash_normal.gif"), OpponentFlashNormal(
        "Pictures/Sprites/Battle/BattleStart/Opponent_flash_normal.gif"),

// Throwing Animation
MainThrow("Pictures/Sprites/Player/MainThrow.png"), MainThrow1(
        "Pictures/Sprites/Player/MainThrow1.png"), MainThrow2(
        "Pictures/Sprites/Player/MainThrow2.png"),

// Attack Effects
Tackle("Pictures/Sprites/Battle/Attacks/Tackle.png"), BlackBack(
        "Pictures/Sprites/Battle/Attacks/BlackBack.png"), Growl(
        "Pictures/Sprites/Battle/Attacks/Growl.png"), Leer(
        "Pictures/Sprites/Battle/Attacks/Leer.png"),

// POKEMONS
Cosmet("Pictures/Sprites/Pokemon/Cosmet.png"), CosmetB(
        "Pictures/Sprites/PokemonB/CosmetB.png"), CosmetStart(
        "Pictures/Menu/Starters/CosmetStart.png"),

Tykepol("Pictures/Sprites/Pokemon/Tykepol.png"), TykepolB(
        "Pictures/Sprites/PokemonB/TykepolB.png"), TykepolStart(
        "Pictures/Menu/Starters/TykepolStart.png"),

Embite("Pictures/Sprites/Pokemon/Embite.png"), EmbiteB(
        "Pictures/Sprites/PokemonB/EmbiteB.png"), EmbiteStart(
        "Pictures/Menu/Starters/EmbiteStart.png"),

// Balls
Pokeball("Pictures/Sprites/Misc/Pokeball.png"), Pokeball4(
        "Pictures/Sprites/Misc/Pokeball4.gif"),

// Outlines
Outline1("Pictures/Sprites/Battle/BattleMenu/Outline1.png"), Outline2(
        "Pictures/Sprites/Battle/BattleMenu/Outline2.png"), Outline3(
        "Pictures/Sprites/Battle/BattleMenu/Outline3.png"), Outline4(
        "Pictures/Sprites/Battle/BattleMenu/Outline4.png");

private String filePath;
private Image image;
private boolean imageLoaded;

private Images(String imagePath) {
    filePath = imagePath;
    imageLoaded = false;

}

public Image getImage() {
    if (!imageLoaded) {
        loadImage();
    }
    return image;

}

public void loadImage() {
    try {
        image = ImageIO.read(new File(filePath));
    } catch (IOException e) {
        System.err.println("Failed to load image!");
        e.printStackTrace();
    }
    imageLoaded = true;
}
4

1 回答 1

1

你如何做将取决于你想要实现什么。

如果您想防止在路径发生变化时需要在多个地方替换路径,那么使用String常量是一个合理的想法。

就像是...

public interface PathConstants {

    protected static final String SPRITES_PATH = "Pictures/Sprites";
    protected static final String SPRITES_MISC_PATH = SPRITES_PATH + "/Misc";
    protected static final String POKEMON_SPRITES_PATH = SPRITES_PATH + "/Pokemon";
    protected static final String POKEMONB_SPRITES_PATH = SPRITES_PATH + "/PokemonB";
    protected static final String STARTERS_MENU_PATH = "Pictures/Menu/Starters";
    protected static final String SPRITES_BATTLE_PATH = SPRITES_PATH + "/Battle";
    protected static final String MENU_BATTLE_PATH = SPRITES_BATTLE_PATH + "/BattleMenu";

}

然后你应该能够做类似的事情......

public enum Images {
    Cosmet(PathConstants.SPRITES_PATH + "/Cosmet.png"), CosmetB(
           PathConstants.POKEMONB_SPRITES_PATH + "/CosmetB.png"), CosmetStart(
           PathConstants.STARTERS_MENU_PATH + "/CosmetStart.png"),

    Tykepol(PathConstants.POKEMON_SPRITES_PATH + "/Tykepol.png"), TykepolB(
            PathConstants.POKEMONB_SPRITES_PATH + "/TykepolB.png"), TykepolStart(
            PathConstants.STARTERS_MENU_PATH + "/TykepolStart.png"),

    Embite(PathConstants.POKEMON_SPRITES_PATH + "/Embite.png"), EmbiteB(
           PathConstants.POKEMONB_SPRITES_PATH + "/EmbiteB.png"), EmbiteStart(
           PathConstants.STARTERS_MENU_PATH + "/EmbiteStart.png"),

    // Balls
    Pokeball(PathConstants.SPRITES_MISC_PATH + "/Pokeball.png"), Pokeball4(
             PathConstants.SPRITES_MISC_PATH + "/Pokeball4.gif"),

    // Outlines
    Outline1(PathConstants.MENU_BATTLE_PATH + "/Outline1.png"), Outline2(
             PathConstants.MENU_BATTLE_PATH + "/Outline2.png"), Outline3(
             PathConstants.MENU_BATTLE_PATH + "/Outline3.png"), Outline4(
             PathConstants.MENU_BATTLE_PATH + "/Outline4.png");

例如。

这意味着如果SPRITES_PATH发生更改,您只需更改一处并重新编译。

于 2013-07-18T04:34:43.747 回答