java - 如何将图像插入 HashMap 中

标签 java image

这是我的代码,我正在使用 GUI 和 JavaFX 创建一个堡垒之夜数据库,我想添加一个图像 Rockets.png,其中包含每个项目给出的信息,我该怎么做,我会把它放在这里

objList.put("火箭",new Ammo("火箭",2,12,"火箭专门用于爆炸性武器。" +“\nLTM 中的最大堆栈可能会有所不同,包括 Team Rumble。” + "\n所有弹药类型的堆叠数量是正常的两倍。"));

    import java.util.HashMap;

    public class DbFill {
    public static HashMap<String,String> loadDisamb() {
    HashMap<String,String> disamb = new HashMap<String,String>();
    disamb.put("wood","Wood");
    disamb.put("stone","Stone");
    disamb.put("metal","Metal");
    //MATERIALS END

    //AMMO START
    disamb.put("light bullets","Light Ammo");
    disamb.put("light ammo","Light Ammo");
    disamb.put("small bullets","Light Ammo");
    disamb.put("small ammo","Light Ammo");
    disamb.put("medium bullets","Medium Ammo");
    disamb.put("medium ammo","Medium Ammo");
    disamb.put("heavy ammo","Heavy Ammo");
    disamb.put("shells","Slugs n Shells");
    disamb.put("shotgun shells","Slugs n Shells");
    disamb.put("shotgun ammo","Slugs n Shells");
    disamb.put("slugs n shells","Slugs n Shells");
    disamb.put("slugs and shells","Slugs n Shells");
    disamb.put("rocket ammo","Rockets");
    disamb.put("rockets","Rockets");
    disamb.put("rocket","Rockets");
    //AMMO END

    //TRAPS START
    disamb.put("trap","Damage Trap");
    disamb.put("damage trap","Damage Trap");
    disamb.put("spike trap","Damage Trap");
    //TRAPS END

    //HEALING START
    /*
     * Bandages
     * Medkit
     * Small Shield Potion (Mini Shield/Minis)
     * Shield Potion (Shield Pot/50 Pot)
     * Flopper
     * Small Fry
     * Slurpfish
     * etc.
     */
    //HEALING END

    //THROWN START
    /*
     * Rusty Can
     * Grenade
     * Mythic Goldfish
     * etc.
     */
    //THROWN END

    //OTHER START (i don't even know what goes here, just in case)
    //OTHER END

    //FORAGED START
    //FORAGED HEALS START
    disamb.put("apple","Apple");
    disamb.put("mushroom","Mushroom");
    disamb.put("shieldshroom","Mushroom");
    disamb.put("slurpshroom","Slurpshroom");
    disamb.put("slurp mushroom","Slurpshroom");
    //FORAGED HEALS END
    //FORAGED END

    //WEAPONS START
    //ARs START
    disamb.put("common assault rifle","Common Assault Rifle");
    disamb.put("gray Assault Rifle","Common Assault Rifle");
    disamb.put("grey Assault Rifle","Common Assault Rifle");
    disamb.put("common ar","Common Assault Rifle");
    disamb.put("gray ar","Common Assault Rifle");
    disamb.put("grey ar","Common Assault Rifle");
    disamb.put("uncommon assault rifle","Uncommon Assault Rifle");
    disamb.put("green assault rifle","Uncommon Assault Rifle");
    disamb.put("uncommon ar","Uncommon Assault Rifle");
    disamb.put("green ar","Uncommon Assault Rifle");
    disamb.put("rare assault rifle","Rare Assault Rifle");
    disamb.put("blue assault rifle","Rare Assault Rifle");
    disamb.put("rare ar","Rare Assault Rifle");
    disamb.put("blue ar","Rare Assault Rifle");
    disamb.put("epic assault rifle","Epic Assault Rifle");
    disamb.put("purple essault rifle","Epic Assault Rifle");
    disamb.put("epic ar","Epic Assault Rifle");
    disamb.put("purple ar","Epic Assault Rifle");
    disamb.put("purple scar","Epic Assault Rifle");
    disamb.put("legendary assault rifle","Legendary Assault Rifle");
    disamb.put("gold assault rifle","Legendary Assault Rifle");
    disamb.put("legendary ar","Legendary Assault Rifle");
    disamb.put("gold ar","Legendary Assault Rifle");
    disamb.put("gold scar","Legendary Assault Rifle");
    //ARs END

    //SMGs START
    //SMGs END

    //SHOTGUNS START
    //SHOTGUNS END

    //SNIPERS START
    //SNIPERS END

    //BOOM BOW
    //BOOM BOW
    //WEAPONS END

    return disamb;
    }

    public static HashMap<String,FNObj> loadObjList() {
    HashMap<String,FNObj> objList = new HashMap<String,FNObj>();
    objList.put("Wood", new Material("Wood"));
    objList.put("Stone", new Material("Stone"));
    objList.put("Metal", new Material("Metal"));
    //MATERIALS END

    //AMMO START
    objList.put("Light Ammo",new Ammo("Light Bullets",18,"Light bullets (also         known as \"small bullets\") are used primarily by SMGs."
            + "\nIf found with a Minigun, a stack of 90 will be found instead of     the normal amount."
            + "\nAll ammo types can be found in stacks of twice as much as normal."));
    objList.put("Medium Ammo",new Ammo("Medium Bullets",10,"Medium bullets are used primarily by Assault Rifles."
            + "\nAll ammo types can be found in stacks of twice as much as normal."));
    objList.put("Heavy Ammo",new Ammo("Heavy Bullets",6,"Heavy bullets are used primarily by Sniper Rifles."
            + "\nAll ammo types can be found in stacks of twice as much as normal."));
    objList.put("Slugs n Shells",new Ammo("Slugs 'n' Shells",4,"Slugs 'n' Shells are used by Shotguns."
            + "\nAll ammo types can be found in stacks of twice as much as normal."));
    objList.put("Rockets",new Ammo("Rockets",2,12,"Rockets are used exclusively by explosive weapons."
            + "\nMax Stack may vary in LTMs, including Team Rumble."
            + "\nAll ammo types can be found in stacks of twice as much as normal."));
    //AMMO END

    //TRAPS START
    objList.put("Damage Trap", new Trap("Damage Trap",Rarity.Uncommon,1,"The Damage Trap (also known as a \"Spike Trap\") can be placed on any wall, ceiling, or floor, and deals 150 damage in the tile in front of it 1 second after being triggered."));
    //TRAPS END

    //HEALING START
    /*
     * Bandages
     * Medkit
     * Small Shield Potion (Mini Shield/Minis)
     * Shield Potion (Shield Pot/50 Pot)
     * Flopper
     * Small Fry
     * Slurpfish
     * etc.
     */
    //HEALING END

    //THROWN START
    /*
     * Rusty Can
     * Grenade
     * Mythic Goldfish
     * etc.
     */
    //THROWN END

    //OTHER START (i don't even know what goes here, just in case)
    //OTHER END

    //FORAGED START
    //FORAGED HEALS START
    objList.put("Apple", new ForagedHeal("Apple","Found on the ground, especially near trees in fields.",1,5,HealType.Health));
    objList.put("Mushroom", new ForagedHeal("Mushroom","Found on the ground, especially near trees in forests.",1,5,HealType.Shields));
    objList.put("Slurpshroom", new ForagedHeal("Slurpshroom","Found in Slurpy Swamp.",1,5,HealType.Adaptive));
    //FORAGED HEALS END
    //FORAGED END

    //WEAPONS START
    //ARs START
    objList.put("Legendary Assault Rifle", new Weapon("Assault Rifle",Rarity.Legendary,"",true, AmmoType.Medium, 0, 0, 0, 0, 0, 0)); // these numbers are wrong
    //ARs END

    //SMGs START
    //SMGs END

    //SHOTGUNS START
    //SHOTGUNS END

    //SNIPERS START
    //SNIPERS END

    //BOOM BOW
    //BOOM BOW
    //WEAPONS END
    return objList;
    }
    }

最佳答案

正如 @WJS 在上面的评论中所说,您可以在 HashMap 中存储您想要的任何数据类型。这是一个通用类型。

// You can store whatever you want in a HashMap, even Giraffes.
HashMap<String, Giraffe> zoo = new HashMap<String, Giraffe>(); // make a zoo for giraffes
zoo.put("Timmy", new Giraffe()); // add a giraffe
zoo.get("Timmy"); // returns Timmy the giraffe
<小时/>

要将图像添加到 javafx 应用程序,您可以创建一个新的 Image,然后在 ImageView 中显示该 Image。因此,在您的 Ammo 类中,创建一个新的实例字段来存储图像文件的路径。

每个 Ammo 对象都有自己的图像文件路径后,您可以执行以下操作:

if (null != myAmmoInstance.getImageFilepath()) { // avoid NullPointerException if there
                                                 // is ever an Ammo without an image
    Image image = new Image(myAmmoInstance.getImageFilepath()); // load the image
    ImageView imageView = new ImageView(); // make an ImageView
    imageView.setImage(image); // put the image in the ImageView to display it in the GUI
}

请注意 - 那里有十亿个图像类别。请务必使用 JavaFX Image 类,而不是 javax 类、awt 类或任何其他类,这些类在您的应用程序中不起作用。你明白了。

关于java - 如何将图像插入 HashMap 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59122112/

相关文章:

java - 是否可以合并同等大小的mp3格式文件,然后在Java中检索、修改每个单元文件并添加新的文件?

java - 对某个java命令的解释

html - 如何使图像超链接具有背景图像?

ios - iOS(Xamarin)中带有图像和文本的UILabel

java - 检测连接的蓝牙媒体设备

java - 如何生成 LogN 唯一数字的排序列表,其中 N 是给定的数组大小?

html - 悬停时图像宽度为 'malfunctioning'(webkit),如何解决?

javascript - JS : How to change the image reference ending value

java - 将 Thrift 与 Java 结合使用,org.apache.thrift.TApplicationException 未知结果

JavaScript:如何强制 Image() 不使用浏览器缓存?