Best place to start java2d game

I’ve had a lot of experience with java, and have even started working with 3D game development with jPCT, but I have a new game idea that I want to use the built-in java2d functions for. I don’t have any experience with this library yet, and I’m getting lost with all these things like frame and canvas and component and awt vs swing. Here’s my question: where’s the best place to begin to start getting images to appear in a window? Once I have that, I can use my sprite animations (that I have already drawn) to animate the game.

Any help is appreciated.

Welcome to JGO!

This forum is littered with examples of how to use JFrame + Canvas + BufferStrategy. Here is a Google-Fu search trick for all related posts.

You load images using, and you get the URL of a file using .class.getClassLoader().getResource(String).

After getting a BufferedImage from ImageIO, you should make them compatible with the graphics mode (look at line 102).

Finally, you should then learn how to use Graphics2D (what use you to draw everything) using the official Sun (Oracle now) tutorials.

Here’s an example of my game loop.

Good luck!! ;D

Well, just to throw this out there…Java2d isn’t all that great. Although, you can use it if you really want too.

The easiest set up I would to use, was to initialize a Jframe, then using simple double buffering along with graphics2d.
So, initialize the game window like this, usually these are in constructors of your core class:

JFrame container = new JFrame("Your Game"); //This creates the frame object, and sets the window name.
		JPanel panel = (JPanel) container.getContentPane(); //This initializes the panel, which in our case is just for setting the size of the window..
		panel.setPreferredSize(new Dimension(800, 600)); //Dimensions
		setBounds(0,0,800+16,600+16); //Window bounds, how big the drawing part is, essentially.
		panel.add(this); //Then, implement it.
		setIgnoreRepaint(true); //So we can make our own double buffer to refresh the screen.
		container.pack(); //Pack the container so that it's ready to use, essentially. 
		container.setResizable(false); //Set the window to not be resizable.
		container.setVisible(true); //Make the window visible.

		createBufferStrategy(2); //Make your buffer strategy.
		strategy = getBufferStrategy();
		container.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //Make sure the container closes the window when you're done. 

Then, make your game loop method.

while (gameRunning){ //Boolean to tell the game it's running, and basically makes the infinite loop of the game.
			//The Buffer.
			Graphics2D g = (Graphics2D) strategy.getDrawGraphics();
			g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); //Smoothing of text and what-not. Optional.
			g.setColor(; //The color of the buffer.
			g.fillRect(0,0,800+16,600+16); //Painting a rectangle over the last frame, so that the new one can be painted without the old one still being there to look bad. 
			//Do stuff here. Like drawing, and logic. Make sure they're separated in their own respect though.
			//Flip the buffer.
			try{ Thread.sleep(30); } catch(Exception e){
				//Put your handling here.

Now that it’s all set up, you can start the game from your main class, with something like this:

public static void main(String args[]){
		gameRunning = true;

That’s a start, feel free to ask more questions. I’ll help to my best extent :slight_smile:

I really don’t see any benefit in learning Java2D for game development. Just go straight to LWJGL since you’ll probably end up using it anyways.

I’d personally suggest just sticking it out and learning OpenGL. It’s hard to grasp at first, but then it gets eaiser – especially for 2D. If you can’t bear the steep learning curve, use a high-level library like Slick2D/LibGDX.

I’m using Java2D because the game I’m making is meant to feel like a retro game. I could take the time to learn the already complicated OpenGL functions or try some different api, but what I’m going to be doing isn’t that complicated, and having experience with awt/swing will help me in the long run. I may eventually port over to an OpenGL api, but I see no reason to use it quite yet.

I don’t agree. Java2D will be easier to start. Graphics has friendly method more than that static GL.

looks like you summarize what you have posted.

If you want Graphics-like API, just use Slick. It’s no more difficult than programming your own game loop, dealing with repainting events, etc.

IMO the years I spent programming Java2D games would have been better spent programming OpenGL-based games, even if I was using a high-level wrapper like Slick.

Slick is better example than lwjgl. Libgdx is also friendly but it’s kinda different from java2d’s pattern.