Beispiel #1
0
  // Initialize Board
  public void init() {
    // Create cells and handlers
    cells = new JTextField[gameBoard.boardSize * gameBoard.boardSize + 1];

    // Redraw Panel
    boardPanel.removeAll();
    boardPanel.setLayout(new GridLayout(gameBoard.boardSize, gameBoard.boardSize)); // Set layout

    JTextFilter TextFilter = new JTextFilter(3);
    JTextDocumentListener JTextDocFilter = new JTextDocumentListener();

    for (int i = 1; i <= gameBoard.boardSize * gameBoard.boardSize; i++) {
      cells[i] = new JTextField();

      ((AbstractDocument) cells[i].getDocument()).setDocumentFilter(TextFilter);
      ((AbstractDocument) cells[i].getDocument()).addDocumentListener(JTextDocFilter);
      ((AbstractDocument) cells[i].getDocument()).putProperty("index", i);

      cells[i].setHorizontalAlignment(JTextField.CENTER);
      cells[i].setFont(new Font("Agency FB", Font.BOLD, 24));

      // Add elements to the grid content pane
      boardPanel.add(cells[i]);
    }

    // Initialize booleans
    gameOver = false;

    // Clear Board
    for (int i = 1; i <= (gameBoard.boardSize * gameBoard.boardSize); i++) {
      String ch = Integer.toString(this.gameBoard.cells[i]);
      char chr = '-';
      if (ch.compareTo("0") == 0 || ch == Character.toString(chr)) {
        cells[i].setText("");
      } else {
        cells[i].setText(ch);
        cells[i].setBackground(Color.lightGray);
      }
    }
    // gameBoard.out();

    setVisible(true);
    this.boardPanel.repaint();

    this.gameTimer.reset();
    jButtonSOLVE.setEnabled(true);
  }
Beispiel #2
0
  public NumbrixGUI() throws ParseException { // Board gameBoard) throws ParseException{

    // Board
    this.gameBoard = new Board();

    // Container
    setTitle(TITLE);
    setSize(WIDTH, HEIGHT);
    setDefaultCloseOperation(EXIT_ON_CLOSE);

    // About Frame
    jFrameAbout = new javax.swing.JFrame();
    jFrameAbout.setResizable(false);
    jFrameAbout.setPreferredSize(new Dimension(500, 300));

    jTextAreaAbout = new javax.swing.JTextArea();
    jTextAreaAbout.setBackground(new java.awt.Color(240, 240, 240));
    jTextAreaAbout.setColumns(20);
    jTextAreaAbout.setRows(10);
    jTextAreaAbout.setText(
        "\t\tNumbrix Version 1\n\nClass Project for Artificial Intelligence course.\nUniversity of Florida\nCISE\n\n\n\n\nby Rainer Ledesma\nhttp://www.cise.ufl.edu/~rledesma/\n");
    jTextAreaAbout.setFont(new java.awt.Font("Agency FB", 1, 20)); // NOI18N
    jTextAreaAbout.setEditable(false);
    jTextAreaAbout.setPreferredSize(new Dimension(500, 300));

    javax.swing.GroupLayout jFrameAboutLayout =
        new javax.swing.GroupLayout(jFrameAbout.getContentPane());
    jFrameAbout.getContentPane().setLayout(jFrameAboutLayout);
    jFrameAboutLayout.setHorizontalGroup(
        jFrameAboutLayout
            .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(
                jFrameAboutLayout
                    .createSequentialGroup()
                    .addGap(11, 11, 11)
                    .addComponent(
                        jTextAreaAbout,
                        javax.swing.GroupLayout.PREFERRED_SIZE,
                        javax.swing.GroupLayout.DEFAULT_SIZE,
                        javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)));
    jFrameAboutLayout.setVerticalGroup(
        jFrameAboutLayout
            .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(
                jFrameAboutLayout
                    .createSequentialGroup()
                    .addGap(0, 16, Short.MAX_VALUE)
                    .addComponent(
                        jTextAreaAbout,
                        javax.swing.GroupLayout.PREFERRED_SIZE,
                        267,
                        javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(0, 17, Short.MAX_VALUE)));
    //

    // Instructions Frame
    jFrameInstructions = new javax.swing.JFrame();
    jFrameInstructions.setResizable(false);
    jFrameInstructions.setPreferredSize(new Dimension(500, 300));

    jTextAreaInstructions = new javax.swing.JTextArea();
    jTextAreaInstructions.setBackground(new java.awt.Color(240, 240, 240));
    jTextAreaInstructions.setColumns(20);
    jTextAreaInstructions.setRows(10);
    jTextAreaInstructions.setText(
        "\t\tNumbrix\n"
            + "The object of Numbrix is to fill the board with sequence of consecutive \n"
            + "numbers from 1 to the maximun cell count. The numbers must fillow a \n"
            + "horizontal or vertical path (no diagonals)\n");
    jTextAreaInstructions.setFont(new java.awt.Font("Agency FB", 1, 20)); // NOI18N
    jTextAreaInstructions.setEditable(false);
    jTextAreaInstructions.setPreferredSize(new Dimension(500, 300));

    javax.swing.GroupLayout jFrameInstructionsLayout =
        new javax.swing.GroupLayout(jFrameInstructions.getContentPane());
    jFrameInstructions.getContentPane().setLayout(jFrameInstructionsLayout);
    jFrameInstructionsLayout.setHorizontalGroup(
        jFrameInstructionsLayout
            .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(
                jFrameInstructionsLayout
                    .createSequentialGroup()
                    .addGap(11, 11, 11)
                    .addComponent(
                        jTextAreaInstructions,
                        javax.swing.GroupLayout.PREFERRED_SIZE,
                        javax.swing.GroupLayout.DEFAULT_SIZE,
                        javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)));
    jFrameInstructionsLayout.setVerticalGroup(
        jFrameInstructionsLayout
            .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(
                jFrameInstructionsLayout
                    .createSequentialGroup()
                    .addGap(0, 16, Short.MAX_VALUE)
                    .addComponent(
                        jTextAreaInstructions,
                        javax.swing.GroupLayout.PREFERRED_SIZE,
                        267,
                        javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addGap(0, 17, Short.MAX_VALUE)));
    //

    // File Chooser
    jFrameFileChooser = new javax.swing.JFrame();
    jFileChooser = new javax.swing.JFileChooser();
    jFileChooser.addActionListener(
        new java.awt.event.ActionListener() {
          public void actionPerformed(java.awt.event.ActionEvent evt) {
            try {
              jFileChooserActionPerformed(evt);
            } catch (Exception ex) {
              JOptionPane.showMessageDialog(frame, "File Exception");
            }
          }
        });

    javax.swing.GroupLayout jFrameFileChooserLayout =
        new javax.swing.GroupLayout(jFrameFileChooser.getContentPane());
    jFrameFileChooser.getContentPane().setLayout(jFrameFileChooserLayout);
    jFrameFileChooserLayout.setHorizontalGroup(
        jFrameFileChooserLayout
            .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 618, Short.MAX_VALUE)
            .addGroup(
                jFrameFileChooserLayout
                    .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(
                        jFrameFileChooserLayout
                            .createSequentialGroup()
                            .addGap(18, 18, 18)
                            .addComponent(
                                jFileChooser,
                                javax.swing.GroupLayout.DEFAULT_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE,
                                Short.MAX_VALUE)
                            .addGap(18, 18, 18))));
    jFrameFileChooserLayout.setVerticalGroup(
        jFrameFileChooserLayout
            .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 456, Short.MAX_VALUE)
            .addGroup(
                jFrameFileChooserLayout
                    .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(
                        jFrameFileChooserLayout
                            .createSequentialGroup()
                            .addGap(27, 27, 27)
                            .addComponent(
                                jFileChooser,
                                javax.swing.GroupLayout.DEFAULT_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE,
                                Short.MAX_VALUE)
                            .addGap(32, 32, 32))));
    //

    // SidePanel
    jPanelSideBar = new javax.swing.JPanel();
    jButtonRESET = new javax.swing.JButton();
    jButtonSOLVE = new javax.swing.JButton();
    jButtonNEWGAME = new javax.swing.JButton();
    jLabelTIMER = new javax.swing.JLabel();
    jButtonPAUSE = new javax.swing.JButton();

    jButtonRESET.setText("RESET");
    jButtonRESET.addActionListener(
        new java.awt.event.ActionListener() {
          public void actionPerformed(java.awt.event.ActionEvent evt) {
            jButtonRESETActionPerformed(evt);
          }
        });
    jButtonSOLVE.setText("SOLVE");
    jButtonSOLVE.setEnabled(false);
    jButtonSOLVE.addActionListener(
        new java.awt.event.ActionListener() {
          public void actionPerformed(java.awt.event.ActionEvent evt) {
            try {
              jButtonSOLVEActionPerformed(evt);
            } catch (IOException ex) {
              Logger.getLogger(NumbrixGUI.class.getName()).log(Level.SEVERE, null, ex);
            }
          }
        });
    jButtonNEWGAME.setText("NEW GAME");
    jButtonNEWGAME.addActionListener(
        new java.awt.event.ActionListener() {
          public void actionPerformed(java.awt.event.ActionEvent evt) {
            jButtonNEWGAMEActionPerformed(evt);
          }
        });
    jLabelTIMER.setText("TIMER");
    jLabelTIMER.setFont(new java.awt.Font("Agency FB", 1, 18));
    jButtonPAUSE.setText("PAUSE");
    jButtonPAUSE.addActionListener(
        new java.awt.event.ActionListener() {
          public void actionPerformed(java.awt.event.ActionEvent evt) {
            jButtonPAUSEActionPerformed(evt);
          }
        });
    jLabelClock = new javax.swing.JLabel();
    jLabelClock.setFont(new java.awt.Font("Agency FB", 1, 24)); // NOI18N
    jLabelClock.setText("10 : 99 : 60");

    javax.swing.GroupLayout jPanelSideBarLayout = new javax.swing.GroupLayout(jPanelSideBar);
    jPanelSideBar.setLayout(jPanelSideBarLayout);
    jPanelSideBarLayout.setHorizontalGroup(
        jPanelSideBarLayout
            .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(
                jPanelSideBarLayout
                    .createSequentialGroup()
                    .addContainerGap()
                    .addGroup(
                        jPanelSideBarLayout
                            .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(
                                jButtonRESET,
                                javax.swing.GroupLayout.Alignment.TRAILING,
                                javax.swing.GroupLayout.DEFAULT_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE,
                                Short.MAX_VALUE)
                            .addComponent(
                                jButtonSOLVE,
                                javax.swing.GroupLayout.Alignment.TRAILING,
                                javax.swing.GroupLayout.DEFAULT_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE,
                                Short.MAX_VALUE)
                            .addComponent(
                                jButtonNEWGAME,
                                javax.swing.GroupLayout.Alignment.TRAILING,
                                javax.swing.GroupLayout.DEFAULT_SIZE,
                                136,
                                Short.MAX_VALUE)
                            .addComponent(
                                jButtonPAUSE,
                                javax.swing.GroupLayout.DEFAULT_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE,
                                Short.MAX_VALUE)
                            .addGroup(
                                jPanelSideBarLayout
                                    .createSequentialGroup()
                                    .addGroup(
                                        jPanelSideBarLayout
                                            .createParallelGroup(
                                                javax.swing.GroupLayout.Alignment.LEADING)
                                            .addComponent(jLabelTIMER)
                                            .addComponent(
                                                jLabelClock,
                                                javax.swing.GroupLayout.PREFERRED_SIZE,
                                                97,
                                                javax.swing.GroupLayout.PREFERRED_SIZE))
                                    .addGap(0, 0, Short.MAX_VALUE)))
                    .addContainerGap()));
    jPanelSideBarLayout.setVerticalGroup(
        jPanelSideBarLayout
            .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(
                javax.swing.GroupLayout.Alignment.TRAILING,
                jPanelSideBarLayout
                    .createSequentialGroup()
                    .addContainerGap()
                    .addComponent(jLabelTIMER)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                    .addComponent(
                        jLabelClock,
                        javax.swing.GroupLayout.PREFERRED_SIZE,
                        32,
                        javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                    .addComponent(jButtonPAUSE)
                    .addPreferredGap(
                        javax.swing.LayoutStyle.ComponentPlacement.RELATED, 229, Short.MAX_VALUE)
                    .addComponent(jButtonNEWGAME)
                    .addGap(18, 18, 18)
                    .addComponent(jButtonRESET)
                    .addGap(18, 18, 18)
                    .addComponent(jButtonSOLVE)
                    .addContainerGap()));
    ////////

    // Menu
    jMenuBar1 = new javax.swing.JMenuBar();
    jMenu1 = new javax.swing.JMenu();
    jMenuItemNewGame = new javax.swing.JMenuItem();
    jMenuItemExit = new javax.swing.JMenuItem();
    jMenu2 = new javax.swing.JMenu();
    jMenuItemInstructions = new javax.swing.JMenuItem();
    jMenuItemAbout = new javax.swing.JMenuItem();

    jMenu1.setText("File");
    jMenu2.setText("Edit");

    jMenuItemNewGame.setText("New Game");
    jMenuItemExit.setText("Exit");
    jMenuItemInstructions.setText("Instructions");
    jMenuItemAbout.setText("About Numbrix");

    jMenuBar1.add(jMenu1);
    jMenuBar1.add(jMenu2);
    jMenu1.add(jMenuItemNewGame);
    jMenu1.add(jMenuItemExit);
    jMenu2.add(jMenuItemInstructions);
    jMenu2.add(jMenuItemAbout);

    setJMenuBar(jMenuBar1);

    jMenuItemNewGame.addActionListener(
        new java.awt.event.ActionListener() {
          public void actionPerformed(java.awt.event.ActionEvent evt) {
            jMenuItemNewGameActionPerformed(evt);
          }
        });
    jMenuItemExit.addActionListener(
        new java.awt.event.ActionListener() {
          public void actionPerformed(java.awt.event.ActionEvent evt) {
            jMenuItemExitActionPerformed(evt);
          }
        });
    jMenuItemInstructions.addActionListener(
        new java.awt.event.ActionListener() {
          public void actionPerformed(java.awt.event.ActionEvent evt) {
            jMenuItemInstructionsActionPerformed(evt);
          }
        });
    jMenuItemAbout.addActionListener(
        new java.awt.event.ActionListener() {
          public void actionPerformed(java.awt.event.ActionEvent evt) {
            jMenuItemAboutActionPerformed(evt);
          }
        });
    //

    // SolvedBoardPanel
    jPanelSolvedBoard = new javax.swing.JPanel();
    jPanelSolvedBoard.setBackground(new java.awt.Color(255, 255, 255));

    // Solved Board Frame
    jFrameSolvedBoard = new javax.swing.JFrame();
    jFrameSolvedBoard.setTitle("Numbrix Solution");
    javax.swing.GroupLayout jFrameSolvedBoardLayout =
        new javax.swing.GroupLayout(jFrameSolvedBoard.getContentPane());
    jFrameSolvedBoard.getContentPane().setLayout(jFrameSolvedBoardLayout);
    jFrameSolvedBoardLayout.setHorizontalGroup(
        jFrameSolvedBoardLayout
            .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 400, Short.MAX_VALUE)
            .addGroup(
                jFrameSolvedBoardLayout
                    .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(
                        jPanelSolvedBoard,
                        javax.swing.GroupLayout.Alignment.TRAILING,
                        javax.swing.GroupLayout.DEFAULT_SIZE,
                        400,
                        Short.MAX_VALUE)));
    jFrameSolvedBoardLayout.setVerticalGroup(
        jFrameSolvedBoardLayout
            .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 300, Short.MAX_VALUE)
            .addGroup(
                jFrameSolvedBoardLayout
                    .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(
                        jPanelSolvedBoard,
                        javax.swing.GroupLayout.Alignment.TRAILING,
                        javax.swing.GroupLayout.DEFAULT_SIZE,
                        300,
                        Short.MAX_VALUE)));

    // BoardPanel
    boardPanel = new javax.swing.JPanel();
    boardPanel.setBackground(new java.awt.Color(255, 255, 255));
    // Clock
    gameTimer = new GameTimer(jLabelClock);
    this.gameTimer.start();
    this.gameTimer.run();

    // Initialize
    // init();

    // Container Layout
    javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
    getContentPane().setLayout(layout);
    layout.setHorizontalGroup(
        layout
            .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(
                javax.swing.GroupLayout.Alignment.TRAILING,
                layout
                    .createSequentialGroup()
                    .addContainerGap()
                    .addComponent(
                        boardPanel, javax.swing.GroupLayout.DEFAULT_SIZE, 488, Short.MAX_VALUE)
                    .addGap(18, 18, 18)
                    .addComponent(
                        jPanelSideBar,
                        javax.swing.GroupLayout.PREFERRED_SIZE,
                        javax.swing.GroupLayout.DEFAULT_SIZE,
                        javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addContainerGap()));
    layout.setVerticalGroup(
        layout
            .createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(
                javax.swing.GroupLayout.Alignment.TRAILING,
                layout
                    .createSequentialGroup()
                    .addContainerGap()
                    .addGroup(
                        layout
                            .createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(
                                boardPanel,
                                javax.swing.GroupLayout.DEFAULT_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE,
                                Short.MAX_VALUE)
                            .addComponent(
                                jPanelSideBar,
                                javax.swing.GroupLayout.DEFAULT_SIZE,
                                javax.swing.GroupLayout.DEFAULT_SIZE,
                                Short.MAX_VALUE))
                    .addContainerGap()));
  }