Loading images in java (eclipse vs IntelliJ) - java

One would load an image:
project>res>img.png (path = "res/img.png")
BufferedImage image = loadImage(path);
Where LoadImage is:
protected BufferedImage loadImage(String path) {
BufferedImage img = null;
try {
img = ImageIO.read(new File(path));
} catch(IOException e) {
System.err.println("could not load: " + path);
}
return img;
}
Someone using Eclipse used:
(path = "/img.png")
BufferedImage image = null;
try {
image = ImageIO.read(Sprite.class.getResourceAsStream(path));
} catch (IOException e) {
e.printStackTrace();
}
But using this in IntelliJ gives:
Exception in thread "Game_main" java.lang.IllegalArgumentException:
input == null!
why getResourceAsStream fails?

getResourceAsStream() uses (by default) the system classloader to find the file. Therefore, the resources directory has to be on the classpath - check that the IntelliJ project is correctly including the res directory and marking it as a resource directory.

Related

How to load the image from a jar file?

This code works only if I run my project, but if I run the Jar file, I get a NullPointerException. The image is in src directory.
try {
URL resource = Sticker.class.getResource("\\transparentSticker.png");
img = null;
this.img = ImageIO.read(Paths.get(resource.toURI()).toFile());
System.out.println("c");
} catch (IOException | URISyntaxException e) {
System.out.println("caught");
}
If I use this code, an IllegalArgumentException is being thrown.
InputStream input = Sticker.class.getResourceAsStream("\\transparentSticker.png");
if (input == null) {
input = Sticker.class.getClassLoader().getResourceAsStream("\\transparentSticker.png");
}
try {
img = ImageIO.read(input);
} catch (IOException e) {
e.printStackTrace();
}
How to load the image?
You did say the PNG is in the src folder?
Let's see, after reading this and this, I think it needs to be:
InputStream input = Sticker.class.getResourceAsStream("/src/transparentSticker.png");
or
URL resource = Sticker.class.getResource("/src/transparentSticker.png");
Think unix, not windows.

Dynamically get image paths in eclipse

this is my code below
public BufferedImage icon32 = loadBufferedImage("/icon/icon32.png");
public BufferedImage icon64 = loadBufferedImage("/icon/icon64.png");
private BufferedImage loadBufferedImage(String string)
{
try
{
BufferedImage bi = ImageIO.read(this.getClass().getResource(string));
return bi;
} catch (IOException e)
{
e.printStackTrace();
}
return null;
}
I just wanted to know if there's a way to dynamically get the images from my image directory in eclipse instead of having to access them one by one

Loading a texture from assets folder for OpenGL

I know there are many questions relating to this question, but few appear to be for openGL.
I'm trying to load some PNG files from the assets folder into a Bitmap, but for some reason the returned Bitmap is null which in turn throws a NullPointerException here:
GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, bitmap, 0);
The code I am using to load the image from the assets folder:
public static Bitmap getBitmapFromAsset(AssetManager mgr, String path)
{
InputStream is = null;
Bitmap bitmap = null;
try {
is = mgr.open(path);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inScaled = false;
bitmap = BitmapFactory.decodeStream(is, null, options);
}
catch (final IOException e)
{
bitmap = null;
Log.e(TAG, "FAILED TO get getBitmapFromAsset: " + e.getMessage());
}
finally
{
if (is != null)
{
try
{
is.close();
}
catch (IOException ignored)
{
}
}
}
return bitmap;
}
I've tried it a few different ways, e.g without the BitmapFactory.Options, but no matter what I am getting the NullPointerException, so I'm guessing there's another procedure I should be doing.
P.S. I can load them from the res/raw folder, but I can't have subdirectories to organise my assets.
OK, I just realised that my assets folder was under the res folder for some reason. I just put it under the main folder app/main/assets and it's working.
*blushes

Loading a BufferedImage in a .jar

I'm currently trying working on an own game and created a Animation class, my problem is that i want the programm to be able to still find all the images when i create a jar out of it so I tried to load an Image via
BufferedImage img = ImageIO.read(getClass().getClassLoader().getResourceAsStream("player.png"));
but when I start the code I get a NullPointerException, i checked the location twice but the image exists and there should be no problems, can anyone help me out a bit?
try this
public BufferedImage loadImage(String fileName){
BufferedImage buff = null;
try {
buff = ImageIO.read(getClass().getResourceAsStream(fileName));
} catch (IOException e) {
e.printStackTrace();
return null;
}
return buff;
}

Java - How to access an image packed in an applet jar

I have created an applet jar. That jar contains an images in the following folder
com\common\images\red.bmp
Now, I want to display this image on the Swing Applet.
private static final ImageIcon redIndicator = new ImageIcon("com\\common\\images\\red.bmp");
After that, I have attached the redIndicator to a JPanel but I am not able to see this image.
Any suggestions?
==================================EDITED=========================================
private static final ImageIcon marker = loadImage("com/common/images/scale.jpg");
#SuppressWarnings("unused")
private static ImageIcon loadImage(String imagePath) {
BufferedInputStream imgStream = new BufferedInputStream(TpcHandler.class.getResourceAsStream(imagePath));
int count = 0;
if (imgStream != null) {
byte buf[] = new byte[2400];
try {
count = imgStream.read(buf);
} catch (java.io.IOException ioe) {
return null;
} finally {
if (imgStream != null)
try {
imgStream.close();
} catch (IOException e) {
e.printStackTrace();
}
}
if (count <= 0) {
LOGGER.warning("Empty image file: " + imagePath);
return null;
}
return new ImageIcon(Toolkit.getDefaultToolkit().createImage(buf));
} else {
LOGGER.warning("Couldn't find image file: " + imagePath);
return null;
}
}
I am getting the following exception
java.io.IOException: Stream closed
at line count = imgStream.read(buf);
This should do the trick (if called from a class loaded from that same jar):
new ImageIcon(getClass().getResource("/com/common/images/red.bmp"))
Use YourPanel.class.getResourceAsStream("/com/common/images/red.bmp"), read the stream to a byte[] and construct the ImageIcon based on that. (and don't use bmps - prefer png or jpeg)
Applets and Images that is a frequently asked questions so, as for Java applets and images, I recommend you read one of my previous answers hope it helps a bit :)
Good luck

Categories