Results 1 to 15 of 15
Like Tree1Likes
  • 1 Post By Norm

Thread: Weird problem on displaying images.

  1. #1
    adwart is offline Member
    Join Date
    Jul 2011
    Posts
    25
    Rep Power
    0

    Default 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:
    Java 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.
    Java 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;

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default 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.

  3. #3
    adwart is offline Member
    Join Date
    Jul 2011
    Posts
    25
    Rep Power
    0

    Default Re: Weird problem on displaying images.

    Another example:
    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 doesn't work, the image is not painted.
    Java Code:
    public class MyApplet extends JApplet{
    	public void init(){
    		new ImagePanel("Koala.jpg").main(null);
    	}
    
    }
    this works.

  4. #4
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default 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.

  5. #5
    adwart is offline Member
    Join Date
    Jul 2011
    Posts
    25
    Rep Power
    0

    Default Re: Weird problem on displaying images.

    Quote Originally Posted by Norm View Post
    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.

  6. #6
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default 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 ?
    adwart likes this.

  7. #7
    adwart is offline Member
    Join Date
    Jul 2011
    Posts
    25
    Rep Power
    0

    Default Re: Weird problem on displaying images.

    Quote Originally Posted by Norm View Post
    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.

  8. #8
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default 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.

  9. #9
    adwart is offline Member
    Join Date
    Jul 2011
    Posts
    25
    Rep Power
    0

    Default Re: Weird problem on displaying images.

    Quote Originally Posted by Norm View Post
    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 ?

  10. #10
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default 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.

  11. #11
    adwart is offline Member
    Join Date
    Jul 2011
    Posts
    25
    Rep Power
    0

    Default Re: Weird problem on displaying images.

    Quote Originally Posted by Norm View Post
    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.
    Last edited by adwart; 09-15-2011 at 01:34 AM.

  12. #12
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default Re: Weird problem on displaying images.

    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 \\

  13. #13
    adwart is offline Member
    Join Date
    Jul 2011
    Posts
    25
    Rep Power
    0

    Default Re: Weird problem on displaying images.

    Yes thanks was exactly that, i was just editing the post.

  14. #14
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,308
    Rep Power
    25

    Default Re: Weird problem on displaying images.

    Does it work now?

  15. #15
    adwart is offline Member
    Join Date
    Jul 2011
    Posts
    25
    Rep Power
    0

    Default Re: Weird problem on displaying images.

    Quote Originally Posted by Norm View Post
    Does it work now?
    yes, perfectly, thanks.

Similar Threads

  1. Images not displaying in JFrame.
    By jlennards in forum AWT / Swing
    Replies: 3
    Last Post: 07-12-2011, 03:46 AM
  2. Displaying multiple images
    By asmitarnd in forum AWT / Swing
    Replies: 2
    Last Post: 04-19-2011, 12:14 PM
  3. Displaying moving images
    By Maulik in forum AWT / Swing
    Replies: 3
    Last Post: 04-02-2011, 12:00 AM
  4. Displaying large images
    By pir8ped in forum AWT / Swing
    Replies: 2
    Last Post: 01-21-2009, 09:20 PM
  5. Images not displaying in JSP in IE7
    By chadscc in forum Advanced Java
    Replies: 0
    Last Post: 11-13-2007, 03:24 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •