Weird problem on displaying images.
For some weird reason, i' m not able to display images on my applications, but, if i initialize one or more of my classes that display images by using a JApplet, the images suddenly appear.
Here is an example: Code:
public class GUI extends JFrame{
public GUI (){
//Display images
}
public static void main(String argv[]) {
new GUI();
}
Doing this, the images do not appear.
Code:
public class MyApplet extends JApplet{
public void init(){
new GUI();
}
Doing this, the images do appear.
Images are .jpg and i tried various methods for displaying images, always getting this exact problem;
Re: Weird problem on displaying images.
You've got a bug somewhere, but it's difficult to see where given the limited information that you've presented so far. If you're still stuck, you may want to give us more information, pertinent code, and what happens when you try to debug things with a debugger or with System.out.println(...) calls sprinkled in your program to test the state of important variables.
Re: Weird problem on displaying images.
Another example:
Code:
public class ImagePanel extends JPanel {
private String path = null;
ImageIcon avatar;
public ImagePanel(String path){
this.path = path;
avatar = new ImageIcon(path);
}
@Override
public void paintComponent(Graphics g){
super.paintComponent(g);
Graphics2D g2 = (Graphics2D) g;
g2.drawImage(avatar.getImage(), 0, 0, null);
g2.dispose();
}
public static void main(String argv[]) {
JFrame f = new JFrame();
ImagePanel p = new ImagePanel("Koala.jpg");
p.setPreferredSize(new Dimension(200,200));
f.getContentPane().add(p);
f.setVisible(true);
f.pack();
}
}
this doesn't work, the image is not painted.
Code:
public class MyApplet extends JApplet{
public void init(){
new ImagePanel("Koala.jpg").main(null);
}
}
this works.
Re: Weird problem on displaying images.
You are showing the GUI BEFORE it has been layed out.
Try setting it visible AFTER it is ready to be seen.
Re: Weird problem on displaying images.
Quote:
Originally Posted by
Norm
You are showing the GUI BEFORE it has been layed out.
Try setting it visible AFTER it is ready to be seen.
thanks for the reply but it still doesn't work, i really don't get why it only works if i use an Applet.
Re: Weird problem on displaying images.
Your not working version works for me. The image comes up immediately.
How are you executing the program? java on a command line or ?
Re: Weird problem on displaying images.
Quote:
Originally Posted by
Norm
Your not working version works for me. The image comes up immediately.
How are you executing the program? java on a command line or ?
Damn, i tried to execute the program on command line and it worked, apparently Eclipse is the problem, even if i make a runnable jar file with Eclipse it still doesn't work. Thanks though.
Re: Weird problem on displaying images.
What does the IDE put in the jar file besides your class files and the manifest?
You can view the contents with a zip file utility.
Re: Weird problem on displaying images.
Quote:
Originally Posted by
Norm
What does the IDE put in the jar file besides your class files and the manifest?
You can view the contents with a zip file utility.
Just the images. Should i try to install another version of eclipse or try in a different PC ?
Re: Weird problem on displaying images.
Does the code running from the jar find the image?
Getting an image from within a jar requires accessing a resource and not a disk file.
Re: Weird problem on displaying images.
Quote:
Originally Posted by
Norm
Does the code running from the jar find the image?
Getting an image from within a jar requires accessing a resource and not a disk file.
With the use of "getClass().getResource("Images\\Koala.jpg")", Eclipse is now able to display my images, but the runnable jar file doesn't work(doesn't even load the frame).
EDIT: i get a NullPointerException.
EDIT2:I replaced "getClass().getResource("Images\\Koala.jpg")" with "getClass().getResource("Images/Koala.jpg")" and worked fine. thanks again for the great help.
Re: Weird problem on displaying images.
Quote:
i get a NullPointerException.
Please post the full text of the error message.
Check that the image files in the jar file are in the correct folder.
"getClass().getResource("Images\\Koala.jpg")"
Use / in the path vs \\
Re: Weird problem on displaying images.
Yes thanks was exactly that, i was just editing the post.
Re: Weird problem on displaying images.
Re: Weird problem on displaying images.
Quote:
Originally Posted by
Norm
Does it work now?
yes, perfectly, thanks.