SlideShare a Scribd company logo
1 of 12
Download to read offline
27/12/53




                                                                            Introduction
                                                                           Applet
                                                                              คือโปรแกรมขนาดเล็กๆ ในภาษาจาวา ทีถูกกําหนดใหใชได
                                                                                                                ่
                                                                               บนเว็บเพจ โดยแสดงผลผานเว็บเบราเซอร
                     Chapter 8:                                               เปนโปรแกรมทีไมสามารถรันไดดวยตัวเอง แตจะตองถูก
                                                                                            ่
              การสรางและใชงาน Applet                                         เรียกใชโดยเบราเซอร
                                                                              สามารถสราง GUI และกราฟกที่เปนรูปภาพได และสามาถ
                                                                               ตอบสนองเหตุการณตางๆที่เกิดขึ้นกับหนาจอ Applet นันได
                                                                                                                                  ้
            662327 Advanced Computer Programming                               เชนการคลิกเมาส หรือการกดปุมคียบอรด
                                                                                                           
                                                                              โดยใชคลาส JApplet ในชุดของ javax.swing
                   Somchai Cheingpongpan
            Department of Information Technology
                                                                                                                                         8-2




หลักการของการสราง Applet                                                   หลักการของการสราง Applet
   Java Applet นันเปนออบเจ็กตที่สรางมาจากคลาส
                  ้                                                        หลังจากสรางคลาสยอยจากคลาส JApplet แลว ซึ่งก็จะไดเมธอด
    java.applet.Applet หรือเรียกไดวาเปน Subclass ของ Applet              ตางๆที่ถูกกําหนดไวในคลาส JApplet ตามกฏการถายทอด โดยมี
   โดย Applet มีหนาที่เพียงแคใชในการสรางคลาสยอย หรือ                  บางเมธอดทีี่สามารถใสคําสั่งเพิ่มลงไป และบางเมธอดไมตองทํา
                                                                                                 ใ     ั       ไป            ไ
    ออบเจ็กตขนมาไมไดทําอะไรมากกวานัน ดังนันจึงเปนเพียงแค
                 ึ้                      ้     ้                            อะไรกับมันเลย
    พื้นที่วางๆ บนหนาจอที่ไมมีการตอบสนองใดๆ                             ใน JApplet นันมีเมธอดทีทํางานอยูดังนี้
                                                                                         ้         ่
    การสราง Applet ที่มีประโยชนนน โปรแกรมเมอรจะตองเพิ่มเติม
                                  ั้
                                                                             public class MyJApplet extends JApplet {
                                                                               public void init() {
    คลาสยอยซึ่งขยาย(extends)มาจากคลาส Applet ซึ่งมีรูปแบบ                      }

    ดงน
    ดังนี้
                                                                                public void start() {
                                                                                }
        public class myJApplet extends JApllet {                                public void paint(Graphics g) {
                                                                                }
               // สวนของโปรแกรม                                                public void stop() {
                                                                                }
        }                                                                       public void destroy() {
                                                                                }
                                                                  8-3        }                                                           8-4




                                                                                                                                                     1
27/12/53




หลักการของการสราง Applet                                                     หลักการของการสราง Applet
   เมธอด init()                                                                 เมธอด stop()
    ใชสําหรับกําหนดคาเริ่มตนหรือการ initialize ใหกับคลาส ซึ่งจะ               การหยุดของ Applet หมายถึง การที่ผูใชออกจากหนาจอปจจุบน     ั
    เริ่มเปนเมธอดแรกเมืื่อมีีการโหลดเขามาทํางาน ซึึ่งทํางานไดครั้ง
          ป                     โ                           ไ      ั             ที่กําลังทํางาน Applet อยู หรือการที่ Applet หยุดตัวเองโดยเรียก
    เดียว                                                                         เมธอด stop() โดยตรง
   เมธอด start()                                                                เมธอด destroy()
    เปนเมธอดที่ทํางานตอจากเมธอด init โดยไมจําเปนตองเขียน
                                                                                  เปนเมธอดที่บอกให Applet ทําการคืนหนวยความจําตางๆใหกบ
                                                                                                                                          ั
    เมธอด start() ก็ได หากไมไดใหทําอะไร โดยสามารถเกิดขึนกี่ ้
    ครั้งก็ไดในชวงชีวิตของ JApplet                                              ระบบกอนที่จะทําการปดเบราเซอร
   เมธอด paint()
    เปนเมธอดที่ใชสําหรับแสดงขอมูลบนหนาจอ ไมมาจะเปน
    ขอความ รูปภาพ รูปทรงตางๆ โดยสามารถเกิดขึนกี่ครั้งก็ได
                                                     ้
    ในชวงชีวิตของ JApplet ซึ่งมีพารามิเตอรเปน คลาส Graphics
                                                                        8-5                                                                            8-6




            หลักการของการสราง Applet                                         การใชงาน Applet กับ HTML
    import java.awt.*;
                                                                                 Applet Tag
    import javax.swing.*;
    public class MyJApplet extends JApplet {                                      เนื่องจากการใชงาน Applet นันตองทํางานบนเว็บเพจที่เขียน
                                                                                                                   ้
        public void init() {
                                                                                  ดวยภาษา H ML โ ใ แท็ก <APPLET> ใ ในการเพิ่ม Applet
                                                                                             HTML โดยใช ็          PPLE ใช           ิ       l
                                                                                  ใหกับเว็บเพจหนึ่งๆ ซึ่งคูกับ </APPLET> มีแอตทริบิวตที่ตองใช
            System.out.println("Init");
        }
        public void start() {                                                     งานดังนี้
                                                                                    CODE ใชสาหรับกําหนดชื่อของคลาส Applet ที่ตองการทํางาน
                                                                                               ํ
            System.out.println("Start");
        }
        public void paint(Graphics g) {                                             CODEBASE ใชสาหรับกําหนดไดเรกทอรีของไฟลคลาสที่ตองการ
                                                                                                      ํ
            System.out.println("Paint");                                              ใชงาน
                                                                                      width ใ สําหรับกําหนดขนาดความกวางของพืื้นทีี่ Applet ทีี่แสดง
                                                                                            ใช
        }
                                                                                  
        public void stop() {
            System.out.println("Stop");                                               ในหนาเวบเพจ
        }                                                                            height ใชสาหรับกําหนดขนาดความสูงของพื้นที่ Applet ที่แสดง
                                                                                                 ํ
        public void destroy() {
                                                                                      ในหนาเวบเพจ
            System.out.println("Destroy");
        }
    }                                                                   8-7                                                                            8-8




                                                                                                                                                                   2
27/12/53




การใชงาน Applet กับ HTML                                             การใชงาน Applet กับ HTML
   ตัวอยาง                                                             PARAM Tag
    <html>                                                                โดยปกติแลว Applet สามารถสามารถมีพารามิเตอรที่สงมาจาก
    <body>                                                                เพจ HTML ได โดยใชแท็ก <PARAM> ซึ่งตองใชอยูในแท็ก
                                                                          <APPLET> และ </APPLET> เทานัน มีแอตทริบิวตที่ตองใชงาน
    <applet code=“MyJApplet.class” height=“200” width=“300”>
    </applet>
                                                                                                       ้                  
    </body>                                                               ดังนี้
    </html>
                                                                           NAME ใชสําหรับกําหนดชือของพารามิเตอร
                                                                                                   ่
    <html>                                                                 VALUE ใชสําหรับกําหนดคาของแตละพารามิเตอร
    <body>
        y
    <applet code=“MyJApplet.class” codebase=“classes”                    การเรียกใชพารามิเตอร
    height=“200” width=“300”>
    </applet>
                                                                          เมื่อกําหนดคคาพารามิเตอรแลว เมื่อตองการใชคาก็เรียกผานเมธ
    </body>                                                               อด getParameter() โดยกําหนดชือใหตรงกับที่กําหนดในแท็ก
                                                                                                              ่
    </html>                                                               <PARAM>
                                                               8-9                                                                          8-10




การใชงาน Applet กับ HTML                                             การใชงาน Applet กับ HTML
   ตัวอยาง                                                             ตัวอยาง
<html>                                                                    import java.awt.*;
<body>                                                                    import javax.swing.*;
<applet code=“ParameterJApplet.class” height=“200”
width=“400”>                                                              public class ParameterJApplet extends JApplet {
<param name=“mesg” value=“Hello, I’m your first
parameter”>
                                                                             public void paint(Graphics g) {
<param name=“font” value=“Times New Roman”>                                    String message = getParameter(“mesg”);
<param name=“size” value=“30”>                                                 String fontname = getParameter(“font”);
</applet>                                                                      int fontSize = Integer.parseInt(
</body>                                                                                             getParameter(“size”));
</html>                                                                        Font textFont = new Font(fontName, Font.BOLD,
                                                                                                     fontSzie);
                                                                               g.setFont(textFont);
                                                                               g.drawDtring(message, 10, 30);
                                                                             }
                                                               8-11       }                                                                 8-12




                                                                                                                                                         3
27/12/53




 การใชงาน Applet กับ HTML                                           การใชงานกราฟก (Graphics)
   ผลลัพธ                                                            จาวาไดเตรียมคลาส Graphics ไวในใชงานทางดานกราฟก
                                                                        ซึ่งอยุในชุดของ java.awt โดยมีเมธอดหลักที่ใชในการแสดง
                                                                        รูปทรงและการระบายสีดังนีี้
                                                                                             ี
                                                                         drawString
                                                                         drawLine
                                                                         drawRect,fillRect,clearRect,
                                                                           drawRoundRct,fillRoundRect
                                                                         drawOval, fillOval
                                                                         drawArc, fillArc




                                                             8-13                                                                 8-14




 คําสั่ง drawString                                                  คําสั่ง drawLine
   เปนเมธอดที่ใชแสดงขอความกราฟกบนหนาจอ มีรปแบบดังนี้
                                                ู                      เปนเมธอดที่ใชวาดเสนตรงบนหนาจอ มีรูปแบบคําสั่งดังนี้


    public void drawString (String msg, int x, int y)                   public void drawLine (int x1, int y1, int x2, int y2)


        msg ขอความทีตองการแสดง
                     ่                                                      x1 คาตําแหนงของแกน x ของจุดตน
        x คาตําแหนงของแกน x เปนพิกเซลบนหนาจอ                            y1 คาตําแหนงของแกน y ของจุดตน
        y คาตําแหนงของแกน y เปนพิกเซลบนหนาจอ
                แ นง งแ น เ          เ ล น น                                x
                                                                            x2 คาตําแหนงของแกน x ของจดปลาย
                                                                                     แ นง งแ น       ง ุ ล
ตัวอยาง                                                                    y2 คาตําแหนงของแกน y ของจุดปลาย
        g.drawString(“Hello,World”, 30, 20);                        ตัวอยาง
                                                                            g.drawLine(25, 30, 200, 120);

                                                             8-15                                                                 8-16




                                                                                                                                               4
27/12/53




 คําสั่ง drawRect                                                               คําสั่ง fillRect
   เปนเมธอดที่ใชวาดรูปสี่เหลี่ยมที่ไมมีการระบายสี มีรูปแบบคําสั่ง             เปนเมธอดที่ใชวาดรูปสี่เหลี่ยมที่มีการระบายสีดวยสี foreground
                                                                                                                                  
    ดังนี้                                                                         มีรูปแบบคําสั่งดังนี้

    public void drawRect (int x, int y, int width, int height)                     public void fillRect (int x, int y, int width, int height)

       x คาตําแหนงของแกน x ของจุดมุมซายบนของรูป                                    x คาตําแหนงของแกน x ของจุดมุมซายบนของรูป
       y คาตําแหนงของแกน y ของจุดมุมซายบนของรูป                                    y คาตําแหนงของแกน y ของจุดมุมซายบนของรูป
       width คาความกวางของรปสี่เหลี่ยม
               คาความกวางของรูปสเหลยม                                                 width คาความกวางของรปสี่เหลี่ยม
                                                                                              คาความกวางของรูปสเหลยม
       height คาความสูงของรูปสี่เหลี่ยม                                              height คาความสูงของรูปสี่เหลี่ยม
ตัวอยาง                                                                       ตัวอยาง
       g.drawRect( 35, 30, 120, 100);                                                 g.fillRect( 35, 30, 120, 100);

                                                                        8-17                                                                           8-18




 คําสั่ง clearRect                                                              คําสั่ง drawRoundRect
   เปนเมธอดที่ใชวาดรูปสี่เหลี่ยมที่มีการระบายสีดวยสี background
                                                                                 เปนเมธอดที่ใชวาดรูปสี่เหลี่ยมที่มีมุมเปนมน ทีไมมการระบายสี มี
                                                                                                                                   ่   ี
    มีรูปแบบคําสั่งดังนี้                                                          รูปแบบคําสั่งดังนี้

    public void clearRect(int x, int y, int width, int height)                     public void drawRoundRect(int x, int y, int width,
                                                                                                      int height,int arcWidth, int arcHeight)
       x คาตําแหนงของแกน x ของจุดมุมซายบนของรูป
                                                                                       x คาตําแหนงของแกน x ของจุดมุมซายบนของรูป
       y คาตําแหนงของแกน y ของจุดมุมซายบนของรูป
                                                                                       y คาตําแหนงของแกน y ของจุดมุมซายบนของรูป
       width คาความกวางของรปสี่เหลี่ยม
               คาความกวางของรูปสเหลยม
                                                                                       width คาความกวางของรูปสี่เหลี่ยม
       height คาความสูงของรูปสี่เหลี่ยม
ตัวอยาง                                                                               height คาความสูงของรูปสี่เหลี่ยม
       g.clearRect( 35, 30, 120, 100);                                                 arcWidth คาความกวางของสวนโคงมนรูปสีเหลี่ยม
                                                                                                                              ่
                                                                                       arcHeight คาความสูงของสวนโคงมนรูปสี่เหลี่ยม
                                                                        8-19                                                                       8-20




                                                                                                                                                                    5
27/12/53




 คําสั่ง drawRoundRect                                                       คําสั่ง fillRoundRect
               ( x, y)
                                                                               เปนเมธอดที่ใชวาดรูปสี่เหลี่ยมที่มีมุมเปนมน ทีมีการระบายสีดวยสี
                                                                                                                                ่
                                 arc height                                     foregroundมีรูปแบบคําสั่งดังนี้

                                  a rc width   height                           public void fillRoundRect(int x, int y, int width,
                                                                                                        int height,int arcWidth, int arcHeight)
                                                                                       x คาตําแหนงของแกน x ของจุดมุมซายบนของรูป
                                                                                       y คาตําแหนงของแกน y ของจุดมุมซายบนของรูป
                                                                                       width คาความกวางของรูปสี่เหลี่ยม
                              w idth
                                                                                       height คาความสูงของรูปสี่เหลี่ยม
                                                                                       arcWidth คาความกวางของสวนโคงมนรูปสี่เหลี่ยม
ตัวอยาง                                                                               arcHeight คาความสูงของสวนโคงมนรูปสี่เหลี่ยม
        g.drawRoundRect( 35, 30, 120, 100,20,20);                           ตัวอยาง
                                                                                       g.fillRoundRect( 35, 30, 120, 100,20,20);
                                                                     8-21                                                                            8-22




 คําสั่ง draw3DRect                                                          คําสั่ง fill3DRect
   เปนเมธอดที่ใชวาดรูปสี่เหลี่ยมที่มีมุมเปนมน ทีไมมการระบายสี
                                                    ่   ี                      เปนเมธอดที่ใชวาดรูปสี่เหลี่ยมที่มีมุมเปนมน ทีมีการระบายสีดวยสี
                                                                                                                                ่
    ดวยสี foreground มีรูปแบบคําสั่งดังนี้                                     foregroundมีรูปแบบคําสั่งดังนี้

    public void draw3dRect(int x, int y, int width,                             public void fill3dRect(int x, int y, int width,
                               int height, booleab b)                                                           int height, booleab b)
        x คาตําแหนงของแกน x ของจุดมุมซายบนของรูป                                    x คาตําแหนงของแกน x ของจุดมุมซายบนของรูป
        y คาตําแหนงของแกน y ของจุดมุมซายบนของรูป                                    y คาตําแหนงของแกน y ของจุดมุมซายบนของรูป
        width คาความกวางของรปสี่เหลี่ยม
         idth คาความกวางของรูปสเหลยม                                                   width คาความกวางของรปสี่เหลี่ยม
                                                                                        idth คาความกวางของรูปสเหลยม
        height คาความสูงของรูปสี่เหลี่ยม                                              height คาความสูงของรูปสี่เหลี่ยม
        b คาที่แสดงลักษณะ 3 มิติ                                                      b คาที่แสดงลักษณะ 3 มิติ
ตัวอยาง                                                                    ตัวอยาง
        g.draw3dRect( 35, 30, 120, 100,true);                        8-23              g.fill3dRect( 35, 30, 120, 100,true);                         8-24




                                                                                                                                                                  6
27/12/53




 คําสั่ง drawOval                                                       คําสั่ง drawOval
   เปนเมธอดที่ใชวาดรูปวงกลม หรือวงรี ที่ไมมการระบายสี มี
                                               ี                                   ( x , y)

    รูปแบบคําสั่งดังนี้

    public void drawOval(int x, int y, int width, int height)
                                                                                                                         height
        x คาตําแหนงของแกน x ของจุดมุมซายบนของรูป
        y คาตําแหนงของแกน y ของจุดมุมซายบนของรูป
        width คาความกวางของรูปสี่เหลี่ยม
                                                                                                     w id th
        height คาความสูงของรูปสเหลยม
        h i ht คาความสงของรปสี่เหลี่ยม

                                                                       ตัวอยาง
                                                                               g.drawOval( 35, 30, 120, 100);

                                                                8-25                                                                  8-26




 คําสั่ง fillOval                                                       คําสั่ง drawArc
   เปนเมธอดที่ใชวาดรูปวงกลม หรือวงรี ที่มการระบายสีดวยสี
                                            ี                             เปนเมธอดที่ใชวาดสวนโคงของวงกลม ที่ไมมีการระบายสี มี
    foreground มีรูปแบบคําสั่งดังนี้                                       รูปแบบคําสั่งดังนี้

    public void fillOval(int x, int y, int width, int height)              public void drawArc(int x, int y, int width, int height,
        x คาตําแหนงของแกน x ของจุดมุมซายบนของรูป                                           int startAngle, int arcAngle)
        y คาตําแหนงของแกน y ของจุดมุมซายบนของรูป                            x คาตําแหนงของแกน x ของจุดมุมซายบนของรูป
        width คาความกวางของรูปสี่เหลี่ยม                                     y คาตําแหนงของแกน y ของจุดมุมซายบนของรูป
        height คาความสูงของรูปสเหลยม
        h i ht คาความสงของรปสี่เหลี่ยม                                        width คาความกวางของรปสี่เหลี่ยม
                                                                                idth คาความกวางของรูปสเหลยม
                                                                               height คาความสูงของรูปสี่เหลี่ยม
ตัวอยาง                                                                       startAngle คามุมเริ่มตน
        g.fillOval( 35, 30, 120, 100);                                         arcAngle คามุมของสวนโคง
                                                                8-27                                                                  8-28




                                                                                                                                                   7
27/12/53




 คําสั่ง drawArc                                                              คําสั่ง fillArc

        Positive angles                       Negative angles                   เปนเมธอดที่ใชวาดสวนโคงของวงกลม ที่มีการระบายสีดวยสี
              90°                                    90°                         foreground มีรูปแบบคําสั่งดังนี้
                                                                                 public void fillArc(int x, int y, int width, int height,
                                                                                                       int startAngle, int arcAngle)
 180°                     0°           180°                     0°
                                                                                        x คาตําแหนงของแกน x ของจุดมุมซายบนของรูป
                                                                                        y คาตําแหนงของแกน y ของจุดมุมซายบนของรูป

             270°                                    270°
                                                                                        width คาความกวางของรูปสี่เหลี่ยม
                                                                                        height คาความสูงของรูปสี่เหลี่ยม
                                                                                        startAngle คามุมเริ่มตน
ตัวอยาง
                                                                                        arcAngle คามุมของสวนโคง
        g.drawArc( 35, 30, 120, 100);                                        ตัวอยาง
                                                                      8-29
                                                                                        g.fillArc( 35, 30, 120, 100);                       8-30




 ตัวอยาง                                                                           g.setColor( Color.BLUE );
                                                                                    g.drawRect( 5, 40, 90, 55 );
                                                                                    g.drawString("drawRect", 15, 55);
import java.awt.*;
import javax.swing.*;                                                               g.fillRect( 100, 40, 90, 55 );
                                                                                    g.setColor( Color.WHITE );
public class LineRectOval extends JApplet {                                         g.drawString( fillRect ,
                                                                                    g drawString("fillRect", 115, 55);

   public void init()                                                               super.setBackground(Color.GREEN);
   {                                                                                g.clearRect( 195, 40, 90, 55 );
        setSize( 600, 180 );      // จะมีผลกับ Appletviewer เทานัน
                                                                  ้                 g.drawString("clearRect", 215, 55);
     }
                                                                                    g.setColor( Color.CYAN );
   // display various lines, rectangles and ovals                                   g.fillRoundRect( 295, 40, 90, 55, 50, 50 );
   public void paint( Graphics g )                                                  g.setColor( Color.BLACK );
   {                                                                                g.drawString("fillRoundRect", 305, 55);
      super.paint( g ); // call superclass's paint method
                                                                                    g.setColor( Color.CYAN );
        g.setColor( Color.RED );                                                    g.drawRoundRect( 390, 40, 90, 55, 20, 20 );
        g.drawLine( 5, 30, 380, 30 );                                               g.setColor( Color.BLACK );
        g.drawString("drawString", 5, 25);                                          g.drawString("drawRoundRect", 400, 55);

                                                                      8-31                                                                  8-32




                                                                                                                                                         8
27/12/53



      g.setColor( Color.YELLOW );
      g.draw3DRect( 5, 100, 90, 55, true );
      g.setColor( Color.BLACK );
      g.drawString("draw3DRect", 15, 115);
      g.setColor( Color.YELLOW );
      g.fill3DRect( 100, 100, 90, 55, false );
      g.drawString("fill3DRect", 115, 115);

      g.setColor( Color.MAGENTA );
      g.drawOval( 295, 100, 90, 55 );
      g.setColor( Color.BLACK );
      g.drawString("drawOval", 305, 120);

      g.setColor( Color.MAGENTA );
      g.fillOval( 390, 100, 90, 55 );
      g.setColor( Color.WHITE );
      g.drawString("fillOval", 405, 120);

      g.setColor( Color.MAGENTA );
      g.drawOval( 495, 100, 55, 55 );
      g.setColor( Color.BLACK );
      g.drawString("drawOval", 505, 120);
   } // end method paint
} // end class LineRectOval                                         8-33                                                   8-34




                                                                                // start at 0 and sweep 110 degrees
                                                                                g.setColor( Color.YELLOW );
ตัวอยาง                                                                        g.drawRect( 100, 35, 80, 80 );
                                                                                g.setColor( Color.BLACK );
import java.awt.*;                                                              g.drawArc( 100, 35, 80, 80, 0, 110 );
import javax.swing.*;
                                                                                // start at 0 and sweep -270 degrees
public class DrawArcs extends JApplet {                                         g.setColor( Color.YELLOW );
   public void init()                                                           g.drawRect( 185, 35, 80, 80 );
   {                                                                            g.setColor( Color.BLACK );
        setSize( 350, 180 );    // จะมีผลกับ Appletviewer เทานัน
                                                                ้               g.drawArc( 185, 35, 80, 80, 0, -270 );
     }
                                                                                // start at 0 and sweep 360 degrees
   // draw rectangles and arcs                                                  g.setColor( Color.YELLOW );
   public void paint( Graphics g )                                              g.fillArc( 15, 120, 80, 40, 0, 360 );
   {
      super.paint(
      super paint( g ); // call superclass's paint method                       // sta t at 270 and s eep -90 degrees
                                                                                   start      0 a d sweep 90 deg ees
                                                                                g.setColor( Color.CYAN);
      // start at 0 and sweep 360 degrees                                       g.fillArc( 100, 120, 80, 40, 270, -90 );
      g.setColor( Color.YELLOW );
      g.drawRect( 15, 35, 80, 80 );                                              // start at 0 and sweep -270 degrees
      g.setColor( Color.BLACK );                                                 g.setColor( Color.ORANGE );
      g.drawArc( 15, 35, 80, 80, 0, 360 );                                       g.fillArc( 185, 120, 80, 40, 0, -270 );
                                                                              } // end method paint
                                                                    8-35   } // end class DrawArcs                         8-36




                                                                                                                                        9
27/12/53




                                                       ตัวอยางการใชงาน Applet กับ GUI
                                                       import   java.awt.*;
                                                       import   java.applet.Applet;
                                                       import   java.awt.event.*;
                                                       import   javax swing *;
                                                                javax.swing.*;

                                                       public class StringJApplet extends JApplet implements
                                                                     ActionListener
                                                       {
                                                           private Button okBtn;
                                                           private JLabel strLabel, xLabel, yLabel;
                                                           private JTextField strField, xField, yField;
                                                           String mesg;
                                                           St i
                                                           int x, y;

                                                            public void init() {
                                                                Container c = getContentPane();
                                                                c.setLayout( new FlowLayout());
                                                8-37                                                           8-38




    strLabel = new JLabel("Enter String : ");              public void actionPerformed(ActionEvent event) {
    c.add(strLabel);                                            mesg = strField.getText();
    strField = new JTextField(20);                              x = Integer.parseInt(xField.getText() );
    c.add(strField);                                            y = Integer.parseInt(yField.getText() );
    xLabel = new JLabel("Coordinate X : ");
    c.add(xLabel);                                               repaint();
    xField = new JTextField(5);
                           ( );                             }
    c.add(xField);
    yLabel = new JLabel("Coordinate Y : ");                 public void paint(Graphics g) {
    c.add(yLabel);                                              super.paint(g);
    yField = new JTextField(5);                                 g.drawRect(5,2, 330,80);
    c.add(yField);                                              g.setColor(Color.BLACK);
                                                                g.drawString(mesg, x, y+90);
    okBtn = new Button(" OK ");                             }
    okBtn.addActionListener(this);
    okBtn addActionListener(this)
    c.add(okBtn);                                      }

    setSize(340,300);
    setForeground(Color.BLUE);
}

                                                8-39                                                           8-40




                                                                                                                           10
27/12/53



                                                                         shrink = new Button("Shrink");
ตัวอยางการใชงาน Applet กับ GUI                                        shrink.addActionListener(this);
                                                                        c.add(shrink);
import   java.awt.*;                                                    left = new Button("Left");
import   java.applet.Applet;                                            left.addActionListener(this);
import   java.awt.event.*;                                              c.add(left);
import   javax swing *;
         javax.swing.*;                                                 right = new Button("Right");
                                                                           g               (   g   );
                                                                        right.addActionListener(this);
public class PlayBalloon extends JApplet implements                     c.add(right);
ActionListener {                                                        up = new Button("Up");
    private Button grow, shrink, left, right, up, down;                 up.addActionListener(this);
    private Balloon myBalloon;                                          c.add(up);
                                                                        down = new Button("Down");
    public void init() {                                                down.addActionListener(this);
        Container c = getContentPane();
        C t i            tC t tP    ()                                  c.add(down);
                                                                        c add(do n)
        c.setLayout( new FlowLayout());                                 myBalloon = new Balloon();
                                                                        setSize(400,400);
          grow = new Button("Grow");                                    setForeground(Color.BLUE);
          grow.addActionListener(this);                             }
          c.add(grow);
                                                          8-41                                                  8-42




    public void actionPerformed(ctionEvent event) {              class Balloon {
       if (event.getSource() == grow)                                private int diameter = 10;
             myBalloon.grow();                                       private int x = 20, y = 50;
        else if (event.getSource() == shrink)
             myBalloon.shrink();                                    public void display(Graphics g) {
        else if (event.getSource() == left)                             g.drawOval(x, y, diameter, diameter);
             myBalloon.left();
              y            ();                                      }
         else if (event.getSource() == right)
                                                                    public void left() {
             myBalloon.right();
                                                                        x = x - 10;
         else if (event.getSource() == up)
                                                                    }
             myBalloon.up();
         else if (event.getSource() == down)                        public void right() {
             myBalloon.down();                                          x = x + 10;
         repaint();                                                 }
    }
                                                                    public void up() {
    public void paint(Graphics g) {                                     y = y - 10;
        super.paint(g);                                             }
        myBalloon.display(g);                                       public void down() {
    }                                                                   y = y + 10;
                                                          8-43
                                                                    }                                           8-44
}




                                                                                                                            11
27/12/53



    public void grow() {
        diameter = diameter + 5;
    }

    public void shrink() {
        diameter = diameter - 5;
    }
}




                                   8-45




                                               12

More Related Content

Featured

How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
ThinkNow
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
Kurio // The Social Media Age(ncy)
 

Featured (20)

2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot2024 State of Marketing Report – by Hubspot
2024 State of Marketing Report – by Hubspot
 
Everything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPTEverything You Need To Know About ChatGPT
Everything You Need To Know About ChatGPT
 
Product Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage EngineeringsProduct Design Trends in 2024 | Teenage Engineerings
Product Design Trends in 2024 | Teenage Engineerings
 
How Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental HealthHow Race, Age and Gender Shape Attitudes Towards Mental Health
How Race, Age and Gender Shape Attitudes Towards Mental Health
 
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdfAI Trends in Creative Operations 2024 by Artwork Flow.pdf
AI Trends in Creative Operations 2024 by Artwork Flow.pdf
 
Skeleton Culture Code
Skeleton Culture CodeSkeleton Culture Code
Skeleton Culture Code
 
PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024PEPSICO Presentation to CAGNY Conference Feb 2024
PEPSICO Presentation to CAGNY Conference Feb 2024
 
Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)Content Methodology: A Best Practices Report (Webinar)
Content Methodology: A Best Practices Report (Webinar)
 
How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024How to Prepare For a Successful Job Search for 2024
How to Prepare For a Successful Job Search for 2024
 
Social Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie InsightsSocial Media Marketing Trends 2024 // The Global Indie Insights
Social Media Marketing Trends 2024 // The Global Indie Insights
 
Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024Trends In Paid Search: Navigating The Digital Landscape In 2024
Trends In Paid Search: Navigating The Digital Landscape In 2024
 
5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary5 Public speaking tips from TED - Visualized summary
5 Public speaking tips from TED - Visualized summary
 
ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd ChatGPT and the Future of Work - Clark Boyd
ChatGPT and the Future of Work - Clark Boyd
 
Getting into the tech field. what next
Getting into the tech field. what next Getting into the tech field. what next
Getting into the tech field. what next
 
Google's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search IntentGoogle's Just Not That Into You: Understanding Core Updates & Search Intent
Google's Just Not That Into You: Understanding Core Updates & Search Intent
 
How to have difficult conversations
How to have difficult conversations How to have difficult conversations
How to have difficult conversations
 
Introduction to Data Science
Introduction to Data ScienceIntroduction to Data Science
Introduction to Data Science
 
Time Management & Productivity - Best Practices
Time Management & Productivity -  Best PracticesTime Management & Productivity -  Best Practices
Time Management & Productivity - Best Practices
 
The six step guide to practical project management
The six step guide to practical project managementThe six step guide to practical project management
The six step guide to practical project management
 
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
Beginners Guide to TikTok for Search - Rachel Pearson - We are Tilt __ Bright...
 

662327 08-2-53 [โหมดความเข้ากันได้]

  • 1. 27/12/53 Introduction  Applet  คือโปรแกรมขนาดเล็กๆ ในภาษาจาวา ทีถูกกําหนดใหใชได ่ บนเว็บเพจ โดยแสดงผลผานเว็บเบราเซอร Chapter 8:  เปนโปรแกรมทีไมสามารถรันไดดวยตัวเอง แตจะตองถูก ่ การสรางและใชงาน Applet เรียกใชโดยเบราเซอร  สามารถสราง GUI และกราฟกที่เปนรูปภาพได และสามาถ ตอบสนองเหตุการณตางๆที่เกิดขึ้นกับหนาจอ Applet นันได ้ 662327 Advanced Computer Programming เชนการคลิกเมาส หรือการกดปุมคียบอรด   โดยใชคลาส JApplet ในชุดของ javax.swing Somchai Cheingpongpan Department of Information Technology 8-2 หลักการของการสราง Applet หลักการของการสราง Applet  Java Applet นันเปนออบเจ็กตที่สรางมาจากคลาส ้  หลังจากสรางคลาสยอยจากคลาส JApplet แลว ซึ่งก็จะไดเมธอด java.applet.Applet หรือเรียกไดวาเปน Subclass ของ Applet ตางๆที่ถูกกําหนดไวในคลาส JApplet ตามกฏการถายทอด โดยมี  โดย Applet มีหนาที่เพียงแคใชในการสรางคลาสยอย หรือ บางเมธอดทีี่สามารถใสคําสั่งเพิ่มลงไป และบางเมธอดไมตองทํา ใ ั ไป ไ ออบเจ็กตขนมาไมไดทําอะไรมากกวานัน ดังนันจึงเปนเพียงแค ึ้ ้ ้ อะไรกับมันเลย พื้นที่วางๆ บนหนาจอที่ไมมีการตอบสนองใดๆ  ใน JApplet นันมีเมธอดทีทํางานอยูดังนี้ ้ ่ การสราง Applet ที่มีประโยชนนน โปรแกรมเมอรจะตองเพิ่มเติม ั้ public class MyJApplet extends JApplet {  public void init() { คลาสยอยซึ่งขยาย(extends)มาจากคลาส Applet ซึ่งมีรูปแบบ } ดงน ดังนี้ public void start() { } public class myJApplet extends JApllet { public void paint(Graphics g) { } // สวนของโปรแกรม public void stop() { } } public void destroy() { } 8-3 } 8-4 1
  • 2. 27/12/53 หลักการของการสราง Applet หลักการของการสราง Applet  เมธอด init()  เมธอด stop() ใชสําหรับกําหนดคาเริ่มตนหรือการ initialize ใหกับคลาส ซึ่งจะ การหยุดของ Applet หมายถึง การที่ผูใชออกจากหนาจอปจจุบน ั เริ่มเปนเมธอดแรกเมืื่อมีีการโหลดเขามาทํางาน ซึึ่งทํางานไดครั้ง ป โ ไ ั ที่กําลังทํางาน Applet อยู หรือการที่ Applet หยุดตัวเองโดยเรียก เดียว เมธอด stop() โดยตรง  เมธอด start()  เมธอด destroy() เปนเมธอดที่ทํางานตอจากเมธอด init โดยไมจําเปนตองเขียน เปนเมธอดที่บอกให Applet ทําการคืนหนวยความจําตางๆใหกบ ั เมธอด start() ก็ได หากไมไดใหทําอะไร โดยสามารถเกิดขึนกี่ ้ ครั้งก็ไดในชวงชีวิตของ JApplet ระบบกอนที่จะทําการปดเบราเซอร  เมธอด paint() เปนเมธอดที่ใชสําหรับแสดงขอมูลบนหนาจอ ไมมาจะเปน ขอความ รูปภาพ รูปทรงตางๆ โดยสามารถเกิดขึนกี่ครั้งก็ได ้ ในชวงชีวิตของ JApplet ซึ่งมีพารามิเตอรเปน คลาส Graphics 8-5 8-6 หลักการของการสราง Applet การใชงาน Applet กับ HTML import java.awt.*;  Applet Tag import javax.swing.*; public class MyJApplet extends JApplet { เนื่องจากการใชงาน Applet นันตองทํางานบนเว็บเพจที่เขียน ้ public void init() { ดวยภาษา H ML โ ใ แท็ก <APPLET> ใ ในการเพิ่ม Applet HTML โดยใช ็ PPLE ใช ิ l ใหกับเว็บเพจหนึ่งๆ ซึ่งคูกับ </APPLET> มีแอตทริบิวตที่ตองใช System.out.println("Init"); } public void start() { งานดังนี้  CODE ใชสาหรับกําหนดชื่อของคลาส Applet ที่ตองการทํางาน ํ System.out.println("Start"); } public void paint(Graphics g) {  CODEBASE ใชสาหรับกําหนดไดเรกทอรีของไฟลคลาสที่ตองการ ํ System.out.println("Paint"); ใชงาน width ใ สําหรับกําหนดขนาดความกวางของพืื้นทีี่ Applet ทีี่แสดง ใช }  public void stop() { System.out.println("Stop"); ในหนาเวบเพจ }  height ใชสาหรับกําหนดขนาดความสูงของพื้นที่ Applet ที่แสดง ํ public void destroy() { ในหนาเวบเพจ System.out.println("Destroy"); } } 8-7 8-8 2
  • 3. 27/12/53 การใชงาน Applet กับ HTML การใชงาน Applet กับ HTML  ตัวอยาง  PARAM Tag <html> โดยปกติแลว Applet สามารถสามารถมีพารามิเตอรที่สงมาจาก <body> เพจ HTML ได โดยใชแท็ก <PARAM> ซึ่งตองใชอยูในแท็ก <APPLET> และ </APPLET> เทานัน มีแอตทริบิวตที่ตองใชงาน <applet code=“MyJApplet.class” height=“200” width=“300”> </applet> ้  </body> ดังนี้ </html>  NAME ใชสําหรับกําหนดชือของพารามิเตอร ่ <html>  VALUE ใชสําหรับกําหนดคาของแตละพารามิเตอร <body> y <applet code=“MyJApplet.class” codebase=“classes”  การเรียกใชพารามิเตอร height=“200” width=“300”> </applet> เมื่อกําหนดคคาพารามิเตอรแลว เมื่อตองการใชคาก็เรียกผานเมธ </body> อด getParameter() โดยกําหนดชือใหตรงกับที่กําหนดในแท็ก ่ </html> <PARAM> 8-9 8-10 การใชงาน Applet กับ HTML การใชงาน Applet กับ HTML  ตัวอยาง  ตัวอยาง <html> import java.awt.*; <body> import javax.swing.*; <applet code=“ParameterJApplet.class” height=“200” width=“400”> public class ParameterJApplet extends JApplet { <param name=“mesg” value=“Hello, I’m your first parameter”> public void paint(Graphics g) { <param name=“font” value=“Times New Roman”> String message = getParameter(“mesg”); <param name=“size” value=“30”> String fontname = getParameter(“font”); </applet> int fontSize = Integer.parseInt( </body> getParameter(“size”)); </html> Font textFont = new Font(fontName, Font.BOLD, fontSzie); g.setFont(textFont); g.drawDtring(message, 10, 30); } 8-11 } 8-12 3
  • 4. 27/12/53 การใชงาน Applet กับ HTML การใชงานกราฟก (Graphics)  ผลลัพธ  จาวาไดเตรียมคลาส Graphics ไวในใชงานทางดานกราฟก ซึ่งอยุในชุดของ java.awt โดยมีเมธอดหลักที่ใชในการแสดง รูปทรงและการระบายสีดังนีี้ ี  drawString  drawLine  drawRect,fillRect,clearRect, drawRoundRct,fillRoundRect  drawOval, fillOval  drawArc, fillArc 8-13 8-14 คําสั่ง drawString คําสั่ง drawLine  เปนเมธอดที่ใชแสดงขอความกราฟกบนหนาจอ มีรปแบบดังนี้ ู  เปนเมธอดที่ใชวาดเสนตรงบนหนาจอ มีรูปแบบคําสั่งดังนี้ public void drawString (String msg, int x, int y) public void drawLine (int x1, int y1, int x2, int y2) msg ขอความทีตองการแสดง ่ x1 คาตําแหนงของแกน x ของจุดตน x คาตําแหนงของแกน x เปนพิกเซลบนหนาจอ y1 คาตําแหนงของแกน y ของจุดตน y คาตําแหนงของแกน y เปนพิกเซลบนหนาจอ แ นง งแ น เ เ ล น น x x2 คาตําแหนงของแกน x ของจดปลาย แ นง งแ น ง ุ ล ตัวอยาง y2 คาตําแหนงของแกน y ของจุดปลาย g.drawString(“Hello,World”, 30, 20); ตัวอยาง g.drawLine(25, 30, 200, 120); 8-15 8-16 4
  • 5. 27/12/53 คําสั่ง drawRect คําสั่ง fillRect  เปนเมธอดที่ใชวาดรูปสี่เหลี่ยมที่ไมมีการระบายสี มีรูปแบบคําสั่ง  เปนเมธอดที่ใชวาดรูปสี่เหลี่ยมที่มีการระบายสีดวยสี foreground  ดังนี้ มีรูปแบบคําสั่งดังนี้ public void drawRect (int x, int y, int width, int height) public void fillRect (int x, int y, int width, int height) x คาตําแหนงของแกน x ของจุดมุมซายบนของรูป x คาตําแหนงของแกน x ของจุดมุมซายบนของรูป y คาตําแหนงของแกน y ของจุดมุมซายบนของรูป y คาตําแหนงของแกน y ของจุดมุมซายบนของรูป width คาความกวางของรปสี่เหลี่ยม คาความกวางของรูปสเหลยม width คาความกวางของรปสี่เหลี่ยม คาความกวางของรูปสเหลยม height คาความสูงของรูปสี่เหลี่ยม height คาความสูงของรูปสี่เหลี่ยม ตัวอยาง ตัวอยาง g.drawRect( 35, 30, 120, 100); g.fillRect( 35, 30, 120, 100); 8-17 8-18 คําสั่ง clearRect คําสั่ง drawRoundRect  เปนเมธอดที่ใชวาดรูปสี่เหลี่ยมที่มีการระบายสีดวยสี background   เปนเมธอดที่ใชวาดรูปสี่เหลี่ยมที่มีมุมเปนมน ทีไมมการระบายสี มี ่ ี มีรูปแบบคําสั่งดังนี้ รูปแบบคําสั่งดังนี้ public void clearRect(int x, int y, int width, int height) public void drawRoundRect(int x, int y, int width, int height,int arcWidth, int arcHeight) x คาตําแหนงของแกน x ของจุดมุมซายบนของรูป x คาตําแหนงของแกน x ของจุดมุมซายบนของรูป y คาตําแหนงของแกน y ของจุดมุมซายบนของรูป y คาตําแหนงของแกน y ของจุดมุมซายบนของรูป width คาความกวางของรปสี่เหลี่ยม คาความกวางของรูปสเหลยม width คาความกวางของรูปสี่เหลี่ยม height คาความสูงของรูปสี่เหลี่ยม ตัวอยาง height คาความสูงของรูปสี่เหลี่ยม g.clearRect( 35, 30, 120, 100); arcWidth คาความกวางของสวนโคงมนรูปสีเหลี่ยม ่ arcHeight คาความสูงของสวนโคงมนรูปสี่เหลี่ยม 8-19 8-20 5
  • 6. 27/12/53 คําสั่ง drawRoundRect คําสั่ง fillRoundRect ( x, y)  เปนเมธอดที่ใชวาดรูปสี่เหลี่ยมที่มีมุมเปนมน ทีมีการระบายสีดวยสี ่ arc height foregroundมีรูปแบบคําสั่งดังนี้ a rc width height public void fillRoundRect(int x, int y, int width, int height,int arcWidth, int arcHeight) x คาตําแหนงของแกน x ของจุดมุมซายบนของรูป y คาตําแหนงของแกน y ของจุดมุมซายบนของรูป width คาความกวางของรูปสี่เหลี่ยม w idth height คาความสูงของรูปสี่เหลี่ยม arcWidth คาความกวางของสวนโคงมนรูปสี่เหลี่ยม ตัวอยาง arcHeight คาความสูงของสวนโคงมนรูปสี่เหลี่ยม g.drawRoundRect( 35, 30, 120, 100,20,20); ตัวอยาง g.fillRoundRect( 35, 30, 120, 100,20,20); 8-21 8-22 คําสั่ง draw3DRect คําสั่ง fill3DRect  เปนเมธอดที่ใชวาดรูปสี่เหลี่ยมที่มีมุมเปนมน ทีไมมการระบายสี ่ ี  เปนเมธอดที่ใชวาดรูปสี่เหลี่ยมที่มีมุมเปนมน ทีมีการระบายสีดวยสี ่ ดวยสี foreground มีรูปแบบคําสั่งดังนี้ foregroundมีรูปแบบคําสั่งดังนี้ public void draw3dRect(int x, int y, int width, public void fill3dRect(int x, int y, int width, int height, booleab b) int height, booleab b) x คาตําแหนงของแกน x ของจุดมุมซายบนของรูป x คาตําแหนงของแกน x ของจุดมุมซายบนของรูป y คาตําแหนงของแกน y ของจุดมุมซายบนของรูป y คาตําแหนงของแกน y ของจุดมุมซายบนของรูป width คาความกวางของรปสี่เหลี่ยม idth คาความกวางของรูปสเหลยม width คาความกวางของรปสี่เหลี่ยม idth คาความกวางของรูปสเหลยม height คาความสูงของรูปสี่เหลี่ยม height คาความสูงของรูปสี่เหลี่ยม b คาที่แสดงลักษณะ 3 มิติ b คาที่แสดงลักษณะ 3 มิติ ตัวอยาง ตัวอยาง g.draw3dRect( 35, 30, 120, 100,true); 8-23 g.fill3dRect( 35, 30, 120, 100,true); 8-24 6
  • 7. 27/12/53 คําสั่ง drawOval คําสั่ง drawOval  เปนเมธอดที่ใชวาดรูปวงกลม หรือวงรี ที่ไมมการระบายสี มี ี ( x , y) รูปแบบคําสั่งดังนี้ public void drawOval(int x, int y, int width, int height) height x คาตําแหนงของแกน x ของจุดมุมซายบนของรูป y คาตําแหนงของแกน y ของจุดมุมซายบนของรูป width คาความกวางของรูปสี่เหลี่ยม w id th height คาความสูงของรูปสเหลยม h i ht คาความสงของรปสี่เหลี่ยม ตัวอยาง g.drawOval( 35, 30, 120, 100); 8-25 8-26 คําสั่ง fillOval คําสั่ง drawArc  เปนเมธอดที่ใชวาดรูปวงกลม หรือวงรี ที่มการระบายสีดวยสี ี  เปนเมธอดที่ใชวาดสวนโคงของวงกลม ที่ไมมีการระบายสี มี foreground มีรูปแบบคําสั่งดังนี้ รูปแบบคําสั่งดังนี้ public void fillOval(int x, int y, int width, int height) public void drawArc(int x, int y, int width, int height, x คาตําแหนงของแกน x ของจุดมุมซายบนของรูป int startAngle, int arcAngle) y คาตําแหนงของแกน y ของจุดมุมซายบนของรูป x คาตําแหนงของแกน x ของจุดมุมซายบนของรูป width คาความกวางของรูปสี่เหลี่ยม y คาตําแหนงของแกน y ของจุดมุมซายบนของรูป height คาความสูงของรูปสเหลยม h i ht คาความสงของรปสี่เหลี่ยม width คาความกวางของรปสี่เหลี่ยม idth คาความกวางของรูปสเหลยม height คาความสูงของรูปสี่เหลี่ยม ตัวอยาง startAngle คามุมเริ่มตน g.fillOval( 35, 30, 120, 100); arcAngle คามุมของสวนโคง 8-27 8-28 7
  • 8. 27/12/53 คําสั่ง drawArc คําสั่ง fillArc Positive angles Negative angles  เปนเมธอดที่ใชวาดสวนโคงของวงกลม ที่มีการระบายสีดวยสี 90° 90° foreground มีรูปแบบคําสั่งดังนี้ public void fillArc(int x, int y, int width, int height, int startAngle, int arcAngle) 180° 0° 180° 0° x คาตําแหนงของแกน x ของจุดมุมซายบนของรูป y คาตําแหนงของแกน y ของจุดมุมซายบนของรูป 270° 270° width คาความกวางของรูปสี่เหลี่ยม height คาความสูงของรูปสี่เหลี่ยม startAngle คามุมเริ่มตน ตัวอยาง arcAngle คามุมของสวนโคง g.drawArc( 35, 30, 120, 100); ตัวอยาง 8-29 g.fillArc( 35, 30, 120, 100); 8-30 ตัวอยาง g.setColor( Color.BLUE ); g.drawRect( 5, 40, 90, 55 ); g.drawString("drawRect", 15, 55); import java.awt.*; import javax.swing.*; g.fillRect( 100, 40, 90, 55 ); g.setColor( Color.WHITE ); public class LineRectOval extends JApplet { g.drawString( fillRect , g drawString("fillRect", 115, 55); public void init() super.setBackground(Color.GREEN); { g.clearRect( 195, 40, 90, 55 ); setSize( 600, 180 ); // จะมีผลกับ Appletviewer เทานัน ้ g.drawString("clearRect", 215, 55); } g.setColor( Color.CYAN ); // display various lines, rectangles and ovals g.fillRoundRect( 295, 40, 90, 55, 50, 50 ); public void paint( Graphics g ) g.setColor( Color.BLACK ); { g.drawString("fillRoundRect", 305, 55); super.paint( g ); // call superclass's paint method g.setColor( Color.CYAN ); g.setColor( Color.RED ); g.drawRoundRect( 390, 40, 90, 55, 20, 20 ); g.drawLine( 5, 30, 380, 30 ); g.setColor( Color.BLACK ); g.drawString("drawString", 5, 25); g.drawString("drawRoundRect", 400, 55); 8-31 8-32 8
  • 9. 27/12/53 g.setColor( Color.YELLOW ); g.draw3DRect( 5, 100, 90, 55, true ); g.setColor( Color.BLACK ); g.drawString("draw3DRect", 15, 115); g.setColor( Color.YELLOW ); g.fill3DRect( 100, 100, 90, 55, false ); g.drawString("fill3DRect", 115, 115); g.setColor( Color.MAGENTA ); g.drawOval( 295, 100, 90, 55 ); g.setColor( Color.BLACK ); g.drawString("drawOval", 305, 120); g.setColor( Color.MAGENTA ); g.fillOval( 390, 100, 90, 55 ); g.setColor( Color.WHITE ); g.drawString("fillOval", 405, 120); g.setColor( Color.MAGENTA ); g.drawOval( 495, 100, 55, 55 ); g.setColor( Color.BLACK ); g.drawString("drawOval", 505, 120); } // end method paint } // end class LineRectOval 8-33 8-34 // start at 0 and sweep 110 degrees g.setColor( Color.YELLOW ); ตัวอยาง g.drawRect( 100, 35, 80, 80 ); g.setColor( Color.BLACK ); import java.awt.*; g.drawArc( 100, 35, 80, 80, 0, 110 ); import javax.swing.*; // start at 0 and sweep -270 degrees public class DrawArcs extends JApplet { g.setColor( Color.YELLOW ); public void init() g.drawRect( 185, 35, 80, 80 ); { g.setColor( Color.BLACK ); setSize( 350, 180 ); // จะมีผลกับ Appletviewer เทานัน ้ g.drawArc( 185, 35, 80, 80, 0, -270 ); } // start at 0 and sweep 360 degrees // draw rectangles and arcs g.setColor( Color.YELLOW ); public void paint( Graphics g ) g.fillArc( 15, 120, 80, 40, 0, 360 ); { super.paint( super paint( g ); // call superclass's paint method // sta t at 270 and s eep -90 degrees start 0 a d sweep 90 deg ees g.setColor( Color.CYAN); // start at 0 and sweep 360 degrees g.fillArc( 100, 120, 80, 40, 270, -90 ); g.setColor( Color.YELLOW ); g.drawRect( 15, 35, 80, 80 ); // start at 0 and sweep -270 degrees g.setColor( Color.BLACK ); g.setColor( Color.ORANGE ); g.drawArc( 15, 35, 80, 80, 0, 360 ); g.fillArc( 185, 120, 80, 40, 0, -270 ); } // end method paint 8-35 } // end class DrawArcs 8-36 9
  • 10. 27/12/53 ตัวอยางการใชงาน Applet กับ GUI import java.awt.*; import java.applet.Applet; import java.awt.event.*; import javax swing *; javax.swing.*; public class StringJApplet extends JApplet implements ActionListener { private Button okBtn; private JLabel strLabel, xLabel, yLabel; private JTextField strField, xField, yField; String mesg; St i int x, y; public void init() { Container c = getContentPane(); c.setLayout( new FlowLayout()); 8-37 8-38 strLabel = new JLabel("Enter String : "); public void actionPerformed(ActionEvent event) { c.add(strLabel); mesg = strField.getText(); strField = new JTextField(20); x = Integer.parseInt(xField.getText() ); c.add(strField); y = Integer.parseInt(yField.getText() ); xLabel = new JLabel("Coordinate X : "); c.add(xLabel); repaint(); xField = new JTextField(5); ( ); } c.add(xField); yLabel = new JLabel("Coordinate Y : "); public void paint(Graphics g) { c.add(yLabel); super.paint(g); yField = new JTextField(5); g.drawRect(5,2, 330,80); c.add(yField); g.setColor(Color.BLACK); g.drawString(mesg, x, y+90); okBtn = new Button(" OK "); } okBtn.addActionListener(this); okBtn addActionListener(this) c.add(okBtn); } setSize(340,300); setForeground(Color.BLUE); } 8-39 8-40 10
  • 11. 27/12/53 shrink = new Button("Shrink"); ตัวอยางการใชงาน Applet กับ GUI shrink.addActionListener(this); c.add(shrink); import java.awt.*; left = new Button("Left"); import java.applet.Applet; left.addActionListener(this); import java.awt.event.*; c.add(left); import javax swing *; javax.swing.*; right = new Button("Right"); g ( g ); right.addActionListener(this); public class PlayBalloon extends JApplet implements c.add(right); ActionListener { up = new Button("Up"); private Button grow, shrink, left, right, up, down; up.addActionListener(this); private Balloon myBalloon; c.add(up); down = new Button("Down"); public void init() { down.addActionListener(this); Container c = getContentPane(); C t i tC t tP () c.add(down); c add(do n) c.setLayout( new FlowLayout()); myBalloon = new Balloon(); setSize(400,400); grow = new Button("Grow"); setForeground(Color.BLUE); grow.addActionListener(this); } c.add(grow); 8-41 8-42 public void actionPerformed(ctionEvent event) { class Balloon { if (event.getSource() == grow) private int diameter = 10; myBalloon.grow(); private int x = 20, y = 50; else if (event.getSource() == shrink) myBalloon.shrink(); public void display(Graphics g) { else if (event.getSource() == left) g.drawOval(x, y, diameter, diameter); myBalloon.left(); y (); } else if (event.getSource() == right) public void left() { myBalloon.right(); x = x - 10; else if (event.getSource() == up) } myBalloon.up(); else if (event.getSource() == down) public void right() { myBalloon.down(); x = x + 10; repaint(); } } public void up() { public void paint(Graphics g) { y = y - 10; super.paint(g); } myBalloon.display(g); public void down() { } y = y + 10; 8-43 } 8-44 } 11
  • 12. 27/12/53 public void grow() { diameter = diameter + 5; } public void shrink() { diameter = diameter - 5; } } 8-45 12