Java 从JAR文件加载图像

Java 从JAR文件加载图像,java,image,class,jar,classloader,Java,Image,Class,Jar,Classloader,好的,这是我的问题,我想问你 我有一个游戏,它需要从jar文件加载图像(所有图像都像这样打包到jar文件中): 我首先要解压缩jar文件: 然后是: 然后在每个文件夹中,我都有如下内容: 现在,上面的提醒是Jarfile GameClient.jar,您可以看到卡在其中的位置 下面是我的代码,用于尝试将这些图像中的每一个加载到内存中 private void addCardsAndChangeSize() throws IOException { String tempSt

好的,这是我的问题,我想问你

我有一个游戏,它需要从jar文件加载图像(所有图像都像这样打包到jar文件中): 我首先要解压缩jar文件:

然后是:

然后在每个文件夹中,我都有如下内容:

现在,上面的提醒是Jarfile GameClient.jar,您可以看到卡在其中的位置

下面是我的代码,用于尝试将这些图像中的每一个加载到内存中

    private void addCardsAndChangeSize() throws IOException
{
    String tempString;
    ImageIcon tempIcon;
    allCards.clear();
    ClassLoader cldr = this.getClass().getClassLoader();
    URL imageURL;

    for(int i = 0; i < all.length; i++)
    {
        for(int x = 0; x < clubs.length; x++)
        {
            tempString = all[i][x];
            tempString = "/cards/"+cardFolders[i]+"/"+tempString;
            imageURL = cldr.getResource(tempString);
            tempIcon = resizeImage(new ImageIcon(imageURL),70,70,false);
            tempString = all[i][x];
            tempIcon.setDescription(tempString);
            allCards.add(tempIcon);
        }
    }
    backCard = resizeImage(new ImageIcon(cldr.getResource(back)),70,70,false);
}
private void addCardsAndChangeSize()引发IOException
{
字符串tempString;
图像图标;
所有卡片。清除();
ClassLoader cldr=this.getClass().getClassLoader();
URL-imageURL;
for(int i=0;i
所以我在构造函数中调用它,将所有图像加载到ArrayList中,如果你想知道我在做什么,这里是整个类

package global;

import java.awt.*;
import java.io.IOException;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Random;

import javax.imageio.ImageIO;
import javax.swing.*;

public class Cards
{
private ImageIcon backCard = new ImageIcon("cards/backCard.jpg");
private String back = "/cards/backCard.jpg";
private String[] cardFolders = {
        "clubs","diamonds","hearts","spades"
};
private String[] clubs = {
                              "aceClubs.jpg","eightClubs.jpg","fiveClubs.jpg","fourClubs.jpg","jackClubs.jpg",
        "kingClubs.jpg","nineClubs.jpg","queenClubs.jpg","sevenClubs.jpg","sixClubs.jpg",
        "tenClubs.jpg","threeClubs.jpg","twoClubs.jpg"
};
private String[] diamonds = {
        "aceDia.jpg","eightDia.jpg","fiveDia.jpg","fourDia.jpg","jackDia.jpg","kingDia.jpg",
        "nineDia.jpg","queenDia.jpg","sevenDia.jpg","sixDia.jpg","tenDia.jpg","threeDia.jpg",
        "twoDia.jpg"
};
private String[] hearts = {
        "aceHearts.jpg","eightHearts.jpg","fiveHearts.jpg","fourHearts.jpg","jackHearts.jpg",
        "kingHearts.jpg","nineHearts.jpg","queenHearts.jpg","sevenHearts.jpg","sixHearts.jpg",
        "tenHearts.jpg","threeHearts.jpg","twoHearts.jpg"
};
private String[] spades = {
        "aceSpades.jpg","eightSpades.jpg","fiveSpades.jpg","fourSpades.jpg","jackSpades.jpg",
        "kingSpades.jpg","nineSpades.jpg","queenSpades.jpg","sevenSpades.jpg","sixSpades.jpg",
        "tenSpades.jpg","threeSpades.jpg","twoSpades.jpg"
};
private String[][] all = {
        clubs,diamonds,hearts,spades
};
private ArrayList<ImageIcon> allCards = new ArrayList<ImageIcon>();

public Cards()
{
    try
    {
        addCardsAndChangeSize();
        shuffle();
    }
    catch(Exception e)
    {e.printStackTrace();}
}

/**
 * @param x Cards name with extension
 * @return Face Value of Card
 */
public static int getFaceValue(String x)
{
    int face = 0;
    switch(x)
    {
        case "aceClubs.jpg":
        case "aceDia.jpg":
        case "aceHearts.jpg":
        case "aceSpades.jpg":
            face = 1;
            break;
        case "eightClubs.jpg":
        case "eightDia.jpg":
        case "eightHearts.jpg":
        case "eightSpades.jpg":
            face = 8;
            break;
        case "fiveClubs.jpg":
        case "fiveDia.jpg":
        case "fiveHearts.jpg":
        case "fiveSpades.jpg":
            face = 5;
            break;
        case "fourClubs.jpg":
        case "fourDia.jpg":
        case "fourHearts.jpg":
        case "fourSpades.jpg":
            face = 4;
            break;
        case "jackClubs.jpg":
        case "jackDia.jpg":
        case "jackHearts.jpg":
        case "jackSpades.jpg":
        case "kingClubs.jpg":
        case "kingDia.jpg":
        case "kingHearts.jpg":
        case "kingSpades.jpg":
        case "queenClubs.jpg":
        case "queenDia.jpg":
        case "queenHearts.jpg":
        case "queenSpades.jpg":
        case "tenClubs.jpg":
        case "tenDia.jpg":
        case "tenHearts.jpg":
        case "tenSpades.jpg":
            face = 10;
            break;
        case "twoClubs.jpg":
        case "twoDia.jpg":
        case "twoHearts.jpg":
        case "twoSpades.jpg":
            face = 2;
            break;
        case "threeClubs.jpg":
        case "threeDia.jpg":
        case "threeHearts.jpg":
        case "threeSpades.jpg":
            face = 3;
            break;
        case "sixClubs.jpg":
        case "sixDia.jpg":
        case "sixHearts.jpg":
        case "sixSpades.jpg":
            face = 6;
            break;
        case "sevenClubs.jpg":
        case "sevenDia.jpg":
        case "sevenHearts.jpg":
        case "sevenSpades.jpg":
            face = 7;
            break;
        case "nineClubs.jpg":
        case "nineDia.jpg":
        case "nineHearts.jpg":
        case "nineSpades.jpg":
            face = 9;
            break;
    }
    return face;
}

//shuffles all the cards in the deck
private void shuffle()
{
    long seed = System.nanoTime();
    Collections.shuffle(allCards, new Random(seed));
}

/**
 * Chooses a card at random from the deck. Also removes that card when chosen
 * @return randomly chosen card
 */
public ImageIcon getRandomCard()
{
    int index = ((int)Math.random() * allCards.size());
    return allCards.remove(index);
}

/**
 * @return Image of the back of a card
 */
public ImageIcon getBackCard()
{return backCard;}

public static ImageIcon parseImage(String x)
{
    if(x.contains("Dia"))
        return new ImageIcon("cards/diamonds/"+x);
    else
        if(x.contains("Clubs"))
            return new ImageIcon("cards/clubs/"+x);
        else
            if(x.contains("Hearts"))
                return new ImageIcon("cards/hearts/"+x);
            else
                if(x.contains("Spades"))
                    return new ImageIcon("cards/spades/"+x);
    return null;
}

//adds all the cards and adds a description to them loaded into memory
private void addCardsAndChangeSize() throws IOException
{
    String tempString;
    ImageIcon tempIcon;
    allCards.clear();
    ClassLoader cldr = this.getClass().getClassLoader();
    URL imageURL;

    for(int i = 0; i < all.length; i++)
    {
        for(int x = 0; x < clubs.length; x++)
        {
            tempString = all[i][x];
            tempString = "/cards/"+cardFolders[i]+"/"+tempString;
            imageURL = cldr.getResource(tempString);
            tempIcon = resizeImage(new ImageIcon(imageURL),70,70,false);
            tempString = all[i][x];
            tempIcon.setDescription(tempString);
            allCards.add(tempIcon);
        }
    }
    backCard = resizeImage(new ImageIcon(cldr.getResource(back)),70,70,false);
}

//resizes images
public ImageIcon resizeImage(ImageIcon imageIcon, int width, int height, boolean max) 
{
    Image image = imageIcon.getImage();
    Image newimg = image.getScaledInstance(-1, height, java.awt.Image.SCALE_SMOOTH);
    int width1 = newimg.getWidth(null);
    if ((max && width1 > width) || (!max && width1 < width))
        newimg = image.getScaledInstance(width, -1, java.awt.Image.SCALE_SMOOTH);
    return new ImageIcon(newimg);
}
packageglobal;
导入java.awt.*;
导入java.io.IOException;
导入java.net.URL;
导入java.util.ArrayList;
导入java.util.Collections;
导入java.util.Random;
导入javax.imageio.imageio;
导入javax.swing.*;
公共课卡
{
private ImageIcon backCard=新的ImageIcon(“cards/backCard.jpg”);
私有字符串back=“/cards/backCard.jpg”;
私有字符串[]cardFolders={
“梅花”、“钻石”、“红心”、“黑桃”
};
私人字符串[]俱乐部={
“aceClubs.jpg”、“eightClubs.jpg”、“fiveClubs.jpg”、“fourClubs.jpg”、“jackClubs.jpg”,
“kingClubs.jpg”、“nineClubs.jpg”、“queenClubs.jpg”、“sevenClubs.jpg”、“sixClubs.jpg”,
“tenClubs.jpg”、“threeClubs.jpg”、“twooclubs.jpg”
};
私有字符串[]菱形={
“aceDia.jpg”、“eightDia.jpg”、“fiveDia.jpg”、“fourDia.jpg”、“jackDia.jpg”、“kingDia.jpg”,
“nineDia.jpg”、“queenDia.jpg”、“sevenDia.jpg”、“sixDia.jpg”、“tenDia.jpg”、“threeDia.jpg”,
“twoDia.jpg”
};
私有字符串[]心形={
“aceharts.jpg”、“eightheart.jpg”、“fiveHearts.jpg”、“fourHearts.jpg”、“jackHearts.jpg”,
“kingharts.jpg”、“nineHearts.jpg”、“queenharts.jpg”、“sevenHearts.jpg”、“sixHearts.jpg”,
“Tenharts.jpg”、“threeHearts.jpg”、“twoHearts.jpg”
};
私有字符串[]黑桃={
“aceSpades.jpg”、“eightSpades.jpg”、“fiveSpades.jpg”、“fourSpades.jpg”、“jackSpades.jpg”,
“kingSpades.jpg”、“nineSpades.jpg”、“queenSpades.jpg”、“sevenSpades.jpg”、“sixSpades.jpg”,
“tenSpades.jpg”、“threeSpades.jpg”、“twoSpades.jpg”
};
专用字符串[][]全部={
梅花、钻石、红桃、黑桃
};
private ArrayList allCards=new ArrayList();
公共卡()
{
尝试
{
添加卡片和更改大小();
洗牌();
}
捕获(例外e)
{e.printStackTrace();}
}
/**
*@param x卡名称及扩展名
*@返回卡的面值
*/
公共静态整型getFaceValue(字符串x)
{
int face=0;
开关(x)
{
案例“aceClubs.jpg”:
案例“aceDia.jpg”:
案例“aceharts.jpg”:
案例“aceSpades.jpg”:
面=1;
打破
案例“eightClubs.jpg”:
案例“eightDia.jpg”:
案例“Eightearts.jpg”:
案例“eightSpades.jpg”:
面=8;
打破
案例“fiveClubs.jpg”:
案例“fiveDia.jpg”:
案例“fiveHearts.jpg”:
案例“fiveSpades.jpg”:
面=5;
打破
案例“fourClubs.jpg”:
案例“fourDia.jpg”:
案例“fourHearts.jpg”:
案例“fourSpades.jpg”:
面=4;
打破
案例“jackClubs.jpg”:
案例“jackDia.jpg”:
案例“jackHearts.jpg”:
案例“jackSpades.jpg”:
案例“kingClubs.jpg”:
案例“kingDia.jpg”:
案例“Kingharts.jpg”:
案例“kingSpades.jpg”:
案例“queenClubs.jpg”:
案例“queenDia.jpg”:
“Queenharts.jpg”案:
案例“queenSpades.jpg”:
案例“tenClubs.jpg”:
案例“tenDia.jpg”:
案例“tenHearts.jpg”:
案例“tenSpades.jpg”:
面=10;
打破
案例“twoClubs.jpg”:
案例“twoDia.jpg”:
案例“twoHearts.jpg”:
案例“twoSpades.jpg”:
面=2;
打破
案例“threeClubs.jpg”:
案例“threeDia.jpg”:
案例“threeHearts.jpg”:
案例“threeSpades.jpg”:
面=3;
打破
案例“sixClubs.jpg”:
案例“sixDia.jpg”:
案例“sixHearts.jpg”:
案例“sixSpades.jpg”:
面=6;
打破
案例“sevenClubs.jpg”:
案例“sevenDia.jpg”:
案例“sevenHearts.jpg”:
案例“sevenSpades.jpg”:
面=7;
打破
案例“nineClubs.jpg”:
案例“nineDia.jpg”:
案例“nineHearts.jpg”:
案例“nineSpades.jpg”:
面=9;
打破
}
返回面;
}
//洗牌组中的所有牌
私有无效洗牌()
{
长种子=System.nanoTime();
收集。洗牌(所有卡片,新随机(种子));
}
/**
*从牌组中随机选择一张牌。选择后也会移除该牌
*@返回随机选择的卡
*/
公共图像图标getRandomCard()
{
int index=((int)Math.random()*allCards.size());
返回所有卡片。移除(索引);
}
/**
*@返回卡片背面的图像
*/
公共图像图标getBackCard()
{返回backCard;}
公共静态图像图标解析图像(字符串x)
{
如果(x.包含(“直径”))
返回新图像图标(“卡/d
private void addCardsAndChangeSize() throws Exception
{
    String tempString;
    ImageIcon tempIcon;
    allCards.clear();
    URL imageURL;

    for(int i = 0; i < all.length; i++)
    {
        for(int x = 0; x < clubs.length; x++)
        {
            tempString = all[i][x];
            tempString = "/cards/"+cardFolders[i]+"/"+tempString;
            imageURL = this.getClass().getResource(tempString);
            System.out.println(tempString);
            System.out.println(imageURL == null);
            tempIcon = resizeImage(new ImageIcon(imageURL),70,70,false);
            tempString = all[i][x];
            tempIcon.setDescription(tempString);
            allCards.add(tempIcon);
        }
    }
    backCard = resizeImage(new ImageIcon(this.getClass().getResource(back)),70,70,false);
}