bookmark_borderUsing Slick to write 2D Java games

I have written a few games in the past. A recent example is Dune II – The Maker. I have written a platformer (in QuickBasic even..) and so forth. You may even consider RealBot a ‘game’ (although it is more a computer controlled opponent).

I haven’t really tried to write any games in Java. Although I do program in Java quite a lot these days. Especially for the web. But it seems there is (for some time now) a game library that I might try out.

The library is called Slick and when looking at its website, forum and documentation (API and get-started) it has some promise!

I have tried setting up a simple project with maven, including slick stuff, and wrote a little engine that only draws some blocks and bounces them at the borders of the screen. So far it works great.

Although writing games now is not my priority at all, I will get back to this once I have finished my thesis about Software Quality.

In the meantime, you might want to consider looking at Slick. Give it a chance!

bookmark_borderDon’t forget your resources directory! (Maven/Eclipse/Sysdeo-tomcat)

Recently I had to set up a XMLRPC Server. Using Apache’s XMLRPC it should not be that hard to set up.

It wasn’t. But it did not work the first time.

The XMLRPC Serlvet would not be initialized properly because it could not find the XmlRpcServlet.properties file. I was a bit suprised, because I had it in my src/main/org/apache/xmlrpc/webserver path… so why did it not work?

The reason is that whenever you run your Tomcat webserver, configured to look into your project directory (using the tomcat-sysdeo maven plugin) which is set up using maven; you’ll be using your classpath as set up there. If you look into your project->build path settings (tab source), you’ll see that everything under src/main is only including **/*.java, meaning it will not find the properties file at all!

Once I moved the XmlRpcServlet.properties to src/main/resources/org/apache/xmlrpc/webserver (where it belongs), it worked fine.

So remember, classpath, classpath, classpath!

bookmark_borderEasyMock Class Extension – IllegalStateException on expect method

When I write unit tests, I find using EasyMock extremely helpful. Especially using EasyMock Class Extensions give me the ability to mock objects which do not have an interface, or objects that are so legacy (and untested) that I don’t dare to touch them yet.

I say “yet” , because once I have reduced this ‘fear to break things’ factor by writing enough tests to ensure the legacy code works as it should, I *will* touch them.

One little piece of advice when writing your test, is that the ‘expect’ method will throw an exception when you tend to do that on methods that are final.

This might give you an IllegalStateException with the message : “no last call on a mock available”.

This basically means “Help I could not mock this call, and now you want me to mock it anyway!”.

Step back a bit and think how this EasyMock Class Extension would work in order to Mock existing classes? What would you do? Yes, you would extend this class, and override the methods!

That is what the EasyMock Class Extension does. So what happens when you make a method final? You cannot override it!

If you have any IllegalStateException from the EasyMock Class Extension, check your methods you tend to ‘expect’ (override) if they are marked “final”.

Remove the “final” keyword and you will be able to continue your work.

I’ll leave the question : “Should i just remove this ‘final’ keyword?” to your own wisdom…

bookmark_borderOverriding and methods

Today I had a little challenge. I had a Class, I call it Class A. It has a method, i call it “doSomething”. Class B extends Class A, and overrides this method with a totally new behavior. Class C who extends from Class B wants to have the original behavior from Class A.

Here is a picture:

The Problem

Now, the simplest but most stupid way to solve this is using copy / paste. Yes, you simply copy the contents of the method of Class A and paste it into the method of Class C and you’re all set right?

Wrong!

It might work for a while, but one of the first things you *should* be feel itchy about, is code duplication! (along with its problems you get when you want to maintain your code).

So what now?

Solution : Use method for shared behavior
Solution : Use method for shared behavior

Well, when you took the easy road and duplicated your code, you probably wanted to get rid of that duplicate code immidiatly… And how do you do that? .. Yes,  you create a new method which is put in Class A , and accessible from Class C and Bam! Code Duplication gone, and you got what you want…

Another way is to re-think your class hierarchy. You might want to consider to do this:

 

Solution : Change in hierarchy, extend C from A, B from C
Solution : Change in hierarchy, extend C from A, B from C

 

 

Yes, you’ve seen it right. Perhaps you can swap Class B and C.  So C extends now from A, and B from C.

But, if you do that, be careful. You need to know exactly what kind of behavior you wanted in Class B. Most likely you have changed that now by extending from C. Take a good look at what methods B was originally calling from Class A, and if it now calls an overridden method by Class C.

These 2 solutions came up today. For my particular problem I’ve used the first. (no, not copy paste smart ass)

I think the first solution is the easiest, I would not recommend anyone to do the second solution unless you really know what you are doing. If you encounter more of these problems like above, swapping might be better for you.  

Do you got another (better?) solution? Let me know!