Tuesday, August 16, 2011

How to Setup JDeveloper with Slick2D

I know we have all struggled with something like this at one point in our coding and usually the first thing I will do is head to Google to find out if anyone else has had the same problem and figured out how to fix it. This post is an explanation on how to setup a game project in JDeveloper 11G to work with the Slick2D library for your games. I have spent many hours working on this in order to make sure how it works and so that I'll be able to explain it better. Please feel free to comment or email me with questions if this is confusing and you need further help setting up your game.

In this post I will assume that you are starting off from the beginning and don't even have the JDK downloaded. 

Firstly, head over to the downloads section on the JDeveloper website and download the version of the Java Development Kit (JDK) that you wish to use. Note that JDeveloper and Slick2D work better with 32 bit versions of the JDK. I downloaded the 32 bit version of JDK 1.6.0_26 even though I have a 64 bit computer (below)



Secondly, after the JDK has downloaded, click on the .exe file (the one you downloaded) and allow it to install the JDK onto your computer. The default settings are what you want for this install, so feel free to click Next until it installs.

After the JDK finishes installing (Default location is C:\Program Files (x86)\Java), return to the JDeveloper site and download JDeveloper. The "Generic Release Java Edition" is all that is necessary unless you plan on developing enterprise websites with elaborate databases on the side (below). Note that Oracle will require you to create a FREE account in order to download JDeveloper.

After you have downloaded the .zip file from Oracle, extract it to a place on your computer (C:\oracle, for example). It is recommended you place all of the files in a location that does not contain any spaces in the path (for example "C:\oracle", not "C:\my oracle"). The next thing I recommend doing is opening the folder where you just extracted all of the contents from the .zip file to and right click on the "jdeveloper.exe" file to create either a desktop shortcut or a taskbar shortcut, as this will be a much faster way to open JDeveloper than navigating to the directory and clicking the .exe every time. 

Next, open up JDeveloper by either clicking on the jdeveloper.exe file or by clicking on the shortcut you just created. Note that JDeveloper will open a window asking you to enter the path to the JDK folder you downloaded earlier. If you installed the JDK to the default location you need to enter (or browse to) C:\Program Files (x86)\Java\jdk1.6.0_26\bin\java.exe and click OK. After this, JDeveloper may pop open a window that asks you if you want to migrate settings from a previous version of JDeveloper to this one, in which case you should click NO (because there is no other version of JDeveloper on your computer). After this, JDeveloper should open up to the start page not present any other message windows (aside from the "Tip of the day" window).

Now, let's create our first application and project. Click File > New > General > Application and enter the name of the application (SlickTest will work just fine) and click Next, then fill in the project name (project1 will do) and then click Finish. 


Next, right click on your project in the Application Navigator and click New

This will open the same window that opened when you created your application. This time, however, we are going to choose Java Class

Click OK and you will be prompted to fill in some basic info for the class (Name, package, etc.)

I recommend matching it to what I have shown above. Click OK and your new class will open in the main window of JDeveloper.

Now let's take a break from JDeveloper and head over to the Slick2D website to download the library. On the right hand side of the main page, click on the link that says "Download Full Distribution" to get a zip file that will have everything we need in order to run our Slick games. Next, I recommend navigating to your project's folder and creating a \bin, and a \lib folder for our Slick2D library items. Optionally, you can create a \content folder for all of your game's assets (images, music, fonts, etc.).

These folders will be referenced from inside JDeveloper (our project's properties, specifically) later on. Next, extract the files from the Slick2D download to a temporary place (Desktop works just fine). Once that is done, create a folder inside of the lib folder in your project and call it Slick2D (\lib\Slick2D). Place everything except for the .dll files from the download into this new folder. Now take the .dll files (four of them, to be precise) and place them inside the bin folder in your project. This is a fairly standard way of packaging your game with the library files it will need in order to be distributed to other people (and work properly).

Now, go into JDeveloper and right click your project's name and select Project Properties. This is where we will setup the Slick library in our project. Click on the Libraries and Classpath node and on the right sight of the window click on Add Library. We will then select New because JDeveloper has no idea about our Slick Library (we haven't created it yet). Now fill out the information as I have it below

It is important that you include all of the .jar files except for slick.jar from the \lib folder. Now click OK and you will return to the libraries screen where you will select Slick2D and click OK. Now you should be back at the Project Properties window and your new Slick2D library will be included in your project. Now click Add Jar/Directory and navigate back to the \lib folder and add slick.jar.

Now you are nearly done with setting up the Project Properties. From the Project Properties window, click on the + sign next to Project Source Paths and click on the Resources node. Click Add and add the \content folder you created earlier. This will allow you to begin your resources' filenames from the content folder (instead of the project folder).

Next click on the Run/Debug/Profile node and click Edit. Under the Java Options text box, add this:

-Djava.library.path=bin

This will tell JDeveloper to look in the bin folder of your project for the .dll files we put there earlier.

Now click OK until you have closed the Project Properties. Now to test your setup and create a basic Hello World game you will need to modify the Game.java file. To keep it simple, modify your Game.java class so that it looks just like this:
import org.newdawn.slick.AppGameContainer;
import org.newdawn.slick.BasicGame;
import org.newdawn.slick.GameContainer;
import org.newdawn.slick.Graphics;
import org.newdawn.slick.Image;
import org.newdawn.slick.SlickException;

public class Game extends BasicGame {

    Image background;

    public Game(String title) {
        super(title);
    }

    public void init(GameContainer container) throws SlickException {
        background = new Image("helloWorld.png");
    }

    public void update(GameContainer container, int delta) 
        throws SlickException {
    }

    public void render(GameContainer container, Graphics g) 
        throws SlickException {
        background.draw();
    }

    public static void main(String[] args) throws SlickException {
        AppGameContainer app = new AppGameContainer(new Game("Game Test"));
        app.setDisplayMode(640, 480, false);
        app.setTargetFrameRate(60);
        app.start();
    }
}

One last thing to do is create a Hello World image like this one:

and place it in the \content folder in your project.

Now click the Run button (Big Green Arrow) and with any luck, your game will start up in no time! I really hope you have learned something from this post and are able to use Slick2D in JDeveloper. Please feel free to comment or email me with any questions you may have.

1 comment:

  1. I'm creating my own reasources gathering-indie game, but i have troubles with the blocks-player collision, can you post the code of yours?

    ReplyDelete