Images are not displayed in the bank, but are displayed in the compiler?

I know, I know, this was asked before. But every resource I looked at uses IconImages, while I only have simple images.

Is there any other solution? Please help as I lingered on, researching and trying to figure this out for several days without progress.

Image Floor = Toolkit.getDefaultToolkit().getImage("Floor.PNG");

EDIT: If I were to make sure that the jar would not be compressed, and I created a separate directory in the jar for images and set the correct path to the file, would this code work?

+3
source share
6 answers

# getImage (String s) , , Jar , . .

, ImageIO.read(...) InputStream, getResourceAsStream (...), , ImageIO. .

, , ?

+3

, , , .

.

1) Require there to be a file called "images.txt" in the directory with your jar (or bundle it into the jar.)
2) Make a file called "images.txt" with a format like `FLOOR:C:\\images\\floor.png`
3) Load this file into memory on load.
4) Load your images based on the entries in the file

, jar:)

, . , , ( , )

, , .

- , img, . , .

+1

getImage , Jar. jar , jar. jar, , , Jar. , , , .

+1

. , "PNG", "png", .

+1

, JAR , . JAR , !

getImage("Floor.png")

JAR ( ), , JAR , . JAR , , Floor.png? ,

getImage("C:\Some Folder Path\Floor.png")

Floor.png C:\Some Folder Path\ JAR.

, JAR ( ZIP ). :

http://download.oracle.com/javase/tutorial/uiswing/components/icon.html#getresource

0

, ImageIcon : new javax.swing.ImageIcon(getClass(). getResource ( "myimage.jpeg" ), .

<head-desk /> You really need to read JavaDocs. Otherwise, you "encode by magic." Which usually will not work.

URL urlToImage = getClass().getResource("myimage.jpeg");
// If you need to support Java 1.3
Image image = Toolkit.getDefaultToolKit().getImage(urlToImage);
// If your users have dragged their JRE into this millennium
BufferedImage bufferedImage = ImageIO.read(urlToImage);
0
source

Source: https://habr.com/ru/post/1794001/


All Articles