//gui5.java -- gui5 sample, @CopyLeft by tsaiwn@cs.nctu.edu.tw
//package ...;  // 指定我這 class 的命名空間 namespace, 阿就是要放哪個目錄啦
import java.applet.*;
import java.awt.*;
import java.awt.event.*;    // Event , EventListener
import javax.swing.*;
  // 注意要寫 public class 不然  Applet 有問題
public class gui5 extends MyApplet implements ActionListener { // 我就是 Applet
   Panel ppp;    // 木板 (面板)
   Button bbb;   // 按鈕
   TextArea tt;    // 文字區
   TextField tx;   // 文字一列的欄
   static boolean isApplication = false;  // for later use

   public static void main(String gg[ ]) {     // Java 的主程式都這樣寫
       isApplication = true;   // Mark as an Application
       JFrame f = new JFrame("Windows 2011");        // 開個窗 f
       f.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
       // JFrame 可以設成按右上角的 X 會關窗, 這樣不必自己處理  WindowEvent !
       gui5 me = new gui5( );     // 模仿 Applet .. // 生出自己 (class gui5 )
       me.init( );      // 因為 Applet 被生出後先做 init( );
       me.start( );     // ..    再做  start( )
       f.add(me, "Center");   // 把 Applet (me) 放入窗 f 內
       f.setSize(688, 520);   // Frame 是 Window, default 用 BorderLayout
       f.setVisible(true);   // f.show( );
   } // main(

   void println(String fmt, Object... oo) { print(fmt+"\n", oo); }
   void print(String fmt, Object... oo){System.out.printf(fmt, oo); }
   void printf(String fmt, Object... oo){System.out.printf(fmt, oo); }
 
   public gui5( ) { 
       super( );   // 可以不寫
       println(" Now doing ...  gui5 constructor !");
   } // Constructor; 注意要寫 public 不然  Applet 有問題
   public void start( ) {
       super.start( );
       println(" Now in start( ) ... kk = " + kk++);
   }// start(
  /////////////////////////////
   public void init( ) {
       System.out.println(" Init ... kk = " + kk++);
       ppp = new Panel( );
       tx=new TextField("你可以在這打字", 38);
       tx.addActionListener(this);  // 由"這" class 的 actionPerformed()處理
         // TextField 是在按下 ENTER 時觸發 ActionEvent
       tx.setForeground(Color.BLUE);
       tx.setFont(new Font("標楷體", Font.BOLD, 24) );  // 24x24

       // note that JLabel is in javax.swing.*
       JLabel labMyPic =    // see MyApplet.java
          createLabelWithPicture("img/mypic.gif", "猜猜我是誰");
       ppp.setLayout( new BorderLayout( ) );
       ppp.add(labMyPic, "West");    // 左
       bbb = new Button(" empty 按這裡看看 ");
       Panel ppbb = new Panel( ); ppbb.add(bbb);  // 先把 bbb 放木板 ppbb
       ppp.add(ppbb, "Center");  // 再把 ppbb 塞入 ppp 木板 Center 但右邊沒東西
       //ppp.setSize(280, 120);    // 好像沒有用ㄟ ?
       bbb.setFont(new Font("標楷體",1,36));
 
       bbb.addActionListener(this);  // 由"這" class 的 actionPerformed()處理

       setLayout(new BorderLayout( ) );  // change Layout Manager 擺設經理
         // 因為 Applet 是 Panel, 其 default Layout Manager 是 FlowLayout
          // 設為  BorderLayout 後就可以指定 東西南北中  五個方位 放入元件
       ppp.setBackground(Color.pink);    // 背景色 粉紅
       tt = new TextArea("Hahaha\n", 5, 20, TextArea.SCROLLBARS_BOTH);
       add(ppp, "North");    /// ppp 放 我 (gui5,  是Applet)的 北邊
       add(tt, "Center");    /// tt 文字區塊 放 我 (gui5,  是Applet)的 正中間
       tt.setFont(new Font("Ariel",0,32));   // 字體大一些
       Panel pn = new Panel( ); 
       Panel ptt=new Panel( );
       Panel pOut = new Panel( );  // 裡面要塞入 pn 和 ptt 
       pOut.setLayout(new BorderLayout( ));   // 外匡面版用 BorderLayout
       pOut.add(pn, "South");  // pn 塞在  pOut 這 Panel 內 :-) 木板上再放木板!
       pOut.add(ptt,"North");  // pOut 內塞兩個 Panel : pn, ptt
       //  現在 pOut 木板上有兩塊木板:  ptt 上, 下是 pn  (南)
       ptt.add(new Button("請在右邊打字: "));     // 放入 ptt
       ptt.add(tx);      // 依序 放入 ptt
      /// 再來, pn 這木板上 放入很多按鈕 ! 所以用 array 陣列 比較方便 !
       pn.setBackground(Color.yellow); 
       bt = new Button[98];  // 只會生出 98 個參考 (阿其實就是 C++ 的指標啦)
          // 上列生出 98 個 參考(Reference) 不會用很多記憶體 :-)
          // 實際上會生出幾個按鈕是由 bMsg.length 決定 (看下列)
       for(int i=1; i< bMsg.length; ++i) {     // 這樣寫是個好習慣!
                            // 因為你去改 bMsg 陣列增加元素也不用來改這裡  :-)
           bt[i] = new Button(bMsg[i]);   // 真正生出六個 Button (注意 0 故意不用)
           pn.add(bt[i]); bt[i].setFont(new Font("細明體",1,16));
                 // 注意上列是在做勞作 :-)  把按鈕加入 pn 木板 !
           bt[i].addActionListener(this);    // 請求監聽 "動作事件"(ActionEvent)
               // 注意上列是要求監聽 bb[i] 喔 !
       } // for int i
       add(pOut, "South");   // 把 pOut 塞入我(gui5)這個 Applet 南邊
       bt[3].setForeground(Color.red); 
       validate( );  //  做了一些對圖形元件 的動作記得要做這  validate( ); 
   } // init(

   Button bt[ ];   // 宣告說 bt[ ]  是 Button 陣列 (array)
   String bMsg[ ] = {"000", "Bt001", "btBBB",
       "哈哈哈會讀書入", "bt西西", "BT五", "六 六 大順 囉", "Bye",
         "張三", "李四", "十全十美"    }; 
   int kk = 0;  // Global variable to this class

   public void paint(Graphics g) {
       System.out.println(" Paint kk = " + kk++);
       // do drawing something ...
       validate( );  //
   } // paint(

  ////////   ===============================================  ///////////////

   public void actionPerformed(ActionEvent e) {
       Object who = e.getSource( );    // 誰被按了一下 ?
       String gg = "You press " + e.getActionCommand( );
                // getActionCommand( ) 也可用來查知是按了啥!
       if(who == bbb) { 
         cid =  (cid+1) % yyc.length;   // 換到下一個顏色, 注意到尾巴要繞回頭
         bbb.setForeground( yyc[cid] ); return;
       } // if bbb

       if(who == bt[7]) System.exit(0);
       else if(who == bt[1]) ppp.setBackground(Color.red);
       else if(who == bt[6]) { ppp.setBackground(Color.green);
           tt.append("如果 林志玲 來演講, 你會去聽嗎? ");  } // if
       bbb.setLabel(gg);
       tt.append(gg+"\n");
       if(who == bt[3] || who==tx)  { // tx is a TextField 按 ENTER 會到這
           tt.append("讀到 " + tx.getText( ) + "\n");
           ppp.setBackground(Color.gray);  }
       repaint( );  // 會偷叫 update(Graphics);
   } // actionPerformed( 

   public void update(Graphics g) {  // 只是看看何時會跑過來這
       System.out.println(" update ==> kk = " + kk++);
       super.update(g); //  叫用上層(super class)的 update( )
   } // update(  will called by repaint( )

   public void stop( ) {  //跑去別的網頁會到這
       super.stop( );
       println(" STOP ..now in stop( ) ... ");  // 只是看看何時會跑過來這
   }// stop(

   public void destroy( ) {   // 當 Browser 被關閉會到這
       println(" ! now in destroy( ) ... ");  // 只是看看何時會跑過來這
       super.destroy( );
   }// destroy(
  ////////   ===========================================  ///////////////
   ///gui2.java
   Color yyc[ ] = { Color.red, Color.green, Color.blue
       , Color.PINK, new Color(198,222,66), new Color(198,8,198) 
    };   // 注意這array yyc[ ]結束也要有分號; 因為大括號內是 Data !
   int cid = -1;

   int rand( ) { return (int) (32768* Math.random( ) ); }
} // class
