Results 1 to 3 of 3
  1. #1
    bumblyb33 is offline Member
    Join Date
    Feb 2009
    Posts
    9
    Rep Power
    0

    Default 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

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    One solution: create 9 JPanels to each hold 9 of your JTextFields. Then add a border to the JPanels.

  3. #3
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    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

  1. Replies: 2
    Last Post: 08-05-2010, 04:49 AM
  2. 2D strategy game or 2D war game
    By led1433 in forum Java 2D
    Replies: 5
    Last Post: 02-10-2009, 06:00 AM
  3. Problem with an assignment: Backgammon game
    By Poddy in forum New To Java
    Replies: 6
    Last Post: 02-05-2009, 05:32 AM
  4. Tic Tac Game
    By loggen in forum New To Java
    Replies: 1
    Last Post: 12-12-2008, 07:36 AM
  5. Problem using buttons to creat a magic square game
    By goldman in forum New To Java
    Replies: 5
    Last Post: 05-05-2008, 04:04 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •