Questo sito utilizza cookies solo per scopi di autenticazione sul sito e nient'altro. Nessuna informazione personale viene tracciata. Leggi l'informativa sui cookies.
Username: Password: oppure
JSudoku - JPlaySudoku.java

JPlaySudoku.java

Caricato da:
Scarica il programma completo

  1. /*
  2.  * To change this template, choose Tools | Templates
  3.  * and open the template in the editor.
  4.  */
  5.  
  6. /*
  7.  * JPlaySudoku.java
  8.  *
  9.  * Created on 8-set-2010, 11.22.01
  10.  */
  11. package jsudoku.graphics;
  12.  
  13. import com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel;
  14. import java.io.File;
  15. import java.io.FileNotFoundException;
  16. import java.io.IOException;
  17. import javax.swing.ImageIcon;
  18. import javax.swing.JFileChooser;
  19. import javax.swing.JFrame;
  20. import javax.swing.JLabel;
  21. import javax.swing.JOptionPane;
  22. import javax.swing.UIManager;
  23. import javax.swing.UnsupportedLookAndFeelException;
  24. import javax.swing.filechooser.FileFilter;
  25. import jsudoku.Sudoku;
  26. import jsudoku.SudokuFileManager;
  27. import jsudoku.exception.InvalidSudokuException;
  28.  
  29. /**
  30.  *
  31.  * @author Paolo
  32.  */
  33. public class JPlaySudoku extends javax.swing.JFrame {
  34.  
  35.     private JSudoku sudoku;
  36.     private JFileChooser choose;
  37.  
  38.     /** Creates new form JPlaySudoku */
  39.     public JPlaySudoku() {
  40.         try {
  41.             UIManager.setLookAndFeel(new NimbusLookAndFeel());
  42.         } catch (UnsupportedLookAndFeelException ex) {
  43.             try {
  44.                 UIManager.setLookAndFeel(UIManager.getSystemLookAndFeelClassName());
  45.             } catch (ClassNotFoundException ex1) {
  46.             } catch (InstantiationException ex1) {
  47.             } catch (IllegalAccessException ex1) {
  48.             } catch (UnsupportedLookAndFeelException ex1) {
  49.             }
  50.         }
  51.         initComponents();
  52.         this.initFileChooser();
  53.         this.sudoku = new JSudoku();
  54.         this.add(this.sudoku);
  55.     }
  56.  
  57.     public void initFileChooser() {
  58.         this.choose = new JFileChooser();
  59.         this.choose.setFileFilter(new FileFilter() {
  60.  
  61.             @Override
  62.             public boolean accept(File f) {
  63.                 if (f.getName().endsWith(".sdk") || f.isDirectory()) {
  64.                     return true;
  65.                 }
  66.                 return false;
  67.             }
  68.  
  69.             @Override
  70.             public String getDescription() {
  71.                 return "File sudoku (.sdk)";
  72.             }
  73.         });
  74.     }
  75.  
  76.     /** This method is called from within the constructor to
  77.      * initialize the form.
  78.      * WARNING: Do NOT modify this code. The content of this method is
  79.      * always regenerated by the Form Editor.
  80.      */
  81.     @SuppressWarnings("unchecked")
  82.     // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
  83.     private void initComponents() {
  84.  
  85.         buttonPanel = new javax.swing.JPanel();
  86.         newButton = new javax.swing.JButton();
  87.         verifyButton = new javax.swing.JButton();
  88.         stateLabel = new javax.swing.JLabel();
  89.         state = new javax.swing.JLabel();
  90.         createButton = new javax.swing.JButton();
  91.         menu = new javax.swing.JMenuBar();
  92.         file = new javax.swing.JMenu();
  93.         open = new javax.swing.JMenuItem();
  94.         save = new javax.swing.JMenuItem();
  95.         exit = new javax.swing.JMenuItem();
  96.         Info = new javax.swing.JMenu();
  97.         help = new javax.swing.JMenuItem();
  98.         info = new javax.swing.JMenuItem();
  99.  
  100.         setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
  101.         setTitle("JSudoku - By Paolo Ricciuti (C)");
  102.         setResizable(false);
  103.  
  104.         newButton.setText("Nuovo Sudoku");
  105.         newButton.addActionListener(new java.awt.event.ActionListener() {
  106.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  107.                 newButtonActionPerformed(evt);
  108.             }
  109.         });
  110.  
  111.         verifyButton.setText("Verifica");
  112.         verifyButton.addActionListener(new java.awt.event.ActionListener() {
  113.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  114.                 verifyButtonActionPerformed(evt);
  115.             }
  116.         });
  117.  
  118.         stateLabel.setText("Stato del sudoku:");
  119.  
  120.         createButton.setText("Crea nuovo");
  121.         createButton.addActionListener(new java.awt.event.ActionListener() {
  122.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  123.                 createButtonActionPerformed(evt);
  124.             }
  125.         });
  126.  
  127.         javax.swing.GroupLayout buttonPanelLayout = new javax.swing.GroupLayout(buttonPanel);
  128.         buttonPanel.setLayout(buttonPanelLayout);
  129.         buttonPanelLayout.setHorizontalGroup(
  130.             buttonPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  131.             .addGroup(buttonPanelLayout.createSequentialGroup()
  132.                 .addContainerGap()
  133.                 .addGroup(buttonPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  134.                     .addGroup(buttonPanelLayout.createSequentialGroup()
  135.                         .addComponent(stateLabel)
  136.                         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  137.                         .addComponent(state, javax.swing.GroupLayout.PREFERRED_SIZE, 137, javax.swing.GroupLayout.PREFERRED_SIZE))
  138.                     .addGroup(buttonPanelLayout.createSequentialGroup()
  139.                         .addComponent(newButton)
  140.                         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
  141.                         .addComponent(verifyButton)
  142.                         .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
  143.                         .addComponent(createButton)))
  144.                 .addContainerGap(17, Short.MAX_VALUE))
  145.         );
  146.         buttonPanelLayout.setVerticalGroup(
  147.             buttonPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  148.             .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, buttonPanelLayout.createSequentialGroup()
  149.                 .addContainerGap()
  150.                 .addGroup(buttonPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  151.                     .addComponent(newButton)
  152.                     .addComponent(verifyButton)
  153.                     .addComponent(createButton))
  154.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 17, Short.MAX_VALUE)
  155.                 .addGroup(buttonPanelLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
  156.                     .addComponent(stateLabel)
  157.                     .addComponent(state, javax.swing.GroupLayout.PREFERRED_SIZE, 14, javax.swing.GroupLayout.PREFERRED_SIZE))
  158.                 .addContainerGap())
  159.         );
  160.  
  161.         file.setText("File");
  162.  
  163.         open.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_A, java.awt.event.InputEvent.CTRL_MASK));
  164.         open.setText("Apri");
  165.         open.addActionListener(new java.awt.event.ActionListener() {
  166.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  167.                 openActionPerformed(evt);
  168.             }
  169.         });
  170.         file.add(open);
  171.  
  172.         save.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F12, java.awt.event.InputEvent.SHIFT_MASK | java.awt.event.InputEvent.CTRL_MASK));
  173.         save.setText("Salva");
  174.         save.addActionListener(new java.awt.event.ActionListener() {
  175.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  176.                 saveActionPerformed(evt);
  177.             }
  178.         });
  179.         file.add(save);
  180.  
  181.         exit.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_ESCAPE, 0));
  182.         exit.setText("Esci");
  183.         exit.addActionListener(new java.awt.event.ActionListener() {
  184.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  185.                 exitActionPerformed(evt);
  186.             }
  187.         });
  188.         file.add(exit);
  189.  
  190.         menu.add(file);
  191.  
  192.         Info.setText("?");
  193.  
  194.         help.setAccelerator(javax.swing.KeyStroke.getKeyStroke(java.awt.event.KeyEvent.VK_F1, 0));
  195.         help.setText("Help");
  196.         help.addActionListener(new java.awt.event.ActionListener() {
  197.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  198.                 helpActionPerformed(evt);
  199.             }
  200.         });
  201.         Info.add(help);
  202.  
  203.         info.setText("Informazioni su JSudoku");
  204.         info.addActionListener(new java.awt.event.ActionListener() {
  205.             public void actionPerformed(java.awt.event.ActionEvent evt) {
  206.                 infoActionPerformed(evt);
  207.             }
  208.         });
  209.         Info.add(info);
  210.  
  211.         menu.add(Info);
  212.  
  213.         setJMenuBar(menu);
  214.  
  215.         javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
  216.         getContentPane().setLayout(layout);
  217.         layout.setHorizontalGroup(
  218.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  219.             .addComponent(buttonPanel, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
  220.         );
  221.         layout.setVerticalGroup(
  222.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
  223.             .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
  224.                 .addContainerGap(303, Short.MAX_VALUE)
  225.                 .addComponent(buttonPanel, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
  226.         );
  227.  
  228.         java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
  229.         setBounds((screenSize.width-316)/2, (screenSize.height-438)/2, 316, 438);
  230.     }// </editor-fold>//GEN-END:initComponents
  231.  
  232.     private void openActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_openActionPerformed
  233.         int retval = this.choose.showOpenDialog(this);
  234.         if (retval == JFileChooser.APPROVE_OPTION) {
  235.             SudokuFileManager sfm = new SudokuFileManager(this.choose.getSelectedFile());
  236.             try {
  237.                 Sudoku sud = sfm.openSudoku();
  238.                 this.sudoku.setSudoku(sud);
  239.             } catch (FileNotFoundException ex) {
  240.             } catch (IOException ex) {
  241.             } catch (InvalidSudokuException ex) {
  242.                 JOptionPane.showMessageDialog(this, "File sudoku non valido.");
  243.             }
  244.         }
  245.     }//GEN-LAST:event_openActionPerformed
  246.  
  247.     private void exitActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_exitActionPerformed
  248.         System.exit(0);
  249.     }//GEN-LAST:event_exitActionPerformed
  250.  
  251.     private void newButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_newButtonActionPerformed
  252.         this.sudoku.setSudoku(new Sudoku());
  253.     }//GEN-LAST:event_newButtonActionPerformed
  254.  
  255.     private void saveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_saveActionPerformed
  256.         int confirm = JOptionPane.OK_OPTION;
  257.         if (!this.sudoku.getSudoku().checkSudoku()) {
  258.             confirm = JOptionPane.showConfirmDialog(this, "Il sudoku non è corretto, salvandolo non sarà più possibile modificarlo.\nContinuare?");
  259.         }
  260.         if (confirm == JOptionPane.OK_OPTION) {
  261.             int retval = this.choose.showSaveDialog(this);
  262.             if (retval == JFileChooser.APPROVE_OPTION) {
  263.                 SudokuFileManager sfm = new SudokuFileManager(this.choose.getSelectedFile());
  264.                 try {
  265.                     sfm.saveSudoku(this.sudoku.getSudoku());
  266.                 } catch (IOException ex) {
  267.                     JOptionPane.showMessageDialog(this, "Errore nel salvataggio file!");
  268.                 }
  269.             }
  270.         }
  271.     }//GEN-LAST:event_saveActionPerformed
  272.  
  273.     private void verifyButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_verifyButtonActionPerformed
  274.         Sudoku sud = this.sudoku.getSudoku();
  275.         String solved = "risolto";
  276.         String correct = "corretto";
  277.         if (!sud.checkSudoku()) {
  278.             correct = "non " + correct;
  279.         }
  280.         if (!sud.isResolvedSudoku()) {
  281.             solved = "non " + solved;
  282.         }
  283.         this.state.setText(solved + " - " + correct);
  284.     }//GEN-LAST:event_verifyButtonActionPerformed
  285.  
  286.     private void createButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_createButtonActionPerformed
  287.         int retval = JOptionPane.showConfirmDialog(this, "Confermando il gioco attuale verrà perso\n la griglia verrà svuotata per permettervi di creare un nuovo schema da salvare.");
  288.         if (retval == JOptionPane.OK_OPTION) {
  289.             this.sudoku.getSudoku().emptySudoku();
  290.             this.sudoku.repaintSudoku();
  291.             this.sudoku.initListeners();
  292.         }
  293.     }//GEN-LAST:event_createButtonActionPerformed
  294.  
  295.     private void helpActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_helpActionPerformed
  296.         JHelp helpWindow = new JHelp();
  297.         helpWindow.setVisible(true);
  298.     }//GEN-LAST:event_helpActionPerformed
  299.  
  300.     private void infoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_infoActionPerformed
  301.         JFrame frame = new JFrame("Informazioni su JSudoku");
  302.         frame.setResizable(false);
  303.         frame.setDefaultCloseOperation(JFrame.HIDE_ON_CLOSE);
  304.         frame.setBounds(this.getLocation().x + 100, this.getLocation().y + 100, 400, 300);
  305.         frame.setVisible(true);
  306.         JLabel content = new JLabel();
  307.         content.setIcon(new ImageIcon(getClass().getResource("/jsudoku/graphics/images/logo.png")));
  308.         content.setText("<html>"
  309.                 + "Creato da Paolo Ricciuti senza alcun scopo di lucro per la "
  310.                 + "community PieroTofy a scopo illustrativo dei principali "
  311.                 + "metodi e classi del package swing."
  312.                 + "</html>");
  313.         frame.add(content);
  314.     }//GEN-LAST:event_infoActionPerformed
  315.  
  316.     /**
  317.      * @param args the command line arguments
  318.      */
  319.     public static void main(String args[]) {
  320.         java.awt.EventQueue.invokeLater(new Runnable() {
  321.  
  322.             public void run() {
  323.                 new JPlaySudoku().setVisible(true);
  324.             }
  325.         });
  326.     }
  327.     // Variables declaration - do not modify//GEN-BEGIN:variables
  328.     private javax.swing.JMenu Info;
  329.     private javax.swing.JPanel buttonPanel;
  330.     private javax.swing.JButton createButton;
  331.     private javax.swing.JMenuItem exit;
  332.     private javax.swing.JMenu file;
  333.     private javax.swing.JMenuItem help;
  334.     private javax.swing.JMenuItem info;
  335.     private javax.swing.JMenuBar menu;
  336.     private javax.swing.JButton newButton;
  337.     private javax.swing.JMenuItem open;
  338.     private javax.swing.JMenuItem save;
  339.     private javax.swing.JLabel state;
  340.     private javax.swing.JLabel stateLabel;
  341.     private javax.swing.JButton verifyButton;
  342.     // End of variables declaration//GEN-END:variables
  343. }