gpt4 book ai didi

java - 从 Jarfile 加载图像

转载 作者:行者123 更新时间:2023-11-30 06:21:44 31 4
gpt4 key购买 nike

好吧,这是我的问题,也是我要问你的问题......

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

然后它去: cards folder

然后在每个文件夹中,我都有如下所示的内容: what the other folders look like also

现在上面的提醒是解压缩的 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);
}

所以我在构造函数中调用将所有图像加载到 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);
}

我不断在 tempIcon = resizeImage(new ImageIcon(imageURL),70,70,false);

上收到空指针异常

谁能告诉我为什么这不能正确加载图像?提醒我将它们预加载到内存中。我的问题不是如何让它变得更好我只是简单地问我在类加载器和类似的东西上做错了什么......

我对 Java 有点陌生,我知道这段代码中的某些部分对你来说会很糟糕,但请再次重申,我不是在问如何使这段代码“专业”,我只是在寻找为什么它给我 null 的解释。

谢谢!

附言如果您需要更多信息图片或 w.e.我会尽快把它放上去

最佳答案

您的问题是 cldr.getResource(tempString); 不返回对象而是返回 null。然后,tempString 出了点问题。类加载器找不到此文件,因此返回 null。

更改 tempString = "/cards/"+cardFolders[i]+"/"+tempString;

tempString = "cards/"+cardFolders[i]+"/"+tempString;

关于java - 从 Jarfile 加载图像,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20060337/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com