Results 1 to 9 of 9
  1. #1
    MarioBrawler is offline Member
    Join Date
    May 2012
    Posts
    18
    Rep Power
    0

    Default Cant find the problem with this short code.

    Hello, so I'm currently making a program with lots of images. Im having problems with awt codes and learned that swing is better in image handling, so I'm learning swing.
    So when I typed in this code, I seem to have a problem.
    Java Code:
    import javax.swing.JFrame;
    import javax.swing.ImageIcon;
    import javax.swing.JLabel;
    import java.awt.Container;
    
    
    
    public class Test {
    public static void main(String args[]){
    	JFrame frame = new JFrame();
    	ImageIcon icon = new ImageIcon("bg.jpg");
    	JLabel label = new JLabel(icon);
    	Container c = frame.getContentPane();
    	
    	c.add(label);
    	
    	frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	frame.pack();
    	frame.setVisible(true);
    	
    }
    }
    The program wont open fully and I've spent about an hour trying to fix it. I think it might have something to do with JLabel and ImageIcon.

  2. #2
    aussiemcgr is offline Member
    Join Date
    May 2012
    Posts
    5
    Rep Power
    0

    Default Re: Cant find the problem with this short code.

    What do you mean by "the program wont open fully"? Does it throw an exception? Is the frame too small?

  3. #3
    MarioBrawler is offline Member
    Join Date
    May 2012
    Posts
    18
    Rep Power
    0

    Default Re: Cant find the problem with this short code.

    Sorry I should of specified. The program opens but nothing is being displayed so the frame.pack(); makes the frame with 0x0 pixels :o
    So I don't think the frame is the problem, I believe that it is the JFrame or ImageIcon that is the problem.

  4. #4
    aussiemcgr is offline Member
    Join Date
    May 2012
    Posts
    5
    Rep Power
    0

    Default Re: Cant find the problem with this short code.

    Ok, well the API for the pack() method says:
    Causes this Window to be sized to fit the preferred size and layouts of its subcomponents. If the window and/or its owner are not yet displayable, both are made displayable before calculating the preferred size. The Window will be validated after the preferredSize is calculated.
    For debugging sake, print out the preferredSize of the label before and after you pack.

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

    Default Re: Cant find the problem with this short code.

    I wonder if you're actually reading in the image, if you're looking in the right place for it. To see where Java is looking, add this line to your code:

    Java Code:
    System.out.println(System.getProperty("user.dir"));
    If I typed this correctly, this should show you what directory Java is using to find your image. If it's not where the image is located, you may need to change the String that you're passing into the ImageIcon constructor to one that shows the image file name, and the path relative to where Java is looking. Otherwise, your code looks like it should work, and so if this doesn't help, I'm going to be scratching my head. Please let me know one way or the other.

    Welcome to the java-forums.org by the way!
    Last edited by Fubarable; 05-25-2012 at 11:45 PM.

  6. #6
    MarioBrawler is offline Member
    Join Date
    May 2012
    Posts
    18
    Rep Power
    0

    Default Re: Cant find the problem with this short code.

    Quote Originally Posted by Fubarable View Post
    I wonder if you're actually reading in the image, if you're looking in the right place for it. To see where Java is looking, add this line to your code:

    Java Code:
    System.out.println(System.getProperty("user.dir"));
    If I typed this correctly, this should show you what directory Java is using to find your image. If it's not where the image is located, you may need to change the String that you're passing into the ImageIcon constructor to one that shows the image file name, and the path relative to where Java is looking. Otherwise, your code looks like it should work, and so if this doesn't help, I'm going to be scratching my head. Please let me know one way or the other.

    Welcome to the java-forums.org by the way!
    Thanks so much, I know where to put my image now :). So If I want to use an image from the internet(URL), what would I need to change?

    And thanks for the greeting :D Pleasure to be here ^.^

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

    Default Re: Cant find the problem with this short code.

    Quote Originally Posted by MarioBrawler View Post
    Thanks so much, I know where to put my image now :).
    You may not want to move the image, but again and instead either use a fully qualified file path name, for example one starting from "C:/....", or a file path relative to the user directory, the one I showed you how to determine in my answer above.

    So If I want to use an image from the internet(URL), what would I need to change?
    You'd want to get the image from a URL, not a file. Please note that I don't use the ImageIcon constructor that takes a String parameter, but instead recommend that you get the image first using ImageIO.read(...). This will accept either a File or URL. Then I recommend you use the ImageIcon constructor that will accept this Image (BufferedImage actually).

    And thanks for the greeting :D Pleasure to be here ^.^
    You're welcome!

  8. #8
    MarioBrawler is offline Member
    Join Date
    May 2012
    Posts
    18
    Rep Power
    0

    Default Re: Cant find the problem with this short code.

    Quote Originally Posted by Fubarable View Post


    You'd want to get the image from a URL, not a file. Please note that I don't use the ImageIcon constructor that takes a String parameter, but instead recommend that you get the image first using ImageIO.read(...). This will accept either a File or URL. Then I recommend you use the ImageIcon constructor that will accept this Image (BufferedImage actually).
    I've done a bit research but seem to be having difficulties applying what you recommended to my program. I guess I'm not familiar with ImageIO.read(..) at all. I guess that means I have to learn ImageIO from scratch. For URL images, is there something else I should learn? Just in case, some of the images should be able to be dragged so action-listener or whatever should be able to be applied to it.

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

    Default Re: Cant find the problem with this short code.

    Quote Originally Posted by MarioBrawler View Post
    I've done a bit research but seem to be having difficulties applying what you recommended to my program. I guess I'm not familiar with ImageIO.read(..) at all. I guess that means I have to learn ImageIO from scratch.
    The first place you should start is have a look at the ImageIO API which lists all the public static methods that are available for this class as well as the important details associated with these methods.

    For URL images, is there something else I should learn?
    You will also want to look at the URL class API for information on its constructors, methods, and fields. Note that you first create a URL object, and then with ImageIO, use this to create a BufferedImage object. Then with the BufferedImage object, create your ImageIcon.

    Just in case, some of the images should be able to be dragged so action-listener or whatever should be able to be applied to it.
    Once you've created your Image or your ImageIcon, you would handle it just like any other Image or ImageIcon. It matters nothing at that point whether it is from a file, a class resource, or a URL.

Similar Threads

  1. Find the Error in Class Code
    By Humphrey Bogart in forum New To Java
    Replies: 3
    Last Post: 03-30-2011, 04:43 PM
  2. Replies: 3
    Last Post: 02-20-2011, 12:15 PM
  3. Help with my code to find MinOfAll numbers
    By May.ver.rick in forum New To Java
    Replies: 9
    Last Post: 04-20-2010, 04:46 AM
  4. Need help with short problem
    By fataguila in forum New To Java
    Replies: 1
    Last Post: 01-30-2010, 08:58 AM
  5. how to find if the code contains deprecated API?
    By Pooja Deshpande in forum Advanced Java
    Replies: 3
    Last Post: 05-29-2008, 09:12 AM

Posting Permissions

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