我和我的伙伴一起制作2D游戏,我通过一些Youtube教程学习了很多基本的游戏开发概念.我正在学习的一件事是精灵(对于那些不知道的人,2D图像渲染到屏幕上)以及如何在我的游戏中使用它们.我一直在使用,ImageIO.read(this.class.getResource(pathToMySprite))
但似乎由于某种原因getResource()
返回null
.
我一直在调整路径,在它前面添加"/",删除"/",放置user.dir属性以查看它是否需要整个路径,我仍然得到相同的错误.
TILE_TEXTURES(System.getProperty("user.dir") + "/textures/tile.png");
//ENTITY_TEXTURES("/textures/entity.png");
private BufferedImage img;
private SpriteSheet(String path) {
System.out.println(System.getProperty("user.dir"));
try {
//TODO: Fix this error, don't know what's wrong.
img = ImageIO.read(SpriteSheet.class.getResource(path)); // error here!!!
} catch (IOException e) {
e.printStackTrace();
}
}
public BufferedImage getImage() {
return img;
}
Run Code Online (Sandbox Code Playgroud)
任何和所有的帮助表示赞赏.我没有评论代码(当我到达我可以坐下来并且对我已经完成的内容感到满意时,我通常这样做)但这是一个非常小的课程,所以我认为你们将能够理解什么是继续进行.
保存图像IS的文件夹位于项目的类路径中.我还包括错误:
Exception in thread "Thread-2" java.lang.ExceptionInInitializerError
at com.brickbattle.client.src.gui.Sprite.<clinit>(Sprite.java:7)
at com.brickbattle.client.src.objs.Tile.<init>(Tile.java:67)
at com.brickbattle.client.src.objs.Player.initPlayerNum(Player.java:19)
at com.brickbattle.client.src.util.BrickBattle.init(BrickBattle.java:114)
at com.brickbattle.client.src.util.BrickBattle.run(BrickBattle.java:85)
at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.IllegalArgumentException: input == null! //HERE …
Run Code Online (Sandbox Code Playgroud) 我有两个ImageViews如下:
<ImageView android:id="@+id/image1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/ic_launcher" />
<ImageView android:id="@+id/image2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:adjustViewBounds="true"
android:scaleType="fitCenter"
/>
Run Code Online (Sandbox Code Playgroud)
第一个imageView以实际大小显示图像,第二个ImageView应该以fitCenter大小显示图像。
我一直在尝试此代码:
ImageView img1 = (ImageView) findViewById(R.id.image1);
ImageView img2 = (ImageView) findViewById(R.id.image2);
img2.setImageDrawable(img1.getDrawable());
Run Code Online (Sandbox Code Playgroud)
但是第二个ImageView只是将图像显示为第一个。是的,以其实际大小。
谁能帮忙找到解决方案?
谢谢
在我的一个JUnit测试中,我试图加载目录中包含的所有文件.我曾经.getClassLoader().getResource("otherresources")
找到目录.然后我做了一个新的java.io.File
.然后我用来listFiles()
获取所有子文件,然后.getClassLoader().getResource()
再次使用来加载每个文件.
URL url = FileLoadTest.class.getClassLoader().getResource("otherresources");
File directory = new File(url.getPath());
File[] files = directory.listFiles();
Run Code Online (Sandbox Code Playgroud)
基本上,我希望能够加载目录中的所有文件,而不知道它们究竟是什么.
我可以在Eclipse中正确运行测试.当我使用Maven(mvn install
)构建项目或使用surefire(mvn -Dtest=FileTest test
)自行运行测试用例时,测试用例失败了NullPointerException
.我认为这个问题与File api在资源部署到的JAR文件中没有按预期工作有关.
有关如何解决此问题的任何提示?
我制作了一个函数(Java),它应该bytes
从文件中读取并将它们打印到控制台:
public void loadPixels(int size){
ClassLoader cl = this.getClass().getClassLoader();
pixels = new byte[size];
try{
InputStream stream = cl.getResource("res/" + fileName).openStream();
stream.read(pixels);
System.out.println(pixels.toString());
stream.close();
}catch(Exception e){
e.printStackTrace();
}
}
Run Code Online (Sandbox Code Playgroud)
问题是,我要NullPointerException
上线了
InputStream stream = cl.getResource("res/" + fileName).openStream();
Run Code Online (Sandbox Code Playgroud)
对于我尝试打开的文件,名称是“font.spt”,这也是fileName
. 该文件位于项目根目录的“res”文件夹中,我目前使用的是 Eclipse IDE。
我对文件路径的方法是错误的,还是其他问题?
回顾一下:fileName
指向“font.spt”,它位于 bin 目录中的“res”文件夹下。
编辑:包含 .spt 文件的“res”文件夹现在位于项目的“bin”下,而不是根目录下,但我仍然收到错误消息。当从 IDE 运行或作为导出的 .jar 运行时,我仍然得到NullPointerException
,我应该把这些文件放在哪里?有人可以给我截图或示例吗?
我想通过菜单索引来订购我的资源,并拥有以下代码:
[[!getResources? &parents=`50` &sortdir=`ASC` &sortby=`menuindex` &limit=`100` &includeTVs=`1` &processTVs=`1` &tpl=`temp` ]]
Run Code Online (Sandbox Code Playgroud)
但是sortby
正义不行.
谁会知道我做错了什么?
谢谢!
我已经打包我的jar文件内的exe文件,我试图将其复制到一个临时位置,这样我可以使用运行它Desktop.browse()
,要做到这一点,我设置了输入流构造扫描仪使用class.getResourceAsStream
,然后用printwriter
写的,所有的到一个文件.发生的问题表明exe无效.我认为这是由于一些二进制数据丢失.如果有人可以提供帮助,请发表评论.
Scanner sc = new Scanner(ClassBuilder.class.getResourceAsStream("jd-gui.exe"));
File copy = new File("C://users//Owner//Desktop//java//jd-gui.exe");
copy.createNewFile();
PrintWriter writer = new PrintWriter(copy);
while(sc.hasNextLine())
writer.println(sc.nextLine());
writer.flush();
writer.close();
sc.close();
Desktop.getDesktop().browse(copy.toURI());
Run Code Online (Sandbox Code Playgroud) 我正在用Java做一个小游戏。对于这个游戏,我只是添加了声音。所以我想把我所有的图像和音频文件放在罐子里。对于图片,这很简单:
new ImageIcon(Main.class.getResource("images/machgd2.png")).getImage()
Run Code Online (Sandbox Code Playgroud)
但是对于音频,仅当我在Eclipse中运行程序时才起作用,而不能从jar中运行。我用:
File soundFile = new File(Main.class.getResource(filename).getFile());
Run Code Online (Sandbox Code Playgroud)
那么如何从.jar文件中获取此文件?
更新:
好的,感谢Andrew!为了播放声音,我使用了一个在网上找到的类,然后发现该类仅使用File来获取AudioInputStream,因此我删除了File。
我使用This方法写入文本文件(使用getResource()...在JAR文件中使用).我的文件在Classpath中,这是我的代码:
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
public class JarWrite {
public JarWrite(){
writethis();
}
public void writethis(){
try{
InputStreamReader isReader= new InputStreamReader(this.getClass().getResourceAsStream("AllBookRecords.txt"));
BufferedReader br = new BufferedReader(isReader);
PrintWriter writer1=new PrintWriter(new File(this.getClass().getResource("Boutput.txt").getPath()));
String Bs;
while( (Bs=br.readLine()) != null ){
writer1.println(Bs);
}
writer1.close();
br.close();
} catch(FileNotFoundException fnfe){
} catch(IOException ioe){
ioe.printStackTrace();
}
}
public static void main(String[] args){
new JarWrite();
}
}
Run Code Online (Sandbox Code Playgroud) 我正在尝试使用getResourceAsStream来加载java密钥库,尽管没有成功.我不确定它为什么会失败,但我在Play里面加载它!2.0从典型文件夹内的子文件夹.
这是文件夹结构和密钥库位置:
播放root >> app >> subfolder1 >> keystore
我正在加载它的文件的位置:
播放root >> app >>子文件夹1 >> scala.class
以及我如何将其加载到内部scala.class
:
getClass().getResourceAsStream("/keystore")
关于出了什么问题的任何想法?
我想在另一个资源中显示一个资源的内容.我认为这可以通过getResources实现,但我无法让它为我工作.我有:
[[getResources &parents=`-1` $resources =`16`]]
Run Code Online (Sandbox Code Playgroud)
但没有任何显示.我确保已发布所有资源并正确安装了getResources.
谁能指出我正确的方向?
谢谢
我正在尝试使用IntelliJ中标记为"资源根"的资源目录中的文件,但以下代码无法找到该文件.
你能告诉我出了什么问题吗?谢谢.
public class ResourceTest {
public void testResource() {
URL url = this.getClass().getResource("resources/table.1gram");
System.out.println(url);
}
public static void main(String[] args) {
ResourceTest rt = new ResourceTest();
rt.testResource();
}
Run Code Online (Sandbox Code Playgroud)
}
我发布这个问题是因为我无法理解为了从我的java项目加载资源文件我必须做什么.
结构如下:
我想要做的是加载boletinoficial.wsdl
文件以获取URL.
我曾尝试多种方法来做到这一点:getClass.getClassLoader.getResource()
,getClass.getResource()
,使用绝对路径,相对路径.但总是得到null或异常.我还在src
构建路径中添加了文件夹,但没有成功.
有没有简单的方法来加载该文件?
编辑
好.现在我有了结构,
如何boletinoficial.wsdl
从App.java类加载资源?