Results 1 to 15 of 15
- 09-14-2011, 02:31 AM #1
Member
- Join Date
- Jul 2011
- Posts
- 25
- Rep Power
- 0
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:Doing this, the images do not appear.Java Code:public class GUI extends JFrame{ public GUI (){ //Display images } public static void main(String argv[]) { new GUI(); }
Doing this, the images do appear.Java Code:public class MyApplet extends JApplet{ public void init(){ new GUI(); }
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.
- 09-14-2011, 07:18 PM #3
Member
- Join Date
- Jul 2011
- Posts
- 25
- Rep Power
- 0
Re: Weird problem on displaying images.
Another example:
this doesn't work, the image is not painted.Java 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 works.Java Code:public class MyApplet extends JApplet{ public void init(){ new ImagePanel("Koala.jpg").main(null); } }
- 09-14-2011, 07:51 PM #4
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.Last edited by Norm; 09-14-2011 at 07:55 PM.
- 09-14-2011, 08:25 PM #5
Member
- Join Date
- Jul 2011
- Posts
- 25
- Rep Power
- 0
- 09-14-2011, 08:28 PM #6
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 ?
- 09-14-2011, 10:42 PM #7
Member
- Join Date
- Jul 2011
- Posts
- 25
- Rep Power
- 0
- 09-14-2011, 10:46 PM #8
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.
- 09-14-2011, 11:17 PM #9
Member
- Join Date
- Jul 2011
- Posts
- 25
- Rep Power
- 0
- 09-14-2011, 11:38 PM #10
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.
- 09-15-2011, 01:07 AM #11
Member
- Join Date
- Jul 2011
- Posts
- 25
- Rep Power
- 0
Re: Weird problem on displaying images.
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.Last edited by adwart; 09-15-2011 at 01:34 AM.
- 09-15-2011, 01:29 AM #12
Re: Weird problem on displaying images.
Please post the full text of the error message.i get a NullPointerException.
Check that the image files in the jar file are in the correct folder.
"getClass().getResource("Images\\Koala.jpg")"
Use / in the path vs \\
- 09-15-2011, 01:35 AM #13
Member
- Join Date
- Jul 2011
- Posts
- 25
- Rep Power
- 0
Re: Weird problem on displaying images.
Yes thanks was exactly that, i was just editing the post.
- 09-15-2011, 01:35 AM #14
Re: Weird problem on displaying images.
Does it work now?
- 09-17-2011, 03:45 AM #15
Member
- Join Date
- Jul 2011
- Posts
- 25
- Rep Power
- 0
Similar Threads
-
Images not displaying in JFrame.
By jlennards in forum AWT / SwingReplies: 3Last Post: 07-12-2011, 03:46 AM -
Displaying multiple images
By asmitarnd in forum AWT / SwingReplies: 2Last Post: 04-19-2011, 12:14 PM -
Displaying moving images
By Maulik in forum AWT / SwingReplies: 3Last Post: 04-02-2011, 12:00 AM -
Displaying large images
By pir8ped in forum AWT / SwingReplies: 2Last Post: 01-21-2009, 09:20 PM -
Images not displaying in JSP in IE7
By chadscc in forum Advanced JavaReplies: 0Last Post: 11-13-2007, 03:24 PM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks