Results 1 to 3 of 3
Thread: SUDOKU game problem
- 05-05-2009, 08:24 PM #1
Member
- Join Date
- Feb 2009
- Posts
- 9
- Rep Power
- 0
SUDOKU game problem
Hello
I am trying to program an online sudoku game, and the layout of the game is a grid of 9x9 JTextFields. So, I'm arranging these 81 Fields in a panel, however, I need to have some sort of line between each 3x3 block. If anyone has ever played sudoku, you'd know what I mean. Any ideas how to create these lines?
Thanks for any advice,
bumblyb33
-
One solution: create 9 JPanels to each hold 9 of your JTextFields. Then add a border to the JPanels.
-
For example:
Java Code:import java.awt.Color; import java.awt.Dimension; import java.awt.Font; import java.awt.GridLayout; import javax.swing.BorderFactory; import javax.swing.JComponent; import javax.swing.JFrame; import javax.swing.JPanel; import javax.swing.JTextField; import javax.swing.SwingConstants; public class SudokuEg { private static final Dimension FIELD_SIZE = new Dimension(60, 60); private static final int BORDER_WIDTH = 3; private static final float FONT_POINTS = 25; private JPanel mainPanel = new JPanel(); private JPanel[][] subPanels = new JPanel[3][3]; private JTextField[][] fields = new JTextField[9][9]; public SudokuEg() { mainPanel.setBorder(BorderFactory.createLineBorder(Color.black, BORDER_WIDTH)); mainPanel.setLayout(new GridLayout(3, 3)); // create 3x3 grid of sub-panels, // add a line border to each sub-panel // and add to main panel for (int i = 0; i < subPanels.length; i++) { for (int j = 0; j < subPanels[i].length; j++) { subPanels[i][j] = new JPanel(new GridLayout(3, 3)); subPanels[i][j].setBorder( BorderFactory.createLineBorder(Color.black, BORDER_WIDTH)); mainPanel.add(subPanels[i][j]); } } // create all JTextFields // set fields preferred size, font, center text // and add to sub-panels for (int i = 0; i < fields.length; i++) { for (int j = 0; j < fields[i].length; j++) { fields[i][j] = new JTextField(); fields[i][j].setPreferredSize(FIELD_SIZE); Font font = fields[i][j].getFont().deriveFont(Font.BOLD, FONT_POINTS); fields[i][j].setFont(font); fields[i][j].setHorizontalAlignment(SwingConstants.CENTER); subPanels[i/3][j/3].add(fields[i][j]); } } } public JComponent getPanel() { return mainPanel; } private static void createAndShowGUI() { JFrame frame = new JFrame("SudokuEg Application"); frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); frame.getContentPane().add(new SudokuEg().getPanel()); frame.pack(); frame.setLocationRelativeTo(null); frame.setVisible(true); } public static void main(String[] args) { javax.swing.SwingUtilities.invokeLater(new Runnable() { public void run() { createAndShowGUI(); } }); } }
Similar Threads
-
Implementing "Game Over" in Minesweeper game based on Gridworld framework.
By JFlash in forum New To JavaReplies: 2Last Post: 08-05-2010, 04:49 AM -
2D strategy game or 2D war game
By led1433 in forum Java 2DReplies: 5Last Post: 02-10-2009, 06:00 AM -
Problem with an assignment: Backgammon game
By Poddy in forum New To JavaReplies: 6Last Post: 02-05-2009, 05:32 AM -
Tic Tac Game
By loggen in forum New To JavaReplies: 1Last Post: 12-12-2008, 07:36 AM -
Problem using buttons to creat a magic square game
By goldman in forum New To JavaReplies: 5Last Post: 05-05-2008, 04:04 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks