New to game programming

Hey everyone, im a second year computer science major and im interested in beginning game development. My university offers a concentration in game design to computer science majors and I would like to try and make a game before I decide if its for me or not.

I am pretty fluent in java at this point, so I might as well use java to start out with. What I lack is the knowledge of API’s for game development. What I would like to get from you all here is: where do I start? I’d just like some broad advice as to where to concentrate my efforts as a novice game programmer.

Just decide if you’d like to go 2D or 3D then pick one from here.

Decide too what library you will use :wink:

My first post :D.
For simple 2D games, graphically AWT and Swing is all you’re gonna need. Swing to have the main window(jpanel) to draw in, AWT for manipulating transforms and drawing sprites.
http://download.oracle.com/javase/6/docs/api/

There is no point in drawing in JPanel. JComponent is suggested for very simple games, Canvas is otherwise recommended.

I prefer canvas if it still on java2d. Double buffered and I feel it (in my old PC) pretty fast.

I picked up Killer Game Programming in Java a day ago and have been reading it. KGPJ draws on a JPanel by drawing to an image then drawing the image to the JPanel (Double buffering I think?) from what I have read so far the book seems to be a little old. So is there a better way of drawing for a 2D game?

Drawing on an image, then on a JPanel?!? Now that is not efficient at all since a JPanel is already double-buffered. However, to get you used to this habit, start using a Canvas. If this is going to be a desktop app, then there is no need to extend anything. All you have to do is create a Canvas object, add it to the JFrame, and to double buffer, do the following:


//instance variables
Canvas canvas;
BufferStrategy strategy;

//init
canvas = (Canvas)jframe.add(new Canvas());
canvas.createBufferStrategy(2);
strategy = canvas.getBufferStrategy();

//inside game loop
do{
    do{
        Graphics2D g = (Graphics2D)strategy.getDrawGraphics();
        
        //drawing code
        
        g.dispose();
    }while(strategy.contentsRestored());
    
    strategy.show();
}while(strategy.contentsLost());

It’s my first time I see this

canvas = (Canvas)jframe.add(new Canvas());

any different from

canvas = new Canvas();
jframe.add(canvas);

??

Both are the same. The add() method returns the same object you added.

EDIT: Fixed my code a little: strategy.contentsRestored() is supposed to be inner do while loop :stuck_out_tongue:

I just know that :slight_smile: thanks

Glad to help :slight_smile:

EDIT: I just realized I asked the first question about KGPJ :persecutioncomplex: I guess I didn’t understand the answers. I also feel stupid for not realizing that until now ;D

I’m not sure if I should create a new thread for this… but it kind of relates to a question already asked here. I too picked up Killer Game Programming in Java (KGPJ) and used it’s “double buffering” strategy. Now I am trying to convert it’s double buffering strategy which is: “Have a global Graphics object and a global Image. Have a method called renderGame() that gets a Graphics object from the global image and does all the game drawing on the that Graphics object. Then there is a method called paintGame() which draws the Graphics object from the Image onto the Graphics object of a JPanel (I use a JComponent). So drawing on an image then drawing the image to the screen.” My problem is I am having trouble converting this to the suggested Canvas method. Does anyone have tips? One of the problems that comes to my mind is that I handle input through the JComponent with a MouseAdapter and a KeyAdapter, can the same thing be done with a Canvas?

I had left canvas long time so see if I can recall…
first, you maybe confused how to get the big image (for your draw) from canvas right?


Graphics2D g = (Graphics2D) strategy.getDrawGraphics();
//g.drawAnything
g.dispose();
strategy.show();

you get “strategy” object from getBufferStrategy();. Second, canvas can work with those adapters.

Thanks that helped, I was a little confused by the do-while part, your explanation helped. It’s also nice to know that I can use the adapters.

Hello,
I have a question to the above posted code.

}while(strategy.contentsRestored());

For what does this loop wait?

biro

@biro @Z-man
You can read about the methods here

The do-while part is necessary for when the image gets lost and/or reinitialized (because it uses VolatileImage).

So for a 2D game in java would you recommend extending Canvas and controlling the looping, drawing and logic there. Then have a main method somewhere create a JFrame and add your custom Canvas to it. Or is there another method you would suggest.

That’s my recommended method :slight_smile:

@Z-Man
I use that too :smiley:
you can also have a canvas -> JPanel -> JFrame too so you can set the preferred size.