After you've put one number (the red box) on the rectangle, the next number should be on one of the marked locations relative to the red number.

public void Generate(int x, int y, int num) {
int nx, ny, dir;
if (num >= mxsize) {
System.out.println("Next
solution:");
board.output(System.out);
return;
} else {
dir = 0;
while (dir < NumDir)
{
nx = x + xAdd(dir);
ny = y + yAdd(dir);
dir++;
if (board.isAvailable(nx,ny)) {
board.set(nx,ny,num);
Generate(nx,ny,num+1);
board.reset(nx,ny);
}
}
}
}
By Jan Struyf, 22/03/98