Results 1 to 6 of 6
Like Tree1Likes
  • 1 Post By Fubarable

Thread: Display images in JFrame - Proper method

  1. #1
    rru96 is offline Member
    Join Date
    Sep 2012
    Posts
    97
    Rep Power
    0

    Default Display images in JFrame - Proper method

    So, I have been starting to mess around a little more in Swing, and am starting to feel a little more comfortable with loading images, using ActionListeners, MouseListeners,... so on my first go through of videos, I decided to watch Bucky's Java game tutorial. I am trying to understand, is this still correct, or should I be loading images into JLabels like I am doing now?

    https://www.youtube.com/watch?NR=1&v...ture=endscreen - Reference video


    Any clarification would be great.

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

    Default Re: Display images in JFrame - Proper method

    Consider posting all relevant information in the forum. Many will not want to view the video and so you'll increase your chances of getting good help if you do this.

  3. #3
    rru96 is offline Member
    Join Date
    Sep 2012
    Posts
    97
    Rep Power
    0

    Default Re: Display images in JFrame - Proper method

    Sorry about that.

    Currently, I am loading my images in this manner.
    Java Code:
    test_Image = new ImageIcon(getClass().getResource("file.jpg");
    fileLabel = new JLabel(test_Image);
    and then from there adjusting where I want it located *(I have been using a null layout)

    But in the above video, Bucky loads the images via ImageIcon (this I follow), but then he calls a paint() or repaint() method to display the images onto the screen without loading into a JLabel. Is this method A)outdated (as the video is from 2009) B) the AWT method vs. SWING, C) this is a more stable way for his project (Game building) versus my project... simple app, no threading.

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

    Default Re: Display images in JFrame - Proper method

    Quote Originally Posted by rru96 View Post
    Sorry about that.

    Currently, I am loading my images in this manner.
    Java Code:
    test_Image = new ImageIcon(getClass().getResource("file.jpg");
    fileLabel = new JLabel(test_Image);
    On brief view, it looks like that code won't compile as it's missing an end parenthesis. Otherwise if it works for you, I see no problem with it.

    ... and then from there adjusting where I want it located *(I have been using a null layout)
    Most Swing experts will advise you to avoid using null layout if at all possible.

    ...But in the above video, Bucky loads the images via ImageIcon (this I follow), but then he calls a paint() or repaint() method to display the images onto the screen without loading into a JLabel. Is this method A)outdated (as the video is from 2009) B) the AWT method vs. SWING, C) this is a more stable way for his project (Game building) versus my project... simple app, no threading.
    It's hard to say anything specific about what Bucky is doing without seeing his code, but I'm guessing that he's drawing the image in a JComponent's paintComponent(...) method, and if so, this is another fine way for displaying an image. The two approaches have their pluses and minuses, but both are used. The repaint() method will ask the GUI toolkit's paint manager to re-draw the component being repainted. I would hope that the video doesn't have you call paint(...) directly or override the paint(...) method.

  5. #5
    rru96 is offline Member
    Join Date
    Sep 2012
    Posts
    97
    Rep Power
    0

    Default Re: Display images in JFrame - Proper method

    Thanks for clarifying that up some.

    code was a quick reference to my method of adding images, not intended to be completely working. Sorry.

    As for using the null layout, while I have been teaching myself, whenever I get to a block, I do what I can to keep going. Otherwise I will give up on this whole coding idea of mine. I tried to get the GridBagLayout working as I learned if you can use this, then you can use any. Well, I got stuck, and learned about null. I do plan on going back and learning at LEAST GBL, if not all, but for now I wanted to keep trucking.

    For the override, no. Bucky explained it like... When you call JFrame, it initially calls a paint() method though it isn't seen by the programmer. He just recalled it once he loaded the images. Think I understood that correctly.



    Thanks again. I think I am going to learn his method as he then creates objects out of the images he loads and puts them into animations. Seems his method might be cleaner than me stuffing ImageIcon's in JLabels.

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

    Default Re: Display images in JFrame - Proper method

    Quote Originally Posted by rru96 View Post
    ...code was a quick reference to my method of adding images, not intended to be completely working. Sorry.
    Most of us here have been beaten by the compiler into submission to be as precise as possible when creating or posting code, since the compiler is unforgiving. Perhaps that is why it raises the hackles on the back of my neck to see small but pertinent errors in posted code. Since I'm guessing that others experience the same effect, and since you will want to try to be as precise as possible with your dealings with the compiler, you will want to be careful when posting code here that it be as accurate as possible.

    As for using the null layout, while I have been teaching myself, whenever I get to a block, I do what I can to keep going. Otherwise I will give up on this whole coding idea of mine. I tried to get the GridBagLayout working as I learned if you can use this, then you can use any. Well, I got stuck, and learned about null. I do plan on going back and learning at LEAST GBL, if not all, but for now I wanted to keep trucking.
    You've got it backwards. You should learn and use the simpler layouts first, FlowLayout, GridLayout, BorderLayout and BoxLayout, and save GridBagLayout for limited use. Most complex GUI's can be most easily made by nesting JPanels, each using one of the more simple layouts. You will find that as you build on to your GUI's that you will hamper your ability to do this if you use null layout since when doing so, if you need to add anything into the middle of an existing GUI, you will be responsible for shifting all components below and to the right of it -- not fun, and very prone to error.

    For the override, no. Bucky explained it like... When you call JFrame, it initially calls a paint() method though it isn't seen by the programmer. He just recalled it once he loaded the images. Think I understood that correctly.
    Not sure I get this, but it may not matter.

    Thanks again. I think I am going to learn his method as he then creates objects out of the images he loads and puts them into animations. Seems his method might be cleaner than me stuffing ImageIcon's in JLabels.
    Learn them both since they both have their uses.
    rru96 likes this.

Similar Threads

  1. Replies: 0
    Last Post: 06-12-2012, 09:53 AM
  2. Not able to place image in Jframe in proper manner
    By rahul405804 in forum AWT / Swing
    Replies: 1
    Last Post: 05-07-2012, 08:38 PM
  3. Replies: 1
    Last Post: 04-11-2011, 01:39 AM
  4. "java -version" doesn't display proper value.
    By goldest in forum New To Java
    Replies: 8
    Last Post: 11-06-2009, 09:30 AM
  5. Need help. Method won't returning proper value..
    By zlwilly in forum New To Java
    Replies: 2
    Last Post: 12-02-2008, 09:44 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
  •