Regarding:
getGraphics().drawImage(t.getImage().getImage(), i * 16, j * 16, this);
You should never draw with a Graphics object obtained by calling getGraphics()
on a component as the Graphics object thus obtained will be short-lived, and your drawing can become unstable. To see what I mean, try using your technique, and then after your GUI has been created, minimize and restore it, and you'll likely see a portion or all of your image has disappeared. Instead you should either draw inside of the JPanel (or other JComponent-derived class)'s paintComponent(...)
method using the Graphics object given you.
Also don't read in the image every time you want to draw it as doing this causes an unnecessary slowing of your program. Instead, if the images aren't huge, read them in once at program start up, put them in a BufferedImage or ImageIcon variable (depending on the need), and use them when and where needed in the application.
Regarding:
JLabel tile = new JLabel(t.getImage());
tile.setBounds(i * 16, j * 16, t.getImage().getIconWidth(), t.getImage().getIconHeight());
add(tile);
This is fine except for the setBounds(...)
part which suggests that the program is not using layout managers appropriately. Let the JLabel and its ImageIcon set its own preferred size and let the layout managers use this when setting out components and sizing the GUI.
Note that you can give JLabel's layout managers and add components to them just as if they were a JPanel. The main difference is that JLabels are not opaque by default.
Regarding which is better, using a JLabel to hold the image or drawing in the paintComponent(...)
: often it depends on if the image must resize to fit the component. If so, draw in the JPanel's paintComponent(...)
. Otherwise use the JLabel.
Edit
Per your comments:
Regarding part two, I am not using a layout manager.
Then your GUI's are at risk of being ugly on different OS's, and are at risk of not being very extensible. The layout managers are one of the most powerful aspects of Swing programming, and if you use them your GUI's will be much easier to code, to update, to enhance, and to work well on other systems.
Regarding part two, what Graphics object am I passing to paintComponent()?
You don't pass any Graphics object into a JPanel's paintComponent(Graphics g)
. This is a method that is called by the JVM's repaint manager at either your suggestion (by calling repaint()
) or the suggestion of the operating system.
Finally, are you saying that it doesn't really matter which of the two options I use?
No, I didn't say that. Please re-read my recommendation just above this edit.
Edit 2
Key Links:
Edit 3
For a tile map, I'd use ImageIcons for my tile images, and then would have them displayed within a grid of JLabels. This way, swapping tiles would be as trivial as calling setIcon(nextIcon)
on the JLabel of interest.
For example, please see TrashGod and my answers to this question: jlabel images array.
Regarding:
As far as the layout manager, I have a good reason. I've made many GUIs with layout managers, but this one is special ;).
Do so at your own risk, and I'm willing to wager a beer that your reasoning for this is incorrect.
Edit 4
The reason why I am not using a layout manager is because I am trying to make a small RPG game in Swing.
Then the tiles would be best held in a container that uses a GridLayout, and that container could possibly be held in a JScrollPane so that you could scroll in whichever direction. You could then either have your sprites sit in the JLabels (by giving them an appropriate layout), or on top by using a JLayeredPane.
I don't want any comments telling me I can't or I shouldn't because if it is impossible, I'll find out on my own.
- We're not here to tell you that you can't do anything, of course, but otherwise you can't stipulate what we should or shouldn't tell you. You've come here asking for our advice, and we have an obligation to tell you what we think would work best. You have a right to follow our advice or not since it's your program, but again, you can't stipulate what we can or can't tell you, other than that you should not tolerate or allow rude or offensive statements (flag the moderators if you see this, and they'll take care of it).