Sudoku generator applet with OpenSudokuLib

To provide a ready-to-run example for OpenSudokuLib how to implement the OpenSudokuLib Classes, I created a Java Applet which runs in a webbrowser.

The application runs encapsulated in a component which can be applied to any AWT Frame, as well. To do this you only need to make an instance of the class SudokuFrame and add it to a suitable container.

Three Sudoku cases were created when the SudokuFrame has been initialized. The first one (Solution) is the case which stores the solution of the Sudoku. The second one (Workarea) is where we delete the fields which the player should work out afterwards by modifying the case step-by-step. The last one (InitialSudoku) stores the information about where the fixed fields were placed when the SudokuFrame has been created. The fixed fields can not be modified by the player.

Solution = new Sudoku();
Solution.createSudoku();

WorkArea = new Sudoku(Solution);
WorkArea.deleteFields(deletedFields);

InitialSudoku = new Sudoku(WorkArea);

The main function of the SudokuFrame is where we process the mouse events. We listen to left mouse clicks to increase the number in the sudoku fields and on right mouse clicks to decrease the number in the sudoku fields.

protected void processMouseEvent(MouseEvent e) {
...
if (SudokuStarted == false) {
SudokuStarted = true;
drawSudoku();
} else {
...
if (e.getButton() == MouseEvent.BUTTON1)
{ /* Here we react on left mouse click */ }
if (e.getButton() == MouseEvent.BUTTON3)
{ /* Here we react on right mouse click */ }
...
}

A mouse click on a single field will be basically processed in the same manner for both types of mouse events.

  1. At First we check whether we are out of bounds by clicking outside of the sudoku field. If we are out of bounds we are not going to react on the mouse click. Also we check whether the sudoku is already solved as we are not going to react on mouse clicks when the sudoku is solved as well.
  2. Afterwards we will check whether the field we are clicking is an initial field, as we are not going to modify initial fields.
  3. If these prerequistes are met, we will query the actual value of the clicked field.
  4. Now this is the part where the right click differs from the left click. A left click will increase the value of the sudoku field. If we already reached the highest value, which is 9 for a 9×9 sudoku, we will blank the field by setting the value to 0. Processing a right click will be performed in the opposite manner, which means the field will be blank when the actual value is 1.
  5. The change of the field will be stored in the work area instance
  6. Afterwards we will redraw the sudoku field which has been modified and the grids of the case.
  7. Finally we check all fields of the sudoku whether we already have solved the sudoku.

The Sudoku case is build as a component and simply added to the applet in the init phase. The whole logic is encapsulated in the component SudokuFrame.

public void init() {
setBackground(Color.lightGray);
setLayout(new FlowLayout());

Ca = new SudokuFrame(45);
Panel Panel1 = new Panel();
Panel1.add(Ca);
add(Panel1);
Panel1.setSize(Ca.getPreferredSize());
Ca.setVisible(true);
}

You can use the shown example for free and modify it under the terms of the GNU General Public License. You will find the sources for the example on sourceforge or get it from the download section of this website (adf.ly).

You will find more information about the sudoku generator in this article.