Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Newcomers » Newcomers » Displaying Tiles and Efficient array population
Displaying Tiles and Efficient array population [message #757642] Mon, 21 November 2011 00:20 Go to next message
itisme1997 is currently offline itisme1997Friend
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

Re: Displaying Tiles and Efficient array population [message #758572 is a reply to message #757642] Wed, 23 November 2011 17:48 Go to previous messageGo to next message
Eric Rizzo is currently offline Eric RizzoFriend
Messages: 3070
Registered: July 2009
Senior Member
These forums are for discussing topics related specifically to Eclipse,
not general Java programming. You should ask general Java questions on a
general Java forum or web site, such as stackoverflow.com


On 11/20/11 7:20 PM, itisme1997 wrote:
> 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, not considering the fact that people have
> many different resolutions. My code looks like this so far:
>
Re: Displaying Tiles and Efficient array population [message #758584 is a reply to message #758572] Wed, 23 November 2011 18:46 Go to previous message
itisme1997 is currently offline itisme1997Friend
Messages: 2
Registered: November 2011
Junior Member
Thanks, kinda figured after i looked a few posts. I'll remove this.
Previous Topic:eclipse workbench help fails
Next Topic:Build hangs on synchronized fortran job
Goto Forum:
  


Current Time: Thu Mar 28 21:05:27 GMT 2024

Powered by FUDForum. Page generated in 0.03532 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top