Home » Newcomers » Newcomers » Displaying Tiles and Efficient array population
Displaying Tiles and Efficient array population [message #757642] |
Mon, 21 November 2011 00:20 |
itisme1997 Messages: 2 Registered: November 2011 |
Junior Member |
|
|
I am working on a program that I would hope to use in a top down style RPG. For the purposes of example, I have re-purposed the mayday texture pack as a sample. My program so far displays one row of 15 images. The only problem is that, each tile being 15 pixels, populating the array I am using would take forever manually, not considering the fact that people have many different resolutions. My code looks like this so far:
public class Map {
private JFrame frame;
/**
* Launch the application.
*/
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
Map window = new Map();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
/**
* Create the application.
*/
public Map() {
initialize();
}
/**
* Initialize the contents of the frame.
*/
private void initialize() {
frame = new JFrame();
frame.setBounds(100, 100, 211, 114);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.getContentPane().setLayout(new GridLayout(0, 15, 0, 0));
JPanel panel = new JPanel();
frame.getContentPane().add(panel);
JPanel panel_1 = new JPanel();
frame.getContentPane().add(panel_1);
JPanel panel_2 = new JPanel();
frame.getContentPane().add(panel_2);
//and so on and so forth
//real code
JPanel[] array = {panel, panel_1, panel_2, panel_3, panel_4, panel_5, panel_6, panel_7, panel_8, panel_9, panel_10, panel_11, panel_12, panel_13, panel_14};
int[] data = {1,1,1,2,2,2,3,3,3,4,4,4,5,5,5};
int i;
BufferedImage myPicture = null;
BufferedImage tile = null;
try {
myPicture = ImageIO.read(new File("mayday.png"));
} catch (IOException e) {
e.printStackTrace();
}
for(i=0;i<data.length;i++) {
switch (data[i]) {
case 1: tile = myPicture.getSubimage(48, 0, 15, 15); break;
case 2: tile = myPicture.getSubimage(112, 0, 15, 15); break;
case 3: tile = myPicture.getSubimage(32, 16, 15, 15); break;
case 4: tile = myPicture.getSubimage(81, 33, 15, 15); break;
case 5: tile = myPicture.getSubimage(128, 0, 15, 15); break;
}
array[i].add(new JLabel(new ImageIcon( tile) ));
}
}
}
How do I elegantly populate the JPanel array? Any other criticism welcome, as I am new to Swing and Java in general.
[Updated on: Mon, 21 November 2011 00:29] Report message to a moderator
|
|
| | |
Goto Forum:
Current Time: Fri Mar 29 09:27:16 GMT 2024
Powered by FUDForum. Page generated in 0.04173 seconds
|