So I'm making the transition from BlueJ to Eclipse like any good college Freshman would and I can't, for the life of me, Find where to store an image in the workspace which would allow me to access it from my code. any ideas?
Usually, you store your images in a resources folder.
Here's an example from one of my projects.
My images are stored in an images folder, which is included in the Java classpath.
I read them with a method call:
model.setCheckMark(readImage("/check_mark.png"));
And here's the readImage method:
private Image readImage(String streamString) {
try {
return ImageIO.read(getClass().getResourceAsStream(streamString));
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
Right click on your project on the left side-bar and create a 'New -> Source Folder'. A common mistake and one I made is to simply create a 'New -> Folder'. This wont work and I can relate to anyones frustrations at this and similar hurdles when moving from BlueJ to a proper IDE like Eclipse.
Related
I'm working on a simple 2D game in JavaFX, and I loaded some of my assets (just a bunch of folders and images) into my project into their own directory, and went to make another Java class for all the stuff the player needs (movement, keypress, loading player assets), and none of the images are loading. Does anyone know why? I have the code I know is causing the problem below.
class Player
{
public Player() throws FileNotFoundException
{
System.exit(-1);
}
Image leftSide = new Image(new FileInputStream("resources/player/leftNO.png");
ImageView leftSideView = new ImageView(leftSide);
}
My directory is listed below:
.idea
out
resources
player
leftNO.png
rightNO.png
src
sample
Main.java
Player.java
Anyone know why this is happening? Thanks in advance, even if you don't have an answer.
EDIT: Also, I forgot to mention that when I try to open the image in the Main.java class, it loads fine.
When you say not loading, do you mean that it throws an exception or is blank?
Depending on which, this could work:
Mark your resources directory as a source folder and try
class Player
{
public Player() throws FileNotFoundException
{
System.exit(-1);
}
Image leftSide = new Image(getClass().getResourceAsStream("/player/leftNO.png");
ImageView leftSideView = new ImageView(leftSide);
}
This will also allow you app to work from within a jar
I have tried several solutions for changing the icon of my application, but none have worked! I do not get any error when i do the following but it still won't change!? Please, can someone tell me where I am going wrong because i don't see any error, and I am not receiving any error either!
I even made sure that the icon I want to use is a 20x20 pixel icon, because I read somewhere that is the maximum size for an icon.
frame.setIconImage(
new ImageIcon(getClass().getResource("/images/bfc_icon.png")).getImage());
Why is this not working? Any help would be greatly appreciated!
EDIT:
I am testing if the file exists, turns out it does but it still is not being set as the application icon...why is this??
URL url = getClass().getResource("src/images/bfc_icon.png");
if (url == null)
System.out.println( "Could not find image!" );
else
frame.setIconImage(new ImageIcon(url).getImage());
private void formWindowOpened(java.awt.event.WindowEvent evt) {
try {
// TODO add your handling code here:
Image img=ImageIO.read(getClass().getResource("ur path"));
this.setIconImage(img);
} catch (IOException ex) {
}
this will work
It seems a bit too late, but I hope this helps.
This problem probably happens when you called the setIconImage() before you initialize the JFrame.
I also had this problem with the code below (w/ Eclipse IDE):
setIconImage(Toolkit.getDefaultToolkit().getImage(Apps.class.getResource("/ico.png")));
initComponents();
I accidentally solved the problem by swapping these two so it looks like this:
initComponents();
setIconImage(Toolkit.getDefaultToolkit().getImage(Apps.class.getResource("/ico.png")));
You should try to do it as well, at least call the setIconImage() after the JFrame has initialized if you didn't use any window builder tool.
Cheers!
In my case, I just simply copied the picture I want to use as my icon to the project folder and not the src folder (source code folder) and it worked.
I've spent almost 2 days in trying to load files from inside of my netbeans project, but it always gives NullPointException.
currently my directory looks like:
JavaFXApplication:
src
--Manifest (contains Manifest.java)
--images (inside Manifest package aka Manifest.images)
--server.jpg (inside images package)
I'm trying to load the server.jpg from images package, but it always return NULL.
Here is the snippet of my code:
try {
rect.setFill(new ImagePattern(new Image(Manifest.class.getResourceAsStream("images\\server.jpg"))));
} catch (NullPointerException e) {
System.out.println(Manifest.class.getResourceAsStream("server.jpg"));
}
Exactly 2 days before, I saw this code from a YouTube Tutorial, and it doesn't worked. Try many of those solutions from here, but nothing yield for me.And suddenly it worked. Next day, tried to run the same code, and again same NULL error.
Can you guys please help me. I'm totally new to JavaFX. Don't have much experience with it.
Use getClass().getClassLoader().getResourceAsStream(""). When you do Manifest.class.getResourceAsStream("images\\server.jpg"), it will try to load the file relative to where the Manifest.class is present.
I've been working in a little project to get a bit of practice programming. It's basically done, but I won't be satisfied until I can use images properly, a bit of help would be appreciated.
So, currently I'm using the the getImage method from the ImageIcon class, like so:
Image body = new ImageIcon("C:/Users/Centollo/Documents/NetBeansProjects/Chess/build/classes/chess/img/WhiteBishop.png").getImage();
I've been trying to figure out how to do the same thing without using an absolute path, but I don't know how to make the images a part of the jar so that it works fine in any other machine.
All I need to know is where to put the images and how would the code to access them look like.
Try to explain it like I'm stupid, please. I've read answers to similar questions but I can't make heads or tails of them.
I'm working in NetBeans with a "chess" package with all the .java and a "chess.img" package with all the .png.
If your class extends from JFrame, you can do this:
Image image = new ImageIcon(this.getClass().getResource("/images/MyImage.jpg")).getImage();
If your class extends Applet, you can go this way:
private URL base = null;
private Image myImage = null;
try
{
base = getDocumentBase();
} catch (Exception e)
{
e.printStackTrace();
}
myImage = getImage(base, "images/MyImage.jpg");
A very quick google search yields this:
URL resource = getClass().getClassLoader().getResource( "img/WhiteBishop.png" );
Image body = new ImageIcon( resource );
There are a couple ways to do this but here's the way I would suggest:
Make sure the chess.img folder is in your application's classpath. Then try referring to the path like chess.img/image (yes, you can use forward slash in windows.)
If that doesn't work use:
ChessClass.class.getClassLoader().loadResourceAsStream("/chess.img/image");
Note the forward slash at the beginning of the file reference. This points to the root of the classpath. It's a bit confusing as someone with unix/linux experience might think it refers to the root of the file system. This tends to work better than the other answer given for reasons I knew 10 years ago. This is an ugly bit of Java that was never quite cleaned up.
I have a program in Java using LWJGL. It runs fine in Eclipse, but when I try to compile it as a jar file it crashes, giving me a NullPointerException. It's been asked before, I know, but I don't seem to get an answer that works. Any help here? Thanks in advance!
The thing that seems to be having the problem is the class TextureHelper:
public class TextureHelper {
public static Texture LoadTexture(String texture)
{
try {
return TextureLoader.getTexture("PNG", ResPlaceholder.class.getResourceAsStream(texture));
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
}
Some notes:
I've also tried "/res/" + texture, as well as many other things like it.
ResPlaceholder is a class that sits in the res folder where I store all my images. It's a blank empty class.
This works perfectly in Eclipse.
My jar has these folders (just as an example):
foo.jar
----core
--------TextureLoader
----res
-------- Assorted Image Files
-------- ResPlaceholder
This is the same as the packages in eclipse.
Any help you have would be appreciated, I've been stuck on this for days with no progress.
EDIT:
META-INF/MANIFEST.MF
Main.class config/
config/Images.class
core/
core/LevelLoader.class
core/TextureHelper.class
core/TileSet.class
-Skipping some other stuff that has nothing to do with this-
res/
res/ResPlaceholder.class
res/BlankImg.png
res/test.txt
res/testImg.png
res/testTiles.png
If the path String is something like: /res/testImg.png it should work.
i.e.,
String resourcePath = "/res/testImg.png";
InputStream inStream = ResPlaceholder.class.getResourceAsStream(resourcePath );
BufferedImage img = ImageIO.read(inStream);
// use the img BufferedImage here
// for example:
ImageIcon icon = new ImageIcon(img);
JOptionPane.showMessageDialog(null, icon);
As an aside, it's always good to test problems and check new concepts in small test programs. In your case, I would create a small simple program with just a main method that attempts to extract a jar's image resource and display it in an option pane much like my small code above does. I'd try to avoid long lines and instead separate each step on its own line, again similar to my post above. This way if an exception is thrown, the code on the line will be much more informative as to what is causing the error.