2018年4月16日 星期一

Magic Suare :


  1. //import static com.sun.xml.internal.fastinfoset.alphabet.BuiltInRestrictedAlphabets.table;  
  2. import java.awt.Color;  
  3. import java.awt.Dimension;  
  4. import java.util.Vector;  
  5. import java.util.logging.Level;  
  6. import java.util.logging.Logger;  
  7. import javax.swing.JOptionPane;  
  8. import javax.swing.table.DefaultTableModel;  
  9.   
  10. /* 
  11.  * To change this license header, choose License Headers in Project Properties. 
  12.  * To change this template file, choose Tools | Templates 
  13.  * and open the template in the editor. 
  14.  */  
  15.   
  16. /** 
  17.  * 
  18.  * @author user 
  19.  */  
  20. public class MEX extends javax.swing.JFrame {  
  21.   
  22.     /** 
  23.      * Creates new form MEX 
  24.      */  
  25.     public MEX() {     
  26.         super("n*n魔方陣");    
  27.         this.getContentPane().setBackground(new Color(204,204,0));  
  28.         initComponents();               
  29.     }  
  30.   
  31.     /** 
  32.      * This method is called from within the constructor to initialize the form. 
  33.      * WARNING: Do NOT modify this code. The content of this method is always 
  34.      * regenerated by the Form Editor. 
  35.      */  
  36.     @SuppressWarnings("unchecked")  
  37.     // <editor-fold defaultstate="collapsed" desc="Generated Code">                            
  38.     private void initComponents() {  
  39.   
  40.         jPanel1 = new javax.swing.JPanel();  
  41.         jButton1 = new javax.swing.JButton();  
  42.         jButton3 = new javax.swing.JButton();  
  43.         jTextField1 = new javax.swing.JTextField();  
  44.         jButton2 = new javax.swing.JButton();  
  45.         jButton4 = new javax.swing.JButton();  
  46.         jTabbedPane1 = new javax.swing.JTabbedPane();  
  47.         jScrollPane1 = new javax.swing.JScrollPane();  
  48.         jTable1 = new javax.swing.JTable();  
  49.         jLabel1 = new javax.swing.JLabel();  
  50.   
  51.         setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);  
  52.         setBounds(new java.awt.Rectangle(0000));  
  53.         setMaximumSize(new java.awt.Dimension(434236));  
  54.         setMinimumSize(new java.awt.Dimension(434236));  
  55.         setPreferredSize(new java.awt.Dimension(434236));  
  56.   
  57.         jPanel1.setLayout(new java.awt.GridLayout());  
  58.   
  59.         jButton1.setFont(new java.awt.Font("標楷體"114)); // NOI18N  
  60.         jButton1.setText("左上");  
  61.         jButton1.addActionListener(new java.awt.event.ActionListener() {  
  62.             public void actionPerformed(java.awt.event.ActionEvent evt) {  
  63.                 jButton1ActionPerformed(evt);  
  64.             }  
  65.         });  
  66.         jPanel1.add(jButton1);  
  67.   
  68.         jButton3.setFont(new java.awt.Font("標楷體"114)); // NOI18N  
  69.         jButton3.setText("左下");  
  70.         jButton3.addActionListener(new java.awt.event.ActionListener() {  
  71.             public void actionPerformed(java.awt.event.ActionEvent evt) {  
  72.                 jButton3ActionPerformed(evt);  
  73.             }  
  74.         });  
  75.         jPanel1.add(jButton3);  
  76.   
  77.         jTextField1.setFont(new java.awt.Font("新細明體"112)); // NOI18N  
  78.         jTextField1.setHorizontalAlignment(javax.swing.JTextField.CENTER);  
  79.         jTextField1.setText("0");  
  80.         jPanel1.add(jTextField1);  
  81.   
  82.         jButton2.setFont(new java.awt.Font("標楷體"114)); // NOI18N  
  83.         jButton2.setText("右上");  
  84.         jButton2.addActionListener(new java.awt.event.ActionListener() {  
  85.             public void actionPerformed(java.awt.event.ActionEvent evt) {  
  86.                 jButton2ActionPerformed(evt);  
  87.             }  
  88.         });  
  89.         jPanel1.add(jButton2);  
  90.   
  91.         jButton4.setFont(new java.awt.Font("標楷體"114)); // NOI18N  
  92.         jButton4.setText("右下");  
  93.         jButton4.addActionListener(new java.awt.event.ActionListener() {  
  94.             public void actionPerformed(java.awt.event.ActionEvent evt) {  
  95.                 jButton4ActionPerformed(evt);  
  96.             }  
  97.         });  
  98.         jPanel1.add(jButton4);  
  99.   
  100.         jTabbedPane1.setBackground(new java.awt.Color(2042040));  
  101.         jTabbedPane1.setOpaque(true);  
  102.   
  103.         jTable1.setModel(new javax.swing.table.DefaultTableModel(  
  104.             new Object [][] {  
  105.   
  106.             },  
  107.             new String [] {  
  108.   
  109.             }  
  110.         ));  
  111.         jScrollPane1.setViewportView(jTable1);  
  112.   
  113.         jTabbedPane1.addTab("Xin", jScrollPane1);  
  114.   
  115.         jLabel1.setFont(new java.awt.Font("新細明體"118)); // NOI18N  
  116.         jLabel1.setText("9A490021");  
  117.   
  118.         javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());  
  119.         getContentPane().setLayout(layout);  
  120.         layout.setHorizontalGroup(  
  121.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)  
  122.             .addComponent(jTabbedPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 489, Short.MAX_VALUE)  
  123.             .addGroup(layout.createSequentialGroup()  
  124.                 .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 330, javax.swing.GroupLayout.PREFERRED_SIZE)  
  125.                 .addGap(181818)  
  126.                 .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)  
  127.                 .addContainerGap())  
  128.         );  
  129.         layout.setVerticalGroup(  
  130.             layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)  
  131.             .addGroup(layout.createSequentialGroup()  
  132.                 .addComponent(jTabbedPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 235, Short.MAX_VALUE)  
  133.                 .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)  
  134.                 .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)  
  135.                     .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)  
  136.                     .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))  
  137.         );  
  138.   
  139.         setSize(new java.awt.Dimension(505311));  
  140.         setLocationRelativeTo(null);  
  141.     }// </editor-fold>                          
  142. /* 
  143.      
  144.                ************** 主要動作************** ↓ 
  145.      
  146.      
  147.     */  
  148.     private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {                                           
  149.         try {  
  150.             Start(magicOdd(Integer.parseInt(jTextField1.getText())));  
  151.         } catch (Exception ex) {  
  152.            JOptionPane.showMessageDialog(null" 無法偶數");  
  153.         }  
  154.     }                                          
  155.     
  156.       
  157.     private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {                                           
  158.         try {  
  159.             Start(magicOdd2(Integer.parseInt(jTextField1.getText())));  
  160.         } catch (Exception ex) {  
  161.            JOptionPane.showMessageDialog(null" 無法偶數");  
  162.         }  
  163.     }                                          
  164.   
  165.     private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {                                           
  166.         try {  
  167.             Start(magicOdd3(Integer.parseInt(jTextField1.getText())));  
  168.         } catch (Exception ex) {  
  169.            JOptionPane.showMessageDialog(null" 無法偶數");  
  170.         }  
  171.     }                                          
  172.   
  173.     private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {                                           
  174.         try {  
  175.             Start(magicOdd4(Integer.parseInt(jTextField1.getText())));  
  176.         } catch (Exception ex) {  
  177.            JOptionPane.showMessageDialog(null" 無法偶數");  
  178.         }  
  179.     }                                          
  180.   
  181.      public void Start(int[][] m ){   
  182.         DefaultTableModel mod = (DefaultTableModel) jTable1.getModel();       
  183.           
  184.         int n= Integer.parseInt(jTextField1.getText());  
  185.         mod.setColumnCount(n);//設定行數   
  186.         mod.setRowCount(0);//清理每列資料  
  187.         int[][] magic = m ;  
  188.          Object[] o = new Object[n];  
  189.           for(int r = 0 ; r <  n ; r++){        
  190.        for(int c = 0 ; c <  n ; c++){     
  191.                o[c] = magic[r][c];  
  192.            }  
  193.            mod.addRow(o);//每列  o索引值以行加入   
  194.          }  
  195.           if(n > 11){  
  196.               n = n - 11;   
  197.               int y = n/2;   
  198.               this.setBounds(00416+(65*y), 176+(y*62));  
  199.                 
  200.               jTabbedPane1.setBounds(00416+(65*y), 176+(y*62));  
  201.           }  
  202.           
  203.               
  204.           
  205.     }  
  206.      public static int[][] magicOdd(int n) throws Exception  {  
  207.                  if(n%2 == 0 ) throw new Exception() ;  
  208.          int[][] Sq = new int[n+1][n+1] ;  
  209.          int i = 0 ; int j = (n+1)/2;  
  210.          for(int index = 1 ; index <= n*n ; index++){  
  211.              if((index%n) == 1){  // 1開頭 (i+1),(j = (n+1)/2;)  得到餘數為1就向下  
  212.                  i++;  
  213.              }else{    
  214.                  i-- ; j--;  //否則 Column Row各退  
  215.              }  
  216.              if(i == 0){  //退到 Row = 0 轉道前頭  
  217.                     i = n ;  
  218.                     }   
  219.               if(j == 0){  //退到 Column= 0 轉道前頭  
  220.                    j = n ;   
  221.                    }  
  222.               Sq[i][j] = index ;  
  223.          }  
  224.          int[][] Sq2 = new int[n][n] ;  
  225.          for(int r = 0 ; r <  Sq2.length  ; r++){  
  226.              for(int c = 0 ; c <  Sq2.length  ; c++){  
  227.                  Sq2[r][c] = Sq[r+1][c+1];  
  228.              }  
  229.          }  
  230.          return  Sq2;  
  231.      }  
  232.        
  233.       public static int[][] magicOdd2(int n) throws Exception {  
  234.                  if(n%2 == 0 ) throw new Exception() ;  
  235.          int[][] Sq = new int[n+1][n+1] ;  
  236.          int i = 0 ; int j = (n+1)/2;  
  237.          for(int index = 1 ; index <= n*n ; index++){  
  238.              if((index%n) == 1){  // 1開頭 (i+1),(j = (n+1)/2;)  得到餘數為1就向下  
  239.                  i++;  
  240.              }else{    
  241.                  i-- ; j++;  //否則 Column Row各退  
  242.              }  
  243.              if(i == 0){  //退到 Row = 0 轉道前頭  
  244.                     i = n ;  
  245.                     }   
  246.               if(j > n ){  //退到 Column= 0 轉道前頭  
  247.                    j = 1 ;   
  248.                    }  
  249.               Sq[i][j] = index ;  
  250.          }  
  251.          int[][] Sq2 = new int[n][n] ;  
  252.          for(int r = 0 ; r <  Sq2.length  ; r++){  
  253.              for(int c = 0 ; c <  Sq2.length  ; c++)  
  254.                  Sq2[r][c] = Sq[r+1][c+1];  
  255.              }  
  256.                    return  Sq2;   
  257.      }  
  258.        
  259.       public static int[][] magicOdd3(int n) throws Exception {  
  260.                  if(n%2 == 0 ) throw new Exception() ;  
  261.          int[][] Sq = new int[n+1][n+1] ;  
  262.          int i = n+1 ; int j = (n+1)/2;  
  263.          for(int index = 1 ; index <= n*n ; index++){  
  264.              if((index%n) == 1){  // 1開頭 (i+1),(j = (n+1)/2;)  得到餘數為1就向下  
  265.                  i--;  
  266.              }else{    
  267.                  i++ ; j--;  //否則 Column Row各退  
  268.              }  
  269.              if(i >  n){  //退到 Row = 0 轉道前頭  
  270.                     i = 1 ;  
  271.                     }   
  272.               if(j  == 0 ){  //退到 Column= 0 轉道前頭  
  273.                    j = n;   
  274.                    }  
  275.               Sq[i][j] = index ;  
  276.          }  
  277.          int[][] Sq2 = new int[n][n] ;  
  278.          for(int r = 0 ; r <  Sq2.length  ; r++){  
  279.              for(int c = 0 ; c <  Sq2.length  ; c++){  
  280.                  Sq2[r][c] = Sq[r+1][c+1];  
  281.              }  
  282.          }  
  283.          return  Sq2;  
  284.      }  
  285.       public static int[][] magicOdd4(int n) throws Exception{   
  286.                  if(n%2 == 0 ) throw new Exception() ;  
  287.          int[][] Sq = new int[n+1][n+1] ;  
  288.          int i = n+1 ; int j = (n+1)/2;  
  289.          for(int index = 1 ; index <= n*n ; index++){  
  290.              if((index%n) == 1){  // 1開頭 (i+1),(j = (n+1)/2;)  得到餘數為1就向下  
  291.                  i--;  
  292.              }else{    
  293.                  i++ ; j++;  //否則 Column Row各退  
  294.              }  
  295.              if(i >  n){  //退到 Row = 0 轉道前頭  
  296.                     i = 1 ;  
  297.                     }   
  298.               if(j  > n ){  //退到 Column= 0 轉道前頭  
  299.                    j = 1;   
  300.                    }  
  301.               Sq[i][j] = index ;  
  302.          }  
  303.          int[][] Sq2 = new int[n][n] ;  
  304.          for(int r = 0 ; r <  Sq2.length  ; r++){  
  305.              for(int c = 0 ; c <  Sq2.length  ; c++){  
  306.                  Sq2[r][c] = Sq[r+1][c+1];  
  307.              }  
  308.          }  
  309.          return  Sq2;  
  310.      }  
  311. /* 
  312.      
  313.                ************** 主要動作**************  ↑ 
  314.      
  315.      
  316.     */  
  317.     /** 
  318.      * @param args the command line arguments 
  319.      */  
  320.     public static void main(String args[]) {  
  321.         /* Set the Nimbus look and feel */  
  322.         //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">  
  323.         /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel. 
  324.          * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html  
  325.          */  
  326.         try {  
  327.             for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {  
  328.                 if ("Nimbus".equals(info.getName())) {  
  329.                     javax.swing.UIManager.setLookAndFeel(info.getClassName());  
  330.                     break;  
  331.                 }  
  332.             }  
  333.         } catch (ClassNotFoundException ex) {  
  334.             java.util.logging.Logger.getLogger(MEX.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);  
  335.         } catch (InstantiationException ex) {  
  336.             java.util.logging.Logger.getLogger(MEX.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);  
  337.         } catch (IllegalAccessException ex) {  
  338.             java.util.logging.Logger.getLogger(MEX.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);  
  339.         } catch (javax.swing.UnsupportedLookAndFeelException ex) {  
  340.             java.util.logging.Logger.getLogger(MEX.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);  
  341.         }  
  342.         //</editor-fold>  
  343.   
  344.         /* Create and display the form */  
  345.         java.awt.EventQueue.invokeLater(new Runnable() {  
  346.             public void run() {  
  347.                 new MEX().setVisible(true);  
  348.             }  
  349.         });  
  350.     }  
  351.   
  352.     // Variables declaration - do not modify                       
  353.     private javax.swing.JButton jButton1;  
  354.     private javax.swing.JButton jButton2;  
  355.     private javax.swing.JButton jButton3;  
  356.     private javax.swing.JButton jButton4;  
  357.     private javax.swing.JLabel jLabel1;  
  358.     private javax.swing.JPanel jPanel1;  
  359.     private javax.swing.JScrollPane jScrollPane1;  
  360.     private javax.swing.JTabbedPane jTabbedPane1;  
  361.     private javax.swing.JTable jTable1;  
  362.     private javax.swing.JTextField jTextField1;  
  363.     // End of variables declaration                     
  364. }  

沒有留言:

張貼留言

注意:只有此網誌的成員可以留言。