Results 1 to 6 of 6
- 01-25-2013, 11:14 PM #1
Member
- Join Date
- Sep 2012
- Posts
- 44
- Rep Power
- 0
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.
-
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.
- 01-26-2013, 12:03 AM #3
Member
- Join Date
- Sep 2012
- Posts
- 44
- Rep Power
- 0
Re: Display images in JFrame - Proper method
Sorry about that.
Currently, I am loading my images in this manner.
and then from there adjusting where I want it located *(I have been using a null layout)Java Code:test_Image = new ImageIcon(getClass().getResource("file.jpg"); fileLabel = new JLabel(test_Image);
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.
-
Re: Display images in JFrame - Proper method
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.
Most Swing experts will advise you to avoid using null layout if at all possible.... and then from there adjusting where I want it located *(I have been using a null layout)
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....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.
- 01-26-2013, 12:18 AM #5
Member
- Join Date
- Sep 2012
- Posts
- 44
- Rep Power
- 0
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.
-
Re: Display images in JFrame - Proper method
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.
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.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.
Not sure I get this, but it may not matter.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.
Learn them both since they both have their uses.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.
Similar Threads
-
Render Gray Colors to PDF for Proper Printing & Embedded EMF Images
By sherazam in forum Java SoftwareReplies: 0Last Post: 06-12-2012, 09:53 AM -
Not able to place image in Jframe in proper manner
By rahul405804 in forum AWT / SwingReplies: 1Last Post: 05-07-2012, 08:38 PM -
Can someone please explain to me the proper way to display the result of a SQL query
By Turk80 in forum New To JavaReplies: 1Last Post: 04-11-2011, 01:39 AM -
"java -version" doesn't display proper value.
By goldest in forum New To JavaReplies: 8Last Post: 11-06-2009, 09:30 AM -
Need help. Method won't returning proper value..
By zlwilly in forum New To JavaReplies: 2Last Post: 12-02-2008, 09:44 PM


1Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks