發新話題

[JAVA] 魔術方塊

[JAVA] 魔術方塊

引用:
/*****************************************
說明:請在視窗上配置一文字方塊及一按鈕,在文字方塊
上輸入階數n(n<=15,且為奇數),按上述按鈕後,輸出之
魔術方塊,該方塊之各列和各行和與對角線之和均相等。
程式中必須有須有判斷範圍的程式,若是超出題目所訂定
的數值範圍則要求重新輸入。
*************************************************/
import java.awt.*;
import javax.swing.*;
import java.awt.event.*;
//想想還有沒有其他的東西要import
public class f4 extends JFrame implements ActionListener//名稱改一改
{
    Container c;
    //設定UI元件
    JLabel ta,tans;
    JTextField ia;
    JTextArea ans;
    JButton bot;
    //設定共用的變數與類別
    public f4()   //建構元,名稱改一改
    {
        super("魔術方塊");
        c=getContentPane();//取得ContentPane
        //設定版面設定
        c.setLayout(new FlowLayout(FlowLayout.CENTER));//設定為用flowlayout
        //初始化UI元件
        ta=new JLabel("請輸入階數");
        tans=new JLabel("結果");
        ia=new JTextField("5",5);
        bot=new JButton("計算");
        ans=new JTextArea(11,50);
        ans.setEditable(false);
        //將UI元件加入版面中
        c.add(ta);
        c.add(ia);
        c.add(bot);
        c.add(tans);
        c.add(ans);
        //設定UI元件與滑鼠的事件觸發傾聽者
        bot.addActionListener(this);
        setSize(640,480);//設定size,顯示出去
        show();
    }
    public void paint(Graphics g)  
    {
            super.paint(g);//畫出元件
    }
    //UI元件事件處理類別寫在這裡
     //滑鼠事件處理類別寫在這裡
    public void actionPerformed(ActionEvent e)
    {
      int i,j,scale,cnt;
          int x_pt,y_pt,nx_pt,ny_pt;
          int matrix[][]=new int [15][15];
      //初始化陣列
      for (i=0;i<15;i++)
                                 for (j=0;j<15;j++)
                                         matrix[j]=0;
      scale=Integer.parseInt(ia.getText());
      if ((scale%2)==0 ) //長度超過或不足
      {
                        ans.setText("請輸入奇數!");
            repaint();
            return;
      }
          else if ( scale<0 || scale>15)  
      {
          ans.setText("輸入數字太大或太小請重新輸入");
          repaint();
          return;
      }
      //正式開始算
      y_pt=0;
          x_pt=scale/2;
          cnt=scale*scale;
          for (i=1;i<=cnt;i=i+1)
          {
                        matrix[y_pt][x_pt]=i;
                        ny_pt=y_pt-1;
                        if (ny_pt<0) ny_pt=scale-1;
                        nx_pt=x_pt+1;
                        if (nx_pt>=scale) nx_pt=0;
                        if (matrix[ny_pt][nx_pt]!=0) //已填過且不是最後一個
                        {
                                ny_pt=y_pt+1;//跳到下一列
                                if (ny_pt>=scale) ny_pt=0;
                               nx_pt=x_pt; //X保持原狀
                                
                        }
                        x_pt=nx_pt;
                        y_pt=ny_pt;
           }
           /*****印出方陣*********/
       ans.setText("");
           for (i=0;i<scale;i=i+1)
           {
                        for (j=0;j<scale;j=j+1)
                                         ans.append(matrix[j]+"\t");
                        ans.append("\n");
           }
        repaint();
    }

  /***主程式***/
    public static void main(String args[]) //程式起點
    {
        f4 app=new f4(); //名稱改一改,啟動UI元件
        app.addWindowListener(new WindowAdapter(){
                public void windowClosing(WindowEvent e)
            {
                System.exit(0);
            }
        }); //處理視窗關閉要求
    }
}

TOP

這個一定要下載的.
謝謝!

TOP

下來看看好玩不好玩!~~

TOP

發新話題

本站所有圖文均屬網友發表,僅代表作者的觀點與本站無關,如有侵權請通知版主會盡快刪除。