Subscribe to Six's Game Programming Adventures        RSS Feed

Basic Java Tile Engine

Icon 1 Comments
Iíve been continuing with my exploration of Java and game programming. I like Java a lot, though it will never replace my love of C# and XNA when it comes to game programming. I still get stuck in C# thinking at times and dialect. My current adventure is creating a tile engine with Java. Iím still not happy with using a Timer to control my games but I moved to using Timer. I still draw to a back buffer and flip the back buffer to the window when it comes to rendering.

First challenge was deciding if I wanted to use a tile set or multiple tiles. I decided to go with a tile set. The next step was loading in the image. I experimented with Eclipse, I moved to Eclipse from NetBeans, and was able to add images to the project easily enough. I was then able to read the image in at runtime. Thanks to Dogstopper for his tutorial in the Game Programming Tutorials section on creating Tic-Tac-Toe because it showed me how to read in images and prepare them for drawing. I had my tile image for rendering.

Next challenge was creating a map. Here Java is a little different from what Iím used to. Rather than the multidimensional arrays that Iím used to in C# Java has arrays of arrays. In the end it was just a matter of getting used to Javaís way of doing it.

Now that I had my image and my map it was time to get down to rendering a map. Looking up the drawImage method of the Graphics class I found an overload that had a destination and source rectangle, though it wasnít really a rectangle object. I tried that overload and got my map rendering. I did hit a snag in my design that I will fix in the future. I needed to define the source rectangles for my tiles. If I tried to use a source other than the first rectangle the results were unpredictable.

The map was rendering nicely enough it just sat there, pretty boring. I did a little research and figured out how to check if keys were down. I plugged that into the tile engine and I got a scrolling map. Good enough for now but it would scroll past the edges of the map. Again, that will be something to fix in the next adventure. The neat part is that I had a tile engine rendering and scrolling using Java. Pretty cool I thought to myself. B)

As always, the code for this adventure.

1 Comments On This Entry

Page 1 of 1


12 January 2012 - 03:48 PM
I don't know if this affects your particular code, but I recently read that Swing components are double buffered by default, so you might be triple buffering (which could cause loss of performance). I'm a Java novice myself so don't assume that's the case but it might be worth delving into if the game becomes very expansive.
Page 1 of 1

June 2020

 123 4 56

Recent Entries

Recent Comments

Search My Blog

0 user(s) viewing

0 Guests
0 member(s)
0 anonymous member(s)