Re: I cant load the icon!!!!
What errors are you getting if any?
Edit: make sure the image is in the right path or on the classpath. If you try giving a full path to the image it will most likely work as in (c:/pictures/mercuryLogo.jpg)
// Json
Re: I cant load the icon!!!!
never had any idea that i have to set the path of the file exactly ... all the references that im reading in google is just telling me that i just have to type the file name including the file type.... "imge.jpg" ...
tnx sir Json!..
Re: I cant load the icon!!!!
the icon is alreay loaded in the frame.. a follow up question.. if i load the image in a frame.. will the JVM set the image size into a default? i mean theres no way i can adjust its dimension just a little..?
Re: I cant load the icon!!!!
I dont know how the resizing works on icons to be honest, however when it comes to putting a specific location down you might not want to do that when you ship your program so you need to make sure the picture is on the classpath in that case.
// Json
Re: I cant load the icon!!!!
An alternative method to load the image is to load it as a resource.
Code :
java.net.URL imgURL = getClass().getResource(path); //where path is a string path relative to your class
String pt = imgURL.getPath();
if (imgURL != null) {
return new ImageIcon(imgURL, description);
} else {
System.err.println("Couldn't find file: " + path);
}
In many cases I prefer this method because you can ship the icons within a jar. The downside is that loading things this way is slower (for whatever reason) than using the Toolkit loader, so its usually only useful for smaller images. Along the same lines of Json's suggestion of image paths, if I were to do the toolkit approach, I'll usually place the image in a path local to the app, and load it be creating a temp local file from which I can get the full path and with a little string changes get the path to the image
Re: I cant load the icon!!!!
using this way of yours sir copeg.. what method should i use to call this object to load an image..
the setIconImage(), doesnt accept a URL object..
Re: I cant load the icon!!!!
and which of the two is better to prevent some loading errors..
which one do you prefer most?
Re: I cant load the icon!!!!
Quote:
I dont know how the resizing works on icons to be honest, however when it comes to putting a specific location down you might not want to do that when you ship your program so you need to make sure the picture is on the classpath in that case.
can you check this one sir Json.. this one adjusts the image... in a quite amount
Code :
.getScaledInstance(300, 300, 0)
the complete block for my image icon is this
Code :
Image icon = new ImageIcon("c:/pics/mercuryLogo.jpg").getImage().getScaledInstance(300, 300, 0);
Re: I cant load the icon!!!!
Quote:
Originally Posted by
chronoz13
the setIconImage(), doesnt accept a URL object..
Sorry, I mis-read your original post and thought you were just trying to create an ImageIcon. Anyway, from a URL you could create a 'dummy' ImageIcon and retrieve the image that way (getImage()) or use ImageIO.read() which can read a URL and returns a BufferedImage (extends Image).
Quote:
Originally Posted by
chronoz13
and which of the two is better to prevent some loading errors..
which one do you prefer most?
Again, it depends upon what you wish to do. Getting the image via the class loader allows you to package your application and associated icons/images inside a ja. The concepts are somewhat the same though in that rather than specifying a global path to the image, which can move from user to user and platform to platform, the image should be placed in a location relative to the application so you can let the app find the image as opposed to relying on a constant path to the image
Re: I cant load the icon!!!!
Quote:
Again, it depends upon what you wish to do. Getting the image via the class loader allows you to package your application and associated icons/images inside a ja. The concepts are somewhat the same though in that rather than specifying a global path to the image, which can move from user to user and platform to platform, the image should be placed in a location relative to the application so you can let the app find the image as opposed to relying on a constant path to the image
so you mean sir.. URL class is better for getting an image in my case..?... i will just follow your suggestions.. i dont want to take any risk in my project....tnx!
anyway..
Re: I cant load the icon!!!!
Quote:
Originally Posted by
chronoz13
URL class is better for getting an image in my case..?
No. I just meant to reiterate Json's point - access your image relative to your application and not relying on something like:
'c:/pictures/mercuryLogo.jpg' actually being there. Using the class path or getting the path of your app allows you to do so.
Re: I cant load the icon!!!!
How about something like this.
Code :
final URL url = ClassLoader.getSystemClassLoader().getResource("mercuryLogo.jpg");
final Image icon = Toolkit.getDefaultToolkit().getImage(url);
Oh and make sure your "mercuryLogo.jpg" file is in the root of the jar file that you have packaged together :)
// Json