如何将图片插入哈希图中

时间:2019-12-01 02:41:42

标签: java image

这是我的代码,我正在使用GUI和JavaFX创建一个堡垒数据库,例如,我想添加一个图像Rockets.png,其中每个项目都提供信息,我该怎么做呢?

objList.put(“ Rockets”,new Ammo(“ Rockets”,2,12,“火箭专用于爆炸性武器。”                 +“ \ nMax堆栈在LTM中可能会有所不同,包括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;
    }
    }

1 个答案:

答案 0 :(得分:0)

正如@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,然后在Image中显示ImageView。因此,在您的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 图像类,而不是javax或awt之一,或其他任何一种,这些类在您的应用程序中不起作用。你明白了。