|
||
|
|||
|
Hello, my task is to create a new Sudoku object and then create a new SudokuWindow object, passing the Sudoku object as a constructor parameter.
I am given SudokuWindow Java Code
import javax.swing.*;
import javax.swing.border.*;
import java.awt.*;
import java.awt.event.*;
public class SudokuWindow extends JFrame
{
private static final Color lockedColor = new Color(233, 233, 233);
private static final Color bgColor = Color.white;
private static final Color hoverColor = new Color(211, 255, 211);
private static final Border regionBorder = BorderFactory.createLineBorder(new Color(102, 102, 153));
private static final Border cellBorder = BorderFactory.createLineBorder(new Color(221, 221, 238));
private static final Border hoverBorder = BorderFactory.createLineBorder(Color.black);
private static final Cursor defaultCursor = new Cursor(Cursor.DEFAULT_CURSOR);
private Sudoku sudoku;
private State state;
private int lockCount;
private BoardPanel boardPanel;
private StatusPanel statusPanel;
public SudokuWindow(Sudoku sudoku)
{
this.sudoku = sudoku;
state = State.configuring;
lockCount = 0;
boardPanel = new BoardPanel();
statusPanel = new StatusPanel();
JPanel boardContainer = new JPanel();
boardContainer.setBackground(bgColor);
boardContainer.add(boardPanel);
add(boardContainer, BorderLayout.CENTER);
add(statusPanel, BorderLayout.SOUTH);
pack();
setVisible(true);
}
class BoardPanel extends JPanel
{
public BoardPanel()
{
int size = sudoku.getSize();
setLayout(new GridLayout(size, size));
for (int row = 0; row < size; row++)
for (int col = 0; col < size; col++)
add(new RegionPanel(row, col));
}
}
class RegionPanel extends JPanel
{
private JTextField[][] fields;
private boolean[][] locked;
public RegionPanel(int regRow, int regCol)
{
final int size = sudoku.getSize();
final int bigSize = size*size;
setLayout(new GridLayout(size, size));
setBorder(regionBorder);
fields = new JTextField[size][size];
locked = new boolean[size][size];
for (int row = 0; row < size; row++)
for (int col = 0; col < size; col++)
{
final int frow = row, fcol = col;
final JTextField field = new JTextField();
field.addMouseListener(new MouseAdapter()
{
public void mouseEntered(MouseEvent event)
{
if (!locked[frow][fcol])
{
field.requestFocus();
field.setBackground(hoverColor);
field.setBorder(hoverBorder);
}
}
public void mouseExited(MouseEvent event)
{
if (!locked[frow][fcol])
{
field.setBackground(bgColor);
field.setBorder(cellBorder);
}
}
}
);
field.setCursor(defaultCursor);
field.setBorder(cellBorder);
field.setPreferredSize(new Dimension(35, 35));
field.setHorizontalAlignment(JTextField.CENTER);
field.setFont(new Font("Serif", Font.BOLD, 16));
final int region = regRow*size + regCol + 1;
final int cell = row*size + col + 1;
field.addKeyListener(new KeyAdapter()
{
public void keyReleased(KeyEvent event)
{
if (locked[frow][fcol])
return;
int number;
try
{
number = Integer.parseInt(field.getText());
if (number > bigSize) number /= 10;
if (number < 1) number = 1;
}
catch (Exception e)
{
number = Cell.BLANK;
}
boolean lock = state == State.configuring && number != Cell.BLANK;
sudoku.enterNumber(region, cell, number, lock);
number = sudoku.getNumber(region, cell);
field.setText(number == Cell.BLANK ? "" : (""+number));
if (lock)
{
field.setEditable(false);
field.setBackground(lockedColor);
field.setBorder(cellBorder);
lockCount++;
if (lockCount >= size*size*size*size/3)
{
state = State.playing;
statusPanel.update();
}
}
if (sudoku.isSolved())
{
state = State.solved;
statusPanel.update();
}
locked[frow][fcol] |= lock;
}
}
);
add(field);
fields[row][col] = field;
}
}
}
class StatusPanel extends JPanel
{
private JLabel label;
public StatusPanel()
{
setBackground(bgColor);
add(label = new JLabel());
label.setFont(new Font("Serif", Font.BOLD, 18));
update();
}
public void update()
{
label.setText(message());
}
private String message()
{
switch (state)
{
case configuring: return "Please initialise the puzzle...";
case playing: return "Puzzle ready to solve";
case solved: return "It's SOLVED!!!";
default: return "Invalid state?";
}
}
}
enum State { configuring, playing, solved }
}
Java Code
public class Sudoku
{
private Group reg1, reg2, reg3, reg4;
private Group row1, row2, row3, row4;
private Group col1, col2, col3, col4;
public Sudoku()
{
Cell c1 = new Cell(), c2 = new Cell(), c3 = new Cell(), c4 = new Cell();
Cell c5 = new Cell(), c6 = new Cell(), c7 = new Cell(), c8 = new Cell();
Cell c9 = new Cell(), c10 = new Cell(), c11 = new Cell(), c12 = new Cell();
Cell c13 = new Cell(), c14 = new Cell(), c15 = new Cell(), c16 = new Cell();
reg1 = new Group(c1, c2, c5, c6);
reg2 = new Group(c3, c4, c7, c8);
reg3 = new Group(c9, c10, c13, c14);
reg4 = new Group(c11, c12, c15, c16);
row1 = new Group(c1, c2, c3, c4);
row2 = new Group(c5, c6, c7, c8);
row3 = new Group(c9, c10, c11, c12);
row4 = new Group(c13, c14, c15, c16);
col1 = new Group(c1, c5, c9, c13);
col2 = new Group(c2, c6, c10, c14);
col3 = new Group(c3, c7, c11, c15);
col4 = new Group(c4, c8, c12, c16);
}
public void enterNumber(int region, int cell, int number, boolean lock)
{
getRegion(region).enterNumber(cell, number, lock);
}
public int getNumber(int region, int cell)
{
return getRegion(region).getCell(cell).getNumber();
}
private Group getRegion(int regionNumber)
{
switch (regionNumber)
{
case 1: return reg1;
case 2: return reg2;
case 3: return reg3;
case 4: return reg4;
}
return null;
}
public boolean isSolved()
{
return reg1.isSolved() && reg2.isSolved() && reg3.isSolved() && reg4.isSolved()
&& row1.isSolved() && row2.isSolved() && row3.isSolved() && row4.isSolved()
&& col1.isSolved() && col2.isSolved() && col3.isSolved() && col4.isSolved();
}
public void print()
{
System.out.println();
row1.print();
row2.print();
row3.print();
row4.print();
System.out.println();
}
public int getSize()
{
return 2;
}
}
Java Code
import java.util.*;
public class GraphicalMain
{
private static Scanner keyboard = new Scanner(System.in);
private static SudokuWindow sudokuWindow;
private static Sudoku sudoku;
public static void main(String[] args)
{
sudoku = new Sudoku();
sudoku = new SudokuWindow(Sudoku, sudoku);
}
}
I'm getting the error at Java Code
sudoku = new SudokuWindow(Sudoku, sudoku); Thanks
Last edited by derky; 27-10-2009 at 08:33 AM. |
| The Following User Says Thank You to Json For This Useful Post: | ||
derky (27-10-2009) | ||
![]() |
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| Arrays Of Objects? | MysticDeath | Object Oriented Programming | 2 | 21-10-2009 02:39 AM |
| Throwing arrays as objects | Audemars | Collections and Generics | 1 | 23-09-2009 11:29 PM |
| [SOLVED] Problem with array objects | sadi_shihab | Collections and Generics | 4 | 09-07-2009 06:38 PM |
| transferring and recieving encryted objects over a network | vikas | Java Networking | 1 | 07-07-2009 04:00 PM |
| Recycling objects from multiple classes that all descend from the same class | PhilTroy | Object Oriented Programming | 6 | 15-05-2009 10:06 PM |
|
100 most searched terms
Search Cloud
|
| 2d arraylist java actionlistener actionlistener in java actionlistener java addactionlistener addactionlistener in java addactionlistener java applications of oops could not create java virtual machine xp double format java double to int java double to integer in java double to integer java eclipse shortcut keys eclipse tutorial for beginners exception in thread "awt-eventqueue-0" java.lang.outofmemoryerror: java heap space exception in thread "main" java.lang.nullpointerexception exception in thread "main" java.lang.outofmemoryerror: java heap space format double java get mouse position java http://www.javaprogrammingforums.com/object-oriented-programming/3713-limiting-decimal-places-double.html java 2d arraylist java actionlistener java addactionlistener java double format java double to int java double to integer java format double java forum java forums java get mouse position java list to map java mouse position java programmers forum java programming forum java programming forums java programming help java project ideas java sendkeys java two dimensional arraylist java.lang.classformaterror: truncated class file java.lang.outofmemoryerror: java heap space java.util.arraylist jbutton actionlistener jtextarea font jtextarea font color jtextfield font size jxl.read.biff.biffexception: unable to recognize ole stream two dimensional arraylist java writing ipod apps |