Dynamically get image paths in eclipse - java

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

Related

Would writing an image using Image class or the class BufferedImage class work in the same way?

Here are my two implementations. Would both give the same result? I know that BufferedImage is the child class of the Image.
First implementation: writeImage takes an Image Object and uses RenderedImage in the ImageIO.write method.
public void writeImage(Image img, String outputFile) {
try {
ImageIO.write((RenderedImage) img, "jpg", new File(outputFile));
} catch (IOException e) {
}
...
Second Implementation: writeImage takes a BufferedImage`` object and uses the BufferedImage object in theImageIO.write method.
public void writeImage(BufferedImage img, String outputFile){
try {
ImageIO.write(img, "jpg", new File(outputFile));
} catch (IOException e) {
}
...
...
Also, try to tell what's the difference between the two ways of writing an image.

Loading images in java (eclipse vs IntelliJ)

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.

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

Detecting if a final is blank in a constructor

I'm trying to create an enum for final Images, where the variable 'image' would be loaded from a file. If an IOException occurs, I want 'image' to be set to null. However, according to the compiler, 'image' may or may not be set when the catch block runs.
public enum Tile {
GROUND("ground.png"), WALL("wall.png");
final Image image;
Tile(String filename) {
try {
image = ImageIO.read(new File("assets/game/tiles/" + filename));
} catch (IOException io) {
io.printStackTrace();
image= null; // compiler error 'image may already have been assigned'
}
}
}
Final variables need to be set in the constructor, so if the image for some reason cannot be read, it has to be set to something. However, there's no way to tell whether or not image has actually been set. (In this case, the catch block only will run if no image is set, but the compiler says that it may have been set)
Is there a way for me to assign image to null in the catch block only if it hasn't been set?
Try using a local temporary variable:
public enum Tile {
GROUND("ground.png"), WALL("wall.png");
final Image image;
Tile(String filename) {
Image tempImage;
try {
tempImage= ImageIO.read(new File("assets/game/tiles/" + filename));
} catch (IOException io) {
io.printStackTrace();
tempImage= null; // compiler should be happy now.
}
image = tempImage;
}
}
Here is the solution I ended up using. It adds a method so that the code return if the ImageIO class does find an image, leaving no chance for the catch statement to be called.
public enum Tile {
GROUND("ground.png"), WALL("wall.png");
final Image image;
Tile(String filename) {
image = getImage(filename);
}
Image getImage(String filename) {
try {
return ImageIO.read(new File("assets/game/tiles/" + filename));
} catch (IOException io) {
io.printStackTrace();
return null;
}
}
}
However, this isn't really a way to detect a blank final variable. I'm hoping to see if there's a way to set a final variable inside a try/catch without going around the issue using temporary variables.

Categories