Comments Off on Java Swing Gui Game: Towers Of Hanoi
The Java Associate course is fantastic, and I’m getting my head around OO programming at last. We have completed two team projects to date, Hangman Gui, and Towers of Hanoi. This post is about the Tower project, my involvement, experience and review of the project and processes. To start with, below is the project requirements.
  • Title: Group Project 2: Tower Of Hanoi
  • Goal: Create design and test a game. The game is Tower of Hanoi.
  • Requirements: Three difficulty levels. 1. 4 disks 2. 6 disks 3. 8 disks.
  • Game should use GUI
  • Project includes: UML diagrams, Use Case Studies, Unit Testing and a Reveiw.
  • Requirements, Design, Implementation and Verification must be clearly shown.
  • A log is to be kept showing who worked on each part of the project
  • Start: 27.08.12
  • Deadline: 07.09.12

There were three people on each team, and as soon as the random generator pulled our numbers out we got cracking. As my two team mates had previous programming experience they wanted to get started on the logic. I took on the Gui as my main responsibility, and also volunteered to do the UML and Use Case diagrams, as well as the project progress/tracking document, which I setup and shared with the team and tutor in Google docs.

After a meeting to discuss our initial ideas on classes and so on, I spent about a day on the UML and Use Case. I worked out both on paper, then found suitable software to create soft copy versions in. I could have managed them in less time, but there was a bit of a learning curve with the software, particularly Enterprise Architect (trial version). Below are the files produced:

Simple Use Case:

TowersofHanoiUseCaseDiagram
Initial UML diagram:

towers-of-hanoi-original-uml

Using the NetBeans JFrame Gui was interesting, with advantages and disadvantages. I’d used wysiwyg editors before in web development, and long ago came to the conclusion it was better to write XHTML tags and CSS in the text editor. Suddenly here I was faced with a similar sort of wysiwyg editor for Java. It was a bit like travelling back in time to the early 2000’s of web development. Certainly this was the case when dragging and dropping Gui items such as JPanels and JLabels. It was like the making websites in table layout all over again.

I had designed the layout on paper, and identified it’s main labels and panels etc. Below is the first swing Gui version of the layout:

Layout-Design

After a while I got the swing of it (no pun intended) and began to master JLayeredPanes and Button Groups and so on. I enjoyed exploring the properties for each element, setting icons and replacing button visuals with my own graphics for mouse events. Check out the first visually rich version, buttons not included in this screen grab  (I created all the graphics, myself):

TowersOfHanoiDesign.1

One of the main drawbacks of using swing Gui drag and drop is the code it generates. The methods are all set to private and can’t be moved up or down in the code. This did lead to some frustration but considering the software is free, I can’t complain too much! Another difficulty was working with two other people trying to integrate code with the Gui. In the end, one of my team rebuilt the Gui code in classes, which meant I lost access to the wysiwyg. Not such a bad thing as I now had a chance to get more familiar with creating and adding elements to panels and coloring, position and setting icons by hand.

See a more advanced version below, complete with difficulty options, custom start button, labels and text fields for output and prompts:

towersofhanoi

What you can’t hear here is the clap of thunder (.wav file) that runs when the player clicks Start. Also there is a looped jungle soundtrack running in the background. The sound methods are in a seperate class file I created with two methods, playSound and playSoundLoop (that calls playSound() and added an infinite loop to the clip). When a block is released on a tower it makes a clicking sound, and when the player wins, there is another crack of thunder, and an animated monkey appears and runs left and right on the platform, pointing to the tower. The butterflies to the left gently fly around in an animated loop during the game.

The tower stands and blocks are java graphics, and while one of our team finally figured out how to dress the blocks with .png image files that I created, the time was too short to refactor the code to integrate them into the game.  Below is the stack of disks in a tower.

disks-all

Conclusion:

I really enjoyed this team project, and how we developed as a team during the process. We had several productive discussions about the project scope and progress, and as time passed we worked increasingly well together. The project fully met the client outline requirements, and all source files and working .jar file were delivered within the deadline.

It was great to be able to combine my multimedia skills with Java to produce a rich looking front end for this project. My experience gave me increased skills in UML, Use Case design, team dynamics, swing Gui and Java programming. I can make the Google doc tracking/planning doc available to HR and IT managers upon request (please email me your email address – peterjmccormack@gmail.com).

Comments are closed.