- //import static com.sun.xml.internal.fastinfoset.alphabet.BuiltInRestrictedAlphabets.table;
- import java.awt.Color;
- import java.awt.Dimension;
- import java.util.Vector;
- import java.util.logging.Level;
- import java.util.logging.Logger;
- import javax.swing.JOptionPane;
- import javax.swing.table.DefaultTableModel;
- /*
- * To change this license header, choose License Headers in Project Properties.
- * To change this template file, choose Tools | Templates
- * and open the template in the editor.
- */
- /**
- *
- * @author user
- */
- public class MEX extends javax.swing.JFrame {
- /**
- * Creates new form MEX
- */
- public MEX() {
- super("n*n魔方陣");
- this.getContentPane().setBackground(new Color(204,204,0));
- initComponents();
- }
- /**
- * This method is called from within the constructor to initialize the form.
- * WARNING: Do NOT modify this code. The content of this method is always
- * regenerated by the Form Editor.
- */
- @SuppressWarnings("unchecked")
- // <editor-fold defaultstate="collapsed" desc="Generated Code">
- private void initComponents() {
- jPanel1 = new javax.swing.JPanel();
- jButton1 = new javax.swing.JButton();
- jButton3 = new javax.swing.JButton();
- jTextField1 = new javax.swing.JTextField();
- jButton2 = new javax.swing.JButton();
- jButton4 = new javax.swing.JButton();
- jTabbedPane1 = new javax.swing.JTabbedPane();
- jScrollPane1 = new javax.swing.JScrollPane();
- jTable1 = new javax.swing.JTable();
- jLabel1 = new javax.swing.JLabel();
- setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
- setBounds(new java.awt.Rectangle(0, 0, 0, 0));
- setMaximumSize(new java.awt.Dimension(434, 236));
- setMinimumSize(new java.awt.Dimension(434, 236));
- setPreferredSize(new java.awt.Dimension(434, 236));
- jPanel1.setLayout(new java.awt.GridLayout());
- jButton1.setFont(new java.awt.Font("標楷體", 1, 14)); // NOI18N
- jButton1.setText("左上");
- jButton1.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- jButton1ActionPerformed(evt);
- }
- });
- jPanel1.add(jButton1);
- jButton3.setFont(new java.awt.Font("標楷體", 1, 14)); // NOI18N
- jButton3.setText("左下");
- jButton3.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- jButton3ActionPerformed(evt);
- }
- });
- jPanel1.add(jButton3);
- jTextField1.setFont(new java.awt.Font("新細明體", 1, 12)); // NOI18N
- jTextField1.setHorizontalAlignment(javax.swing.JTextField.CENTER);
- jTextField1.setText("0");
- jPanel1.add(jTextField1);
- jButton2.setFont(new java.awt.Font("標楷體", 1, 14)); // NOI18N
- jButton2.setText("右上");
- jButton2.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- jButton2ActionPerformed(evt);
- }
- });
- jPanel1.add(jButton2);
- jButton4.setFont(new java.awt.Font("標楷體", 1, 14)); // NOI18N
- jButton4.setText("右下");
- jButton4.addActionListener(new java.awt.event.ActionListener() {
- public void actionPerformed(java.awt.event.ActionEvent evt) {
- jButton4ActionPerformed(evt);
- }
- });
- jPanel1.add(jButton4);
- jTabbedPane1.setBackground(new java.awt.Color(204, 204, 0));
- jTabbedPane1.setOpaque(true);
- jTable1.setModel(new javax.swing.table.DefaultTableModel(
- new Object [][] {
- },
- new String [] {
- }
- ));
- jScrollPane1.setViewportView(jTable1);
- jTabbedPane1.addTab("Xin", jScrollPane1);
- jLabel1.setFont(new java.awt.Font("新細明體", 1, 18)); // NOI18N
- jLabel1.setText("9A490021");
- javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
- getContentPane().setLayout(layout);
- layout.setHorizontalGroup(
- layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addComponent(jTabbedPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 489, Short.MAX_VALUE)
- .addGroup(layout.createSequentialGroup()
- .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, 330, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addGap(18, 18, 18)
- .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
- .addContainerGap())
- );
- layout.setVerticalGroup(
- layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
- .addGroup(layout.createSequentialGroup()
- .addComponent(jTabbedPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 235, Short.MAX_VALUE)
- .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
- .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
- .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
- .addComponent(jLabel1, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)))
- );
- setSize(new java.awt.Dimension(505, 311));
- setLocationRelativeTo(null);
- }// </editor-fold>
- /*
- ************** 主要動作************** ↓
- */
- private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
- try {
- Start(magicOdd(Integer.parseInt(jTextField1.getText())));
- } catch (Exception ex) {
- JOptionPane.showMessageDialog(null, " 無法偶數");
- }
- }
- private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {
- try {
- Start(magicOdd2(Integer.parseInt(jTextField1.getText())));
- } catch (Exception ex) {
- JOptionPane.showMessageDialog(null, " 無法偶數");
- }
- }
- private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {
- try {
- Start(magicOdd3(Integer.parseInt(jTextField1.getText())));
- } catch (Exception ex) {
- JOptionPane.showMessageDialog(null, " 無法偶數");
- }
- }
- private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {
- try {
- Start(magicOdd4(Integer.parseInt(jTextField1.getText())));
- } catch (Exception ex) {
- JOptionPane.showMessageDialog(null, " 無法偶數");
- }
- }
- public void Start(int[][] m ){
- DefaultTableModel mod = (DefaultTableModel) jTable1.getModel();
- int n= Integer.parseInt(jTextField1.getText());
- mod.setColumnCount(n);//設定行數
- mod.setRowCount(0);//清理每列資料
- int[][] magic = m ;
- Object[] o = new Object[n];
- for(int r = 0 ; r < n ; r++){
- for(int c = 0 ; c < n ; c++){
- o[c] = magic[r][c];
- }
- mod.addRow(o);//每列 o索引值以行加入
- }
- if(n > 11){
- n = n - 11;
- int y = n/2;
- this.setBounds(0, 0, 416+(65*y), 176+(y*62));
- jTabbedPane1.setBounds(0, 0, 416+(65*y), 176+(y*62));
- }
- }
- public static int[][] magicOdd(int n) throws Exception {
- if(n%2 == 0 ) throw new Exception() ;
- int[][] Sq = new int[n+1][n+1] ;
- int i = 0 ; int j = (n+1)/2;
- for(int index = 1 ; index <= n*n ; index++){
- if((index%n) == 1){ // 1開頭 (i+1),(j = (n+1)/2;) 得到餘數為1就向下
- i++;
- }else{
- i-- ; j--; //否則 Column Row各退
- }
- if(i == 0){ //退到 Row = 0 轉道前頭
- i = n ;
- }
- if(j == 0){ //退到 Column= 0 轉道前頭
- j = n ;
- }
- Sq[i][j] = index ;
- }
- int[][] Sq2 = new int[n][n] ;
- for(int r = 0 ; r < Sq2.length ; r++){
- for(int c = 0 ; c < Sq2.length ; c++){
- Sq2[r][c] = Sq[r+1][c+1];
- }
- }
- return Sq2;
- }
- public static int[][] magicOdd2(int n) throws Exception {
- if(n%2 == 0 ) throw new Exception() ;
- int[][] Sq = new int[n+1][n+1] ;
- int i = 0 ; int j = (n+1)/2;
- for(int index = 1 ; index <= n*n ; index++){
- if((index%n) == 1){ // 1開頭 (i+1),(j = (n+1)/2;) 得到餘數為1就向下
- i++;
- }else{
- i-- ; j++; //否則 Column Row各退
- }
- if(i == 0){ //退到 Row = 0 轉道前頭
- i = n ;
- }
- if(j > n ){ //退到 Column= 0 轉道前頭
- j = 1 ;
- }
- Sq[i][j] = index ;
- }
- int[][] Sq2 = new int[n][n] ;
- for(int r = 0 ; r < Sq2.length ; r++){
- for(int c = 0 ; c < Sq2.length ; c++)
- Sq2[r][c] = Sq[r+1][c+1];
- }
- return Sq2;
- }
- public static int[][] magicOdd3(int n) throws Exception {
- if(n%2 == 0 ) throw new Exception() ;
- int[][] Sq = new int[n+1][n+1] ;
- int i = n+1 ; int j = (n+1)/2;
- for(int index = 1 ; index <= n*n ; index++){
- if((index%n) == 1){ // 1開頭 (i+1),(j = (n+1)/2;) 得到餘數為1就向下
- i--;
- }else{
- i++ ; j--; //否則 Column Row各退
- }
- if(i > n){ //退到 Row = 0 轉道前頭
- i = 1 ;
- }
- if(j == 0 ){ //退到 Column= 0 轉道前頭
- j = n;
- }
- Sq[i][j] = index ;
- }
- int[][] Sq2 = new int[n][n] ;
- for(int r = 0 ; r < Sq2.length ; r++){
- for(int c = 0 ; c < Sq2.length ; c++){
- Sq2[r][c] = Sq[r+1][c+1];
- }
- }
- return Sq2;
- }
- public static int[][] magicOdd4(int n) throws Exception{
- if(n%2 == 0 ) throw new Exception() ;
- int[][] Sq = new int[n+1][n+1] ;
- int i = n+1 ; int j = (n+1)/2;
- for(int index = 1 ; index <= n*n ; index++){
- if((index%n) == 1){ // 1開頭 (i+1),(j = (n+1)/2;) 得到餘數為1就向下
- i--;
- }else{
- i++ ; j++; //否則 Column Row各退
- }
- if(i > n){ //退到 Row = 0 轉道前頭
- i = 1 ;
- }
- if(j > n ){ //退到 Column= 0 轉道前頭
- j = 1;
- }
- Sq[i][j] = index ;
- }
- int[][] Sq2 = new int[n][n] ;
- for(int r = 0 ; r < Sq2.length ; r++){
- for(int c = 0 ; c < Sq2.length ; c++){
- Sq2[r][c] = Sq[r+1][c+1];
- }
- }
- return Sq2;
- }
- /*
- ************** 主要動作************** ↑
- */
- /**
- * @param args the command line arguments
- */
- public static void main(String args[]) {
- /* Set the Nimbus look and feel */
- //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
- /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
- * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
- */
- try {
- for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
- if ("Nimbus".equals(info.getName())) {
- javax.swing.UIManager.setLookAndFeel(info.getClassName());
- break;
- }
- }
- } catch (ClassNotFoundException ex) {
- java.util.logging.Logger.getLogger(MEX.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- } catch (InstantiationException ex) {
- java.util.logging.Logger.getLogger(MEX.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- } catch (IllegalAccessException ex) {
- java.util.logging.Logger.getLogger(MEX.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- } catch (javax.swing.UnsupportedLookAndFeelException ex) {
- java.util.logging.Logger.getLogger(MEX.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
- }
- //</editor-fold>
- /* Create and display the form */
- java.awt.EventQueue.invokeLater(new Runnable() {
- public void run() {
- new MEX().setVisible(true);
- }
- });
- }
- // Variables declaration - do not modify
- private javax.swing.JButton jButton1;
- private javax.swing.JButton jButton2;
- private javax.swing.JButton jButton3;
- private javax.swing.JButton jButton4;
- private javax.swing.JLabel jLabel1;
- private javax.swing.JPanel jPanel1;
- private javax.swing.JScrollPane jScrollPane1;
- private javax.swing.JTabbedPane jTabbedPane1;
- private javax.swing.JTable jTable1;
- private javax.swing.JTextField jTextField1;
- // End of variables declaration
- }
2018年4月16日 星期一
Magic Suare :
訂閱:
張貼留言 (Atom)

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