所以我的朋友策劃了這個枚舉,我想縮短它。我想在字符串中縮短每個文件路徑並將其折回String pokemonPath = "Pictures/Menu/MainMenu/";
,然後像pokePath +" Cosmet.png
一樣,但我不確定如何實現它。此外,如果任何人有任何關於如何縮短它更多,我也很想聽到它。除了它自己的類文件,所以將常量放在頂部將無法正常工作,它會給我一個錯誤。修改圖像枚舉
我並不真正使用枚舉,所以我不知道如何有效地使用它們。
這是他的代碼
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;
}
這似乎只提供適量的文件路徑的獨立性...... – Thihara
@Thihara我不太確定你的意思? – Exikle
我的意思是,這種粒度更適合於存儲圖像路徑的目的。因爲您可以將每個圖像移動到您想要的任何路徑。泛化只會限制你的選擇......並迫使你對圖像進行分類。另外如果你有一個圖像分爲兩類?這是要麼複製不正確的分類! – Thihara