Getting Started with Java 2D

Hey guys new reg here. Im a Computer Applications student who will have a lot of time on his hands with the summer fast approaching. Plan to set a few goals for myself during the summer to keep me busy and thought id try program a simple 2D Sports game.

Anyway that said i want to know the boards opinion of where to start learning with Java2D? Is there a preferred IDE or is it the usual use whatever your comfortable with?

Any info on where to get started would be much appreciated ;D

Thanks.

Use eclipse and start with slick2d. Java2D is too restricted in my opion. There is good tutorials for slick.

I recommend Eclipse and start by learning Java2D from the Official Sun Java2D tutorials. After you have gotten the grasp of it and made a couple of games, it is recommended to move to OpenGL by using LWJGL. Slick2D is a library that makes it easier to use LWJGL and has an API that is close to Java2D.

Slick2d is good becouse it handles the game loop. You start from right things and don’t learn bad ways. If you start with java2d then search good and ready game loop for games. In this forum there is couple ones.

Here’s a good on on Game Loops. I use the last one.

Cheers for the input guys. Will have things set in the motion come the end of this week :smiley:

Glad to help :smiley:

For the game loops, I recommend the time step loop. Here is my implementation:


final int FPS = 60;

int currentFPS = 0;
int frames = 0;
long time = System.nanoTime();
long lastTime = System.nanoTime();

while(isRunning) {
    long now = System.nanoTime();
    
    long diffTime = now-lastTime;
    
    //update loop. to prevent certain problems when deltaTime is a large number, max deltaTime is 1000000000/FPS
    while(diffTime > 0) {
        long rem = diffTime%(1000000000/FPS);
        
        long deltaTime = (rem == 0 ? 1000000000/FPS : rem);
        
        update(deltaTime);
        
        diffTime -= deltaTime;
    }
    
    lastTime = now;
    
    //to calculate FPS
    if(System.nanoTime()-time >= 1000000000) {
        time = System.nanoTime();
        currentFPS = frames;
        frames = 0;
    }
    
    //render graphics
    render();
    
    //sleeping in an active loop until the desired sleep time has passed
    try {
        if(FPS > 0) {
            long sleepTime = Math.round((1000000000.0/FPS)-(System.nanoTime()-lastTime));

            if(sleepTime <= 0)
                continue;

            long prevTime = System.nanoTime();
            while(System.nanoTime()-prevTime <= sleepTime) {
                Thread.yield();
                Thread.sleep(1);
            }
        }
    }
    catch(Exception exc) {
        exc.printStackTrace();
    }
}

EDIT: If you are going to use BufferStrategy for drawing on a Canvas, here is the recommended render() method:


public void render() {
    try {
        do {
            do {
                Graphics2D g = (Graphics2D)strategy.getDrawGraphics();
                
                //draw your game

                g.dispose();
            }while(strategy.contentsRestored());

            strategy.show();
        }while(strategy.contentsLost());
    }
    catch(Exception exc) {
        exc.printStackTrace();
    }
}