Skip to Main Content

Java SE (Java Platform, Standard Edition)

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Fastest and simplest way to display a 2D grid in Swing, using images?

843807Mar 14 2010 — edited Mar 14 2010
Hi,

I'm trying to draw a game-board in Swing. I've already figured out how to draw it in my teachers turtle-graphics thing, but I want to make my game a little prettier.
I've managed to draw the entire GUI in Swing, and it has full functionality, I'm just missing the "drawMap()" method that should display the game in the level-JPanel.
One of my friends at school already managed to do this, but he used -400- lines of code and two objects/classes. I hope there is a better way.
The turtle version of my code is like this:
	void turtle() {
		int width = game.getGrid()[0].length;
		int height = game.getGrid().length;
		STG.home(true);
		String url = null; 
		STG.up();
		for (int y = 0; y < game.getGrid().length; y++) {
			for (int x = 0; x < game.getGrid()[y].length; x++) {
				switch (game.getGrid()[y][x]) {
				case WALL:
					url = "/sokoban/icons/wall.png";			
					break;
				case EMPTY: 
					url = (game.getTargets()[y][x]) ? "/sokoban/icons/target.png" : "/sokoban/icons/tile.png"; // url = (boolean) ? iftrue dothis : elseiffalse dothis
					break;
				case MOVER:
					url = (!game.getWinner()) ? "/sokoban/icons/mover.png" : "/sokoban/icons/happyMover.png";					
					break;
				case CRATE:	
					url = (!game.getTargets()[y][x]) ? "/sokoban/icons/crate.png" : "/sokoban/icons/crateOnTarget.png";
					break;
				default: 
					break;
				} 
				STG.image(url, 30, 30, (width/2-x)+0.40, -height/2+y);
			}
		}
I'm pretty sure there has to be a similar way of doing this in Swing, without using hundreds of lines of code.
Here's a screen-shot so there's absolutely no confusion about what I'd like to achieve:
[http://img198.imageshack.us/img198/2815/40317025.png]
I want to draw the game-board in the JPanel titled 'Level: 1'.
Please point me in the right direction =)
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Apr 11 2010
Added on Mar 14 2010
6 comments
1,683 views