从项目文件夹中的文件加载图像 [英] Load image from a file inside a project folder

查看:104
本文介绍了从项目文件夹中的文件加载图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从文件加载图像而不使用 FileChooser
文件夹是:

I'm trying to load an image from a file without using a FileChooser. The folders are:

TestProject
-src
--application
---(all_the_classes_i'm_using.java)
-assets
--drawIcon.png

我想在assets文件夹中加载图像。
我试过:

I want to load the image in the assets folder. I've tried:

Image image = new Image("../assets/drawIcon.png")
Image image = new Image(getClass().getResourceAsStream("../assets/drawIcon.png"))

我用字符串路径/TestProject/assets/drawIcon.png尝试了它,但没有。我不明白如何加载这个图像!

I've tried it with the string path "/TestProject/assets/drawIcon.png", but nothing. I don't understand how to load this image!

推荐答案

设置资产目录作为资源目录,然后从/drawIcon.png位置加载图像作为资源:

Set the assets directory as a resource directory and then load the image as a resource from the location "/drawIcon.png":

URL url = getClass().getResource("/drawIcon.png");
Image image = ImageIO.read(url);

如果你想创建一个javafx图片:

In case you want to create a javafx Image:

Image image = new Image("/drawIcon.png");

在这种情况下,同时将该文件夹标记为资源文件夹。

In this case, also, mark that folder as resource folder.

此处有更多信息: https: //docs.oracle.com/javafx/2/api/javafx/scene/image/Image.html

这篇关于从项目文件夹中的文件加载图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆