Game Development Stack Exchange is a question and answer site for professional and independent game developers. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I would like to create a 2d game in Java, but tutorials online for game libraries such as LWJGL are usually filled with errors once I compile the project. Most people do not explain very well, and just go on very quickly. Can someone please provide me a couple examples with source code, and somewhere to have a starting point? I used to have trouble with making a GUI and you guys helped me through, hopefully you can do the same again!

share|improve this question

closed as off topic by Byte56, Noctrine Jul 18 '12 at 22:43

Questions on Game Development Stack Exchange are expected to relate to game development within the scope defined by the community. Consider editing the question or leaving comments for improvement if you believe the question can be reworded to fit within the scope. Read more about reopening questions here.If this question can be reworded to fit the rules in the help center, please edit the question.

Unfortunately this question is considered off topic on this site (It is very, very broad). You are welcome to discuss it in the chatroom though (Once you've hit 20 rep for the general room at least) – Noctrine Jul 18 '12 at 22:43

A very simple and effective method to write small software rendered games is to use the methods and tools swing offers. Begin a new Swing project and add a single component to the window. That is the component everything is being rendered into. It might look like this:

public class SwingRenderTarget extends JComponent {
    private final BufferedImage image;


    public void paint() {

    public void paint(Graphics g) {

        g.fillRect(0, 0, canvasWidth, canvasHeight);

        g.drawImage(image, 0, 0, canvasWidth, canvasHeight, null);

    private void swapBuffers() {
        System.arraycopy(currentPixels, 0, ((DataBufferInt)image.getRaster().getDataBuffer()).getData(), 0, gameSizeX*gameSizeY);

What it does is rendering a single BufferedImage to the screen that contains all the game data. But how to draw images to the BufferedImage? This is simple! Create an array of ints (called currentPixels in this example) and just treat every int as a single pixel of your screen! In the method swapBuffers this array of pixels is written to the previously created BufferedImage which is then rendered to screen.

How about an example how to render a simple Sprite to the screen? First of all create a Sprite class:

public interface Sprite {

    int[] getPixels();

    int getHeight();

    int getWidth();

The int array will serve as the pixels of the Sprite. You can fill the array programmatically or just load any bitmap from disk. Then when it's time to draw that Sprite just copy the pixels from the Sprite over to our pixel array in the SwingRenderTarget. Here is an example to fill the whole screen. To use smaller Sprites one has to copy one row from the Sprite pixel array to the SwingRenderTarget pixel array at a time:

public void drawFullscreenBitmap(Sprite sprite) {
    int[] data = sprite.getPixels();

    System.arraycopy(data, 0, currentPixels, 0, data.length);

Throw in a game loop that has a fixed timestep and there you go: a basic game in no time!

share|improve this answer

I would suggest using Slick 2d (Slick is built on top of the lwjgl, so you have a great community you can turn to with problems). I just did a little game with it and it worked just fine. It's really easy to grasp and provides all the groundwork, e. g., you don't have to worry about buffers and such. Just define your graphics (images) and draw them. Furthermore, Slick provides states so you can really easy develop the single parts of your game indepent from one another (main menu, options, gameplay and so on).

The states themselves provide you with two alternating methods, update() and render(), so you can divide your code effectively:

public void update(){
     // your gamelogic, such as collision-detection, controls, etc.
     // simple example for handling input:
     Input in = getInput();
public void render(){
     // draw your assets

Slick also handles all input, it even has support for controllers. On the page, there are several good tutorials for beginners, explaining how a simple game could look.

share|improve this answer

I think starting with an existing simple game, then adding to it or changing it is a good route to take. So, find a 2D opensource game to start with as a base. Perhaps you can find one that suits you on one of the game jam sites like ludum dare? I know they usually post their code for people to look at when they're done. This also makes sure that everything is included so you don't have issues with all those compile errors you were talking about.

share|improve this answer
Ludum Dare is a great source for source code to look at, but beware: since the games are written within 48 or 72 hours a lot of the code is not pretty and maybe not the best start for a beginner in the topic. – thalador Jul 18 '12 at 20:25
Thanks thalador. Good to know! I was able to learn a few things and get some basic "boiler plate" type code from there. Since people often post their "this is what I have to start with" code, which is usually everything you need to get started. – GameDev-er Jul 18 '12 at 20:29

Not the answer you're looking for? Browse other questions tagged or ask your own question.