943,685 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 830
  • Java RSS
Sep 2nd, 2009
0

Hiiiiiiii all i m new to dis community. plz help me its urgent

Expand Post »
I m doing Program for sudoku. The blank grid displayed but how to pass random integer values to that grid. here is my code:

java Syntax (Toggle Plain Text)
  1. import javax.swing.BorderFactory;
  2. import javax.swing.JComponent;
  3. import javax.swing.JFrame;
  4. import javax.swing.JPanel;
  5. import java.awt.*;
  6. import javax.swing.JFormattedTextField;
  7. import javax.swing.text.DefaultFormatterFactory;
  8. import javax.swing.text.NumberFormatter;
  9.  
  10. @SuppressWarnings("empty-statement")
  11. public class Board
  12. {
  13.  
  14. private static final Dimension FIELD_SIZE = new Dimension(60, 60);
  15. private static final int EMPTY = 0;
  16. private JPanel grid = new JPanel();
  17. private JPanel[][] subgrid = new JPanel[3][3];
  18. private JFormattedTextField[][] cell = new JFormattedTextField[9][9];
  19. private static int num=0,col=9,row=9;
  20. private int Value=0;
  21. private String strValue;
  22.  
  23. private int GridBoard[][] = {{0, 6, 0, 1, 0, 4, 0, 5, 0},
  24. {0, 0, 8, 3, 0, 5, 6, 0, 0},
  25. {2, 0, 0, 0, 0, 0, 0, 0, 1},
  26. {0, 0, 6, 0, 0, 0, 3, 0, 0},
  27. {7, 0, 0, 9, 0, 1, 0, 0, 4},
  28. {5, 0, 0, 0, 0, 0, 0, 0, 2},
  29. {0, 0, 7, 2, 0, 6, 9, 0, 0},
  30. {0, 4, 0, 5, 0, 8, 0, 7, 0}};
  31. int GridArray[][] = null;
  32.  
  33. public Board()
  34. {
  35. grid.setBorder(BorderFactory.createLineBorder(Color.black, 3));
  36. grid.setLayout(new GridLayout(3, 3));
  37.  
  38. for (int i = 0; i < subgrid.length; i++)
  39. {
  40. for (int j = 0; j < subgrid[i].length; j++)
  41. {
  42. subgrid[i][j] = new JPanel(new GridLayout(3, 3));
  43. subgrid[i][j].setBorder(BorderFactory.createLineBorder(Color.black, 1));
  44. grid.add(subgrid[i][j]);
  45. }
  46. }
  47.  
  48. for (int i = 0; i < cell.length; i++)
  49. {
  50. for (int j = 0; j < cell[i].length; j++)
  51. {
  52. cell[i][j] = new JFormattedTextField(new DefaultFormatterFactory(new NumberFormatter()));
  53. cell[i][j].setValue(new Integer(Value));
  54.  
  55. cell[i][j].setPreferredSize(FIELD_SIZE);
  56. cell[i][j].setFont( new Font("sans-serif", Font.BOLD, 20));
  57. cell[i][j].setHorizontalAlignment(JFormattedTextField.CENTER);
  58. subgrid[i/3][j/3].add(cell[i][j]);
  59. }
  60. }
  61. }
  62.  
  63. Board(int[][] result)
  64. {
  65. this.grid =grid;
  66. }
  67. public JComponent getPanel() //return type of getPanel() is JComponent
  68. {
  69. return grid;
  70. }
  71.  
  72. public void setCellValue(int num, int row, int col)
  73. {
  74. num=0;
  75. for (int r = 1;r<=9 ;r++ )
  76. {
  77. for (int c = 1;c<=9 ; c++)
  78. {
  79. cell[r][c] = new JFormattedTextField();
  80. strValue=GridBoard.toString().replace("0", " ");
  81.  
  82. String text = (num == EMPTY) ? " " : String.valueOf(num);
  83. cell [r][c].setText(text);
  84.  
  85. }
  86. }
  87. }
  88.  
  89. public int getCellValue(int row, int col)
  90. {
  91. int cellval=0;
  92. try
  93. {
  94. for (int r = 0;r<cell.length ;r++ )
  95. {
  96. for (int c = 0;c<cell[r].length ; c++)
  97. {
  98. cellval = Integer.parseInt(cell[r][c].getText());
  99. }
  100. }
  101. }
  102. catch (NumberFormatException e)
  103. {
  104. cellval = EMPTY;
  105. }
  106. return cellval;
  107. }
  108.  
  109. public void clearBoard()
  110. {
  111. for (int r = 0; r < 9 ; r++ )
  112. {
  113. for (int c = 0; c < 9 ; c++)
  114. {
  115. cell[r][c] = new JFormattedTextField();
  116. }
  117. }
  118. }
  119.  
  120. public static boolean isValid(boolean[] choices){
  121. for (int i = 1; i <= 9; i++)
  122. {
  123. if (choices[i]) return true;
  124. }
  125. return false;
  126. }
  127.  
  128.  
  129. protected boolean checkRow( int num )
  130. {
  131. // loop through row and check values
  132. for (int i = 0; i < cell[row-1].length; i++)
  133. {
  134. if(num == Integer.parseInt(cell[row-1][i].getText()));
  135. {
  136. return true;
  137. }
  138. }
  139. return false;
  140. }
  141.  
  142. protected boolean checkCol( int num )
  143. {
  144. // loop through row and check values
  145. for (int i = 0; i < cell[col-1].length; i++)
  146. {
  147. if(num == Integer.parseInt(cell[col-1][i].getText()))
  148. {
  149. return true;
  150. }
  151. }
  152. return false;
  153. }
  154.  
  155. protected boolean inBox(int number)
  156. {
  157. for(int i = 0; i < subgrid.length; i++)
  158. {
  159. for(int j = 0 ; j < subgrid[i].length; j++)
  160. {
  161. if(number == Integer.parseInt(cell[i][j].getText()))
  162. return true;
  163. }
  164. }
  165. return false;
  166. }
  167.  
  168. private static void createAndShowGUI()
  169. {
  170. JFrame frame = new JFrame("Sudoku");
  171. frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  172. frame.getContentPane().add(new Board().getPanel());
  173. frame.pack();
  174. frame.setLocationRelativeTo(null);
  175. frame.setVisible(true);
  176. }
  177.  
  178.  
  179. public static void main(String[] args)
  180. {
  181. Board board= new Board();
  182. javax.swing.SwingUtilities.invokeLater(new Runnable()
  183. {
  184. public void run()
  185. {
  186. createAndShowGUI();
  187. }
  188. });
  189. }
  190. }
Last edited by Ezzaral; Sep 2nd, 2009 at 3:27 pm. Reason: Added [code] [/code] tags. Please use them to format any code that you post.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
harshadap is offline Offline
5 posts
since Sep 2009
Sep 2nd, 2009
0

Re: Sudoku help

Reputation Points: 72
Solved Threads: 26
Posting Whiz in Training
GDICommander is offline Offline
209 posts
since Jun 2008
Sep 4th, 2009
0

Re: Sudoku help

Thanks for suggestion. I done that but random number does not displayed in the grid. can you give source code for that.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
harshadap is offline Offline
5 posts
since Sep 2009
Sep 6th, 2009
0

Re: Sudoku help

hello
can anybody give me idea of how to validate numbers in sudoku and how to apply color for that particular no and cell
Reputation Points: 10
Solved Threads: 0
Newbie Poster
harshadap is offline Offline
5 posts
since Sep 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: Button Are Not In Same Size
Next Thread in Java Forum Timeline: weird output ?





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC