For Java2D n00bs - Mixing Swing and a drawable canvas to make a boardgame

Hello everyone!

A friend of mine asked me how he could start programming games in Java, without having to make
his own menu-system and learn a lot about animations, before he could get started. He just wanted
to play around with something simple, like a boardgame. Nothing too crazy. Something like “Risk”.

There are many good Java tutorials out there, but few tackle the difficulties of dealing with Swing-
menus combined with a drawable canvas, and fewer actually describe and explain what they’re doing
to make it all work. It quickly becomes magic to many. I’ve tried to make a simple, thoroughly commen-
ted project, with a few helpful modules to get started with. They’re not completely optimized or state-
of-the-art, but that’s the point. I wanted to make a project a newcomer could look at, and easily learn
from and expand on.

I’ve covered the following:

  • Creating the JFrame with the correct desired size / handling insets
  • Loading images from within your Jar
  • Loading images from a folder next to your Jar
  • Setting up two menus; one to the right, and one at the bottom
  • How to fill the menus with buttons and labels where you want them
  • Comprehensive customizing of JButtons and JLabels
  • Custom cursor
  • Creating a simple canvas to draw on
  • A very simple gameloop implementation, just to explain the concept
  • Drawing strings and rectangles
  • Very simple keyboard- and mouse-implementation
  • Reacting to controls on the canvas
  • Changing stuff in the menus and on the canvas, when the canvas registers clicks or keystrokes
  • How to get error-logs when running your game as a Jar
  • Customized close-operation attached to the JFrame
  • Using HTML to add more lines in your JLabels, JButtons and tooltips

It is a zip file, which you should be able to import directly into Eclipse. It is Java 1.6.
Download Zipped Eclipse Project
Download Runnable Jar

NOTE: The project is set up, so if you swap the worldmap-image for another image, it can resize itself automatically.
The project is meant as a simple example of a layout, from which one could make a decent game,
given some time, and some activity in a Photoshop-like application. It should be pretty easy to
completely remove the existing menus and create your own playing field, but if you are a newcomer,
I’d advise you to read through everything carefully first, to ensure that you know what is going on.
For now, the mouse- and keyboard-modules aren’t commented. Big whoop. If I get around to it, I’ll
upload a new version, and update the link.

I hope this helps someone out there. Any suggestions welcome.

Sincerely, Jonas Tingmose aka. Ultroman

Screenshot:

http://img441.imageshack.us/img441/5475/boardgameexample.png

Thankyou.
Will have a closer look at this after work tonight…