29 April 2009

Ask a programming related question on stackoverflow.com

One of the best "programmer-asks-questions-formus" I know is stackoverflow.com. It is just like the old experts-exchange, except it is free and it contains no commercials. It is also very simple to ask questions. Just log in with your openID and ask a question!

I have been a reader for some months now but today I finally asked my first question: http://stackoverflow.com/questions/802612/how-to-write-a-null-safe-compare-in-pure-sql.

27 April 2009

Bouncing balls and collision detection

One of the first assignments on the first Java course on my computing science program was to create a program with bouncing balls. The balls should be able to bounce of the walls. There was no requirement for the balls to bounce of each other.

Is there a simple model for bouncing two balls? One approach is to only look at one ball at a time and consider the other ball as a rigid object. The in angle should be the same as the out angle:

The result: a very simple ball simulation program:

There are of course some bugs. Like that the balls can escape... :) Try it out! If the balls get stuck drag them into a new position. Balls version 0.0.1

21 April 2009

GroupLayout vs JFrame - the content pane problem

I really like the "new" GroupLayout that was introduced in JDK 1.6. It originates from Netbeans GUI editor. It is a layout that groups components in logical groups. For a tutorial of GroupLayout see http://java.sun.com/docs/books/tutorial/uiswing/layout/group.html.

The JFrame is a sneaky Swing implementation. It has a content pane where the main things of your application should be in. But is has convenience methods (for example add() and setLauout()) that actually invokes call in the content pane. This might make you believe you can use is just as a normal container when using GroupLayout. The naive approach is to do this:
public class MyFrame extends JFrame {
GroupLayout layout = new GroupLayout(this);

This will unfortunately not work. The GroupLayout is fooled to control the JFrame, but in fact it should control the content pane inside the JFrame. The solution is instead to to this:

import javax.swing.GroupLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingUtilities;

public class GroupLayoutJFrameExample extends JFrame {

private GroupLayout layout = new GroupLayout(this.getContentPane());

public GroupLayoutJFrameExample() {

private void initLayout() {

JLabel helloLabel = new JLabel("Hello!");



public static void main(String args[]) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
JFrame frame = new GroupLayoutJFrameExample();

16 April 2009

How to merge-copy a Java bean

Problem: you have one template object and one target object. They are of the same class and is a standard Java bean. You would like to copy and merge all of the properties from the template object to the target object, but only for attributes that is null in the target.

  public <M> void merge(M target, M destination) throws Exception {
BeanInfo beanInfo = Introspector.getBeanInfo(target.getClass());

// Iterate over all the attributes
for (PropertyDescriptor descriptor : beanInfo.getPropertyDescriptors()) {

// Only copy writable attributes
if (descriptor.getWriteMethod() != null) {
Object originalValue = descriptor.getReadMethod()

// Only copy values values where the destination values is null
if (originalValue == null) {
Object defaultValue = descriptor.getReadMethod().invoke(
descriptor.getWriteMethod().invoke(target, defaultValue);


Test code:
public void testMerge() throws Exception {
Person templatePerson = new Person("Template", 20);
Person targetPerson = new Person("Johan", null);
merge(targetPerson, templatePerson);

assertEquals("Johan", targetPerson.getName());
assertEquals(20, targetPerson.getAge());

private static class Person {
private String name;
private Integer age;

public Person(String name, Integer age) {
this.name = name;
this.age = age;

public void setName(String name) {
this.name = name;
public String getName() {
return name;
public void setAge(Integer age) {
this.age = age;
public Integer getAge() {
return age;

09 April 2009

Tetris in Java (J2ME) MIDP 1.0

I found my old Tetris game i developed in 2004. At that time I had a Ericsson 610i phone. It supported MIDP 1.0. Nowdays the specification for J2ME have evloved quite some much. I tried to play the game on my 3 years old SonyEricsson W810. It still works! Well sort of, I noticed that there is bug so that exiting is a bit harder than it used to...

If you want to play my Tetris direct your phone to this URL: http://lesc.se/tetris/Tetris.jad.

05 April 2009

A simple under clock experiment

I have one computer in a closet. It is a server that is always on. I did a little experiment to see how under clocking the CPU would affect temperature and power consumption.

The CPU is a AMD Atholon 3500+ that clocks in a 2,2 GHz. During the test I had 2 Gb of memory and one Samsung 80 Gb, 2 platter 2,5 inch hard drive (a notebook hard drive).

SpeedPower consumptionTemperature (high fan speed)Temperature (low fan speed)
2,2 GHzLoad36 W5260
Idle26 W3940
1,8 GHzLoad35 W4653
Idle25 W3739
1,0 GHzLoad30 W4244
Idle25 W3739

I did not modify the VCore anything during these tests, I let the motherboard decide that. My guess is that with more tuning of the VCore I would have gotten better numbers.