SlideShare a Scribd company logo
การใชงาน Mouse และ Keyboard                                                                                                                                                             ssc



การใชงาน MouseListener และ MouseEvent ในการตรวจสอบการคลิกเมาสในตำแหนงของวัตถุที่ตองการ
MouseEvent ประกอบดวย 5 Method ดังนี้
1) mousePressed                  .........................................................................................................................................................
2) mouseReleased .........................................................................................................................................................
3) mouseClicked                  .........................................................................................................................................................
4) mouseEntered                  .........................................................................................................................................................
5) mouseExited                   .........................................................................................................................................................
ตัวอยางหนาจอการทำงาน (http://202.44.47.108/~ssc/mouse-keyboard/testMouse.html)




testMouse.html
  <html>
  !     <body>
          <h1>Test Mouse</h1>
          <h3>Create by: 5366261111 Supaporn Simcharoen IT1-RC</h3>
  !     !     <applet code="testMouse.class" height="240" width="320">
  !     !     </applet>
  !     </body>
  </html>


testMouse.java
ขั้นตอนที่ 1 สราง Class พรอมกับ import package ที่ตองใช และสราง Method ที่นาจะมี
  import java.awt.*;
  import java.awt.event.*;
  import javax.swing.*;

  public class testMouse extends JApplet implements MouseListener {
  !     public void init () {
  !     }
  !     public void paint(Graphics g) {
  !     }
  }


ทำไมตอง implements MouseListener .................................................................................................................................
ขั้นตอนที่ 2 ใหนักศึกษาทดลอง Compile
ผลการ Compile ...................................................................................................................................................................
ขั้นตอนที่ 3 ใหนักศึกษาเพิ่ม Method ตางๆ ตอไปนี้
  !         public       void    mousePressed(MouseEvent event) { }
  !         public       void    mouseReleased(MouseEvent event) { }
  !         public       void    mouseClicked(MouseEvent event) { }
  !         public       void    mouseEntered( MouseEvent event ) { }
  !         public       void    mouseExited(MouseEvent event) { }


ขั้นตอนที่ 4 ใหนักศึกษาทดลอง Compile อีกครั้ง
ผลการ Compile ...................................................................................................................................................................
                                                                                                                                                                        Page 1 of 7
ขั้นตอนที่ 5 ใหนักศึกษาเพิ่มการทำงานในสวนตางๆ ดังนี้
ประกาศคาตางๆ
 !        int x, y, size;
 !        Color bgcolor, fgcolor;
 !        boolean isDraw = true;


สรางคาเริ่มตน
 !        public void init () {
 !        !        x = 140;
 !        !        y = 100;
 !        !        size = 50;
 !
 !
          !
          !
                   fgcolor = Color.BLUE;
                   addMouseListener(this);
                                                                                   Note :: .....................................................................
 !        }
 !        public void paint(Graphics g) {
 !        !        super.paint(g);
 !        !        if (isDraw == true) {
 !        !        !       g.setColor(fgcolor);
 !        !        !       g.fillRect( x, y, size , size);
 !        !        !       g.setColor( Color.BLACK );
 !        !        !       g.drawRect( x, y, size, size);
 !        !        }
 !        }
                                                                                         Note :: .....................................................................
สรางในสวนของการรองรับการทำงานตางๆ ของ Mouse
 !        public void mousePressed(MouseEvent event) {
 !        !        Graphics g = getGraphics();
 !        !        g.drawString("("+event.getX()+","+event.getY()+")",event.getX(), event.getY());
 !        }

 !        public void mouseReleased(MouseEvent event) {
 !
 !
          !
          }
                   repaint();
                                                                                                   Note :: .....................................................................
 !        public void mouseClicked(MouseEvent event) {
 !        !        boolean flag = isInside(x,y,size,event.getX(),event.getY());
 !        !        if (isDraw == true) {
 !        !        !       if (flag == true) isDraw = !isDraw;
 !        !        }
 !        !        else {
 !        !        !       x = event.getX();
 !        !        !       y = event.getY();
 !        !        !       isDraw = !isDraw;
 !        !        }
 !        }

 !        public void mouseEntered( MouseEvent event ) {
 !        !        repaint();
 !        }

 !        public void mouseExited(MouseEvent event) {
 !        !        repaint();
 !        }

 !        boolean isInside(int x1,int y1,int size,int posx,int posy) {
 !        !        int x2 = x1 + size;
 !        !        int y2 = y1 + size;

 !        !               if (posx >= x1 && posx <= x2)
 !        !               !      if (posy >= y1 && posy <= y2)
                                         return true;

 !        !               return false;
 !        }



ขั้นตอนที่ 6 ใหนักศึกษาทดลอง Compile อีกครั้ง
ผลของ    mousePressed .......................................................................................................................................................
ผลของ    mouseReleased .....................................................................................................................................................
ผลของ    mouseClicked ........................................................................................................................................................
ผลของ    mouseEntered .......................................................................................................................................................
ผลของ    mouseExited ..........................................................................................................................................................

                                                                                                                                                               Page 2 of 7
การใชงาน KeyListener และ KeyEvent ในการตรวจสอบการกดปุมที่ Keyboard
KeyEvent ประกอบดวย 3 Method ดังนี้
1) keyPressed                ..............................................................................................................................................................
2) keyReleased ..............................................................................................................................................................
3) keyTyped                  ..............................................................................................................................................................
ตัวอยางหนาจอการทำงาน (http://202.44.47.108/~ssc/mouse-keyboard/testKeyboard.html)




testKeyboard.html
  <html>
  !     <body>
          <h1>Test Keyboard</h1>
          <h3>Create by: 5366261111 Supaporn Simcharoen IT1-RC</h3>
  !     !     <applet code="testKeyboard.class" height="240" width="320">
  !     !     </applet>
  !     </body>
  </html>


testKeyboard.java
ขั้นตอนที่ 1 สราง Class พรอมกับ import package ที่ตองใช และสราง Method ที่นาจะมี
  import java.awt.*;
  import java.awt.event.*;
  import javax.swing.*;

  public class testKeyboard extends JApplet implements KeyListener {
  !     public void init () {
  !     }
  !     public void paint(Graphics g) {
  !     }
  }


ทำไมตอง implements KeyListener ......................................................................................................................................
ขั้นตอนที่ 2 ใหนักศึกษาทดลอง Compile
ผลการ Compile ...................................................................................................................................................................
ขั้นตอนที่ 3 ใหนักศึกษาเพิ่ม Method ตางๆ ตอไปนี้
  !         public void keyPressed(KeyEvent event) { }
  !         public void keyReleased(KeyEvent event) { }
  !         public void keyTyped(KeyEvent event) { }


ขั้นตอนที่ 4 ใหนักศึกษาทดลอง Compile อีกครั้ง
ผลการ Compile ...................................................................................................................................................................
ขั้นตอนที่ 5 ใหนักศึกษาเพิ่มการทำงานในสวนตางๆ ดังนี้
ประกาศคาตางๆ
  !         private int x, y, size;
  !         Color fgcolor;
  !         private char typeShape = 'R';


                                                                                                                                                                         Page 3 of 7
สรางคาเริ่มตน
  !         public void init () {
  !         !        x = 140;
  !         !        y = 100;
  !         !        size = 50;
  !         !        fgcolor = Color.BLUE;
  !         !        addKeyListener( this );
  !         }
  !         public void paint(Graphics g) {
  !         !        super.paint(g);
  !         !        if (typeShape == 'R') {
  !         !        !       g.setColor(fgcolor);
  !         !        !       g.fillRect( x, y, size , size);
  !         !        !       g.setColor( Color.BLACK );
  !         !        !       g.drawRect( x, y, size, size);
  !         !        }
  !         !        else {
  !         !        !       g.setColor(fgcolor);
  !         !        !       g.fillOval( x, y, size , size);
  !         !        !       g.setColor( Color.BLACK );
  !         !        !       g.drawOval( x, y, size, size);
  !         !        }
  !         }


สรางในสวนของการรองรับการทำงานตางๆ ของ Keyboard
  !         public void keyPressed(KeyEvent event) {
  !         !        if ( event.getKeyChar() == 'c') typeShape = 'C';
  !         !        if ( event.getKeyChar() == 'r') typeShape = 'R';
  !         }

  !         public void keyReleased(KeyEvent event) {
  !         !        repaint();                                                                Note :: .....................................................................
  !         }

  !         public void keyTyped(KeyEvent event) {
  !         !        Graphics g = getGraphics();
  !         !        g.drawString( "keyTyped: "+ event.getKeyChar(), 50, 50);
  !         }


ขั้นตอนที่ 6 ใหนักศึกษาทดลอง Compile อีกครั้ง
ผลของ keyPressed ............................................................................................................................................................
ผลของ keyReleased ..........................................................................................................................................................
ผลของ keyTyped ...............................................................................................................................................................

การใชงาน MouseListener และ ActionListener
ตัวอยางหนาจอการทำงาน (http://202.44.47.108/~ssc/mouse-keyboard/testKeyAndAction.html)




testKeyAndAction.html
  <html>
  !     <body>
          <h1>Test Keyboard and ActionListener</h1>
          <h3>Create by: 5366261111 Supaporn Simcharoen IT1-RC</h3>
  !     !     <applet code="testKeyAndAction.class" height="240" width="400">
  !     !     </applet>
  !     </body>
  </html>



                                                                                                                                                                Page 4 of 7
testKeyAndAction.java
ขั้นตอนที่ 1 สราง Class พรอมกับ import package ที่ตองใช และสราง Method ที่นาจะมี
  import java.awt.*;
  import java.awt.event.*;
  import javax.swing.*;

  public class testKeyAndAction extends JApplet implements ActionListener,MouseListener {
  !     public void init () {
  !     }
  !     public void paint(Graphics g) {
  !     }
  }


ทำไมตอง implements ActionListener ..................................................................................................................................
ทำไมตอง implements MouseListener .................................................................................................................................
ขั้นตอนที่ 2 ใหนักศึกษาทดลอง Compile
ผลการ Compile ...................................................................................................................................................................
ขั้นตอนที่ 3 ใหนักศึกษาเพิ่ม Method ตางๆ ตอไปนี้
  !         public       void    mousePressed(MouseEvent event) { }
  !         public       void    mouseReleased(MouseEvent event) { }
  !         public       void    mouseClicked(MouseEvent event) { }
  !         public       void    mouseEntered( MouseEvent event ) { }
  !         public       void    mouseExited(MouseEvent event) { }


ขั้นตอนที่ 4 ใหนักศึกษาทดลอง Compile อีกครั้ง
ผลการ Compile ...................................................................................................................................................................
ขั้นตอนที่ 5 ใหนักศึกษาเพิ่ม Method นี้
  !         public void actionPerformed(ActionEvent e) { }


ขั้นตอนที่ 6 ใหนักศึกษาทดลอง Compile อีกครั้ง
ผลการ Compile ...................................................................................................................................................................
ขั้นตอนที่ 7 ใหนักศึกษาเพิ่มการทำงานในสวนตางๆ ดังนี้
ประกาศคาตางๆ
  !         int x1, y1, x2,y2;
  !         JButton btnLine, btnRect, btnCircle, btnClear;
  !         JTextField text1, text2;
  !         Color oldColor1, oldColor2;
  !         char typeShape = 'L';


สรางคาเริ่มตน
  !         public void init () {
  !         !        Container c = getContentPane();
  !         !        c.setLayout( new FlowLayout() );

  !         !               btnLine = new JButton("Line");
  !         !               btnLine.addActionListener( this );
  !         !               c.add(btnLine);

  !         !               btnRect = new JButton("Rectangle");
  !         !               btnRect.addActionListener( this );
  !         !               c.add(btnRect);

  !         !               btnCircle = new JButton("Circle");
  !         !               btnCircle.addActionListener( this );
  !         !               c.add(btnCircle);

  !         !               btnClear = new JButton("Clear");
  !         !               btnClear.addActionListener( this );
  !         !               c.add(btnClear);
  !         !               text1 = new JTextField(12);
  !         !               text1.setEditable( false );
  !         !               c.add( text1 );

  !         !               text2 = new JTextField(12);
  !         !               text2.setEditable( false );
  !         !               c.add( text2 );

                                                                                                                                                                 Page 5 of 7
!          !        addMouseListener(this);
  !          }
  !          public void paint(Graphics g) {
  !          !        super.paint(g);
  !          !        switch (typeShape) {
  !          !        !       case 'L' :
  !          !        !       !      g.drawLine(x1, y1, x2, y2);
  !          !        !       !      break;
  !          !        !       case 'R' :
  !          !        !       !      g.drawRect(x1, y1, x2-x1, y2-y1);
  !          !        !       !      break;
  !          !        !       case 'C' :
  !          !        !       !      g.drawOval(x1, y1, x2-x1, y2-y1);
  !          !        !       !      break;
  !          !        }
  !          }


สรางในสวนของการรองรับการทำงานตางๆ ของ Mouse
  !          public       void mousePressed(MouseEvent event) {
  !          !              if (event.getButton() == 1) { // Mouse Left
  !          !              !       x1 = event.getX();
  !          !              !       y1 = event.getY();
  !          !              !       text1.setText("(X1 = "+x1+",Y1 = "+y1+")" );
  !          !              }
  !          !              else if (event.getButton() == 3) { // Mouse Right
  !          !              !       x2 = event.getX();
  !          !              !       y2 = event.getY();
  !          !              !       text2.setText(" (X2 = "+x2+",Y2 = "+y2+")" );
  !          !              }
  !          }
  !          public       void mouseReleased(MouseEvent event) { }
  !          public       void mouseClicked(MouseEvent event) { }
  !          public       void mouseEntered( MouseEvent event ) {
  !          !              repaint();
  !          }
  !          public       void mouseExited(MouseEvent event) {
  !          !              repaint();
  !          }


สรางในสวนของการรองรับการทำงานตางๆ ของ Action (การคลิกที่ปุมตางๆ)
  !          public void actionPerformed(ActionEvent e) {
  !          !        if (e.getSource() == btnLine) typeShape = 'L';
  !          !        else if (e.getSource() == btnRect) typeShape = 'R';
  !          !        else if (e.getSource() == btnCircle) typeShape = 'C';
  !          !        else if (e.getSource() == btnClear) {
  !          !        !       x1 = y1 = x2 = y2 = 0;
  !          !        !       text1.setText("");
  !          !        !       text2.setText("");
  !          !        }
  !          !        repaint();
  !          }


ขั้นตอนที่ 8 ใหนักศึกษาทดลอง Compile อีกครั้ง
ผลของการคลิกที่ปุมตางๆ
.............................................................................................................................................................................................
.............................................................................................................................................................................................
.............................................................................................................................................................................................
.............................................................................................................................................................................................
ผลของ ​KeyPressed
.............................................................................................................................................................................................
.............................................................................................................................................................................................




                                                                                                                                                                           Page 6 of 7
การบาน
1) ใหนักศึกษานำการทำงานของ Class testMouse และ Class testKeyboard มารวมกัน โดยใหชื่อ Class ใหมวา
   testMouseKeyboard โดยใหเขาไปดูตัวอยางไดที่
Q         http://202.44.47.108/~ssc/mouse-keyboard-hw/testMouseKeyboard.html
2) ใหอธิบายการทำงานของ Code ขางลางนี้ พรอมทั้งเขียนผลของการทำงาน
 import java.awt.event.*;
 import java.awt.*;
 import javax.swing.*;

 public class hw2 extends JApplet implements ActionListener, MouseListener, MouseMotionListener{
 !     private int lastx, lasty;
 !     private JButton redBtn, greenBtn, blueBtn,clearBtn;
 !     private Graphics g;
 !     private Color color;
 !
 !     public void init() {
 !     !        Container c = getContentPane();
 !     !        c.setLayout(new FlowLayout());
 !     !
 !     !        redBtn = new JButton("Red");
 !     !        redBtn.addActionListener(this);
 !     !        c.add(redBtn);
 !     !        greenBtn = new JButton("Green");
 !     !        greenBtn.addActionListener(this);
 !     !        c.add(greenBtn);
 !     !        blueBtn = new JButton("Blue");
 !     !        blueBtn.addActionListener(this);
 !     !        c.add(blueBtn);
 !     !        clearBtn = new JButton("Clear");
 !     !        clearBtn.addActionListener(this);
 !     !        c.add(clearBtn);
 !     !
 !     !        g = getGraphics();
 !     !        color = Color.BLACK;
 !     !
 !     !        addMouseListener(this);
 !     !        addMouseMotionListener(this);
 !     }
 !     public void paint(Graphics g) {
 !     !        super.paint(g);
 !     }
 !     public void mousePressed(MouseEvent event) {
 !     !        lastx = event.getX();
 !     !        lasty = event.getY();
 !     }
 !     public void mouseReleased(MouseEvent event) { }
 !     public void mouseClicked(MouseEvent event) { }
 !     public void mouseEntered( MouseEvent event ) { }
 !     public void mouseExited(MouseEvent event) { }
 !     public void mouseDragged(MouseEvent event) {
 !     !        int x = event.getX();
 !     !        int y = event.getY();
 !     !        g.setColor( color );
 !     !        g.drawLine(lastx, lasty, x, y);
 !     !        lastx = x;
 !     !        lasty = y;
 !     }
 !     public void mouseMoved(MouseEvent event) {
 !     !        showStatus(event.getX() + ", " + event.getY() );
 !     }
 !     public void actionPerformed(ActionEvent e) {
 !     !        if (e.getSource() == redBtn) color = Color.RED;
 !     !        else if (e.getSource() == greenBtn) color = Color.GREEN;
 !     !        else if (e.getSource() == blueBtn) color = Color.BLUE;
 !     !        else if (e.getSource() == clearBtn) {
 !     !        !       color = Color.BLACK;
 !     !        !       clear();
 !     !        }
 !     }
 !     public void clear() {
 !     !        repaint();
 !     !        g.setColor(this.getBackground());
 !     !        g.fillRect(0, 0, bounds().width, bounds().height );
 !     }
 }

3) MouseMotionListener เปน Class ที่ประกอบดวย Method อะไรบาง
4)ใหศึกษาวิธีการนำไฟลรูปภาพเขาไปไวที่ server ดวย ftp
                                                                                                       Page 7 of 7

More Related Content

PDF
Applet 5 class_inheritance
PDF
กิจกรรมพื้นหลังและตัวละคร
PDF
Applet 7 image_j_panel
PDF
New Assingment3 array2D
PDF
Applet 7 image_j_panel
PDF
Applet 5 class_inheritance
PDF
662305 LAB13
Applet 5 class_inheritance
กิจกรรมพื้นหลังและตัวละคร
Applet 7 image_j_panel
New Assingment3 array2D
Applet 7 image_j_panel
Applet 5 class_inheritance
662305 LAB13

Similar to Applet 6 mouse_keyboard (17)

PDF
Basic Event Listener
PDF
PDF
Java-Chapter 14 Creating Graphics with DWindow
PPT
Mobile Game and Application with J2ME
PDF
662305 Lab7new
PDF
Java Programming: การจัดการกับเหตุการณ์กราฟิก
PDF
2D Graphics and Animations in Java World
PPT
Mobile Game and Application with J2ME
PPT
Mobile Game and Application with J2ME
PPT
Java Programming [5/12] : Build Graphical User Interface
PPT
J2ME Game Concept
PDF
662327 08-2-53 [โหมดความเข้ากันได้]
PDF
PDF
Java Programming: การสร้างส่วนต่อประสานกราฟิกกับผู้ใช้ (Java GUI)
PDF
Java-Chapter 13 Advanced Classes and Objects
PPT
๋Java Programming [7/12] : GUI Event Handling
PDF
Java-Chapter 02 Data Operations and Processing
Basic Event Listener
Java-Chapter 14 Creating Graphics with DWindow
Mobile Game and Application with J2ME
662305 Lab7new
Java Programming: การจัดการกับเหตุการณ์กราฟิก
2D Graphics and Animations in Java World
Mobile Game and Application with J2ME
Mobile Game and Application with J2ME
Java Programming [5/12] : Build Graphical User Interface
J2ME Game Concept
662327 08-2-53 [โหมดความเข้ากันได้]
Java Programming: การสร้างส่วนต่อประสานกราฟิกกับผู้ใช้ (Java GUI)
Java-Chapter 13 Advanced Classes and Objects
๋Java Programming [7/12] : GUI Event Handling
Java-Chapter 02 Data Operations and Processing
Ad

More from Nitigan Nakjuatong (16)

PDF
วิธีการกำหนดสิทธิให้กับ Directory
PDF
662305 LAB12
PDF
Applet 4 class_composition
PDF
PDF
PDF
PDF
Applet 3 design_class_composition
PDF
Applet 2 container and action_listener
PDF
Assingment3 array2 d
PDF
PPT
PPT
Method part2
PPT
Control structure
PPT
Method JAVA
PPT
Set putty to use numeric keyboard in pico
PDF
Putty basic setting
วิธีการกำหนดสิทธิให้กับ Directory
662305 LAB12
Applet 4 class_composition
Applet 3 design_class_composition
Applet 2 container and action_listener
Assingment3 array2 d
Method part2
Control structure
Method JAVA
Set putty to use numeric keyboard in pico
Putty basic setting
Ad

Applet 6 mouse_keyboard

  • 1. การใชงาน Mouse และ Keyboard ssc การใชงาน MouseListener และ MouseEvent ในการตรวจสอบการคลิกเมาสในตำแหนงของวัตถุที่ตองการ MouseEvent ประกอบดวย 5 Method ดังนี้ 1) mousePressed ......................................................................................................................................................... 2) mouseReleased ......................................................................................................................................................... 3) mouseClicked ......................................................................................................................................................... 4) mouseEntered ......................................................................................................................................................... 5) mouseExited ......................................................................................................................................................... ตัวอยางหนาจอการทำงาน (http://202.44.47.108/~ssc/mouse-keyboard/testMouse.html) testMouse.html <html> ! <body> <h1>Test Mouse</h1> <h3>Create by: 5366261111 Supaporn Simcharoen IT1-RC</h3> ! ! <applet code="testMouse.class" height="240" width="320"> ! ! </applet> ! </body> </html> testMouse.java ขั้นตอนที่ 1 สราง Class พรอมกับ import package ที่ตองใช และสราง Method ที่นาจะมี import java.awt.*; import java.awt.event.*; import javax.swing.*; public class testMouse extends JApplet implements MouseListener { ! public void init () { ! } ! public void paint(Graphics g) { ! } } ทำไมตอง implements MouseListener ................................................................................................................................. ขั้นตอนที่ 2 ใหนักศึกษาทดลอง Compile ผลการ Compile ................................................................................................................................................................... ขั้นตอนที่ 3 ใหนักศึกษาเพิ่ม Method ตางๆ ตอไปนี้ ! public void mousePressed(MouseEvent event) { } ! public void mouseReleased(MouseEvent event) { } ! public void mouseClicked(MouseEvent event) { } ! public void mouseEntered( MouseEvent event ) { } ! public void mouseExited(MouseEvent event) { } ขั้นตอนที่ 4 ใหนักศึกษาทดลอง Compile อีกครั้ง ผลการ Compile ................................................................................................................................................................... Page 1 of 7
  • 2. ขั้นตอนที่ 5 ใหนักศึกษาเพิ่มการทำงานในสวนตางๆ ดังนี้ ประกาศคาตางๆ ! int x, y, size; ! Color bgcolor, fgcolor; ! boolean isDraw = true; สรางคาเริ่มตน ! public void init () { ! ! x = 140; ! ! y = 100; ! ! size = 50; ! ! ! ! fgcolor = Color.BLUE; addMouseListener(this); Note :: ..................................................................... ! } ! public void paint(Graphics g) { ! ! super.paint(g); ! ! if (isDraw == true) { ! ! ! g.setColor(fgcolor); ! ! ! g.fillRect( x, y, size , size); ! ! ! g.setColor( Color.BLACK ); ! ! ! g.drawRect( x, y, size, size); ! ! } ! } Note :: ..................................................................... สรางในสวนของการรองรับการทำงานตางๆ ของ Mouse ! public void mousePressed(MouseEvent event) { ! ! Graphics g = getGraphics(); ! ! g.drawString("("+event.getX()+","+event.getY()+")",event.getX(), event.getY()); ! } ! public void mouseReleased(MouseEvent event) { ! ! ! } repaint(); Note :: ..................................................................... ! public void mouseClicked(MouseEvent event) { ! ! boolean flag = isInside(x,y,size,event.getX(),event.getY()); ! ! if (isDraw == true) { ! ! ! if (flag == true) isDraw = !isDraw; ! ! } ! ! else { ! ! ! x = event.getX(); ! ! ! y = event.getY(); ! ! ! isDraw = !isDraw; ! ! } ! } ! public void mouseEntered( MouseEvent event ) { ! ! repaint(); ! } ! public void mouseExited(MouseEvent event) { ! ! repaint(); ! } ! boolean isInside(int x1,int y1,int size,int posx,int posy) { ! ! int x2 = x1 + size; ! ! int y2 = y1 + size; ! ! if (posx >= x1 && posx <= x2) ! ! ! if (posy >= y1 && posy <= y2) return true; ! ! return false; ! } ขั้นตอนที่ 6 ใหนักศึกษาทดลอง Compile อีกครั้ง ผลของ mousePressed ....................................................................................................................................................... ผลของ mouseReleased ..................................................................................................................................................... ผลของ mouseClicked ........................................................................................................................................................ ผลของ mouseEntered ....................................................................................................................................................... ผลของ mouseExited .......................................................................................................................................................... Page 2 of 7
  • 3. การใชงาน KeyListener และ KeyEvent ในการตรวจสอบการกดปุมที่ Keyboard KeyEvent ประกอบดวย 3 Method ดังนี้ 1) keyPressed .............................................................................................................................................................. 2) keyReleased .............................................................................................................................................................. 3) keyTyped .............................................................................................................................................................. ตัวอยางหนาจอการทำงาน (http://202.44.47.108/~ssc/mouse-keyboard/testKeyboard.html) testKeyboard.html <html> ! <body> <h1>Test Keyboard</h1> <h3>Create by: 5366261111 Supaporn Simcharoen IT1-RC</h3> ! ! <applet code="testKeyboard.class" height="240" width="320"> ! ! </applet> ! </body> </html> testKeyboard.java ขั้นตอนที่ 1 สราง Class พรอมกับ import package ที่ตองใช และสราง Method ที่นาจะมี import java.awt.*; import java.awt.event.*; import javax.swing.*; public class testKeyboard extends JApplet implements KeyListener { ! public void init () { ! } ! public void paint(Graphics g) { ! } } ทำไมตอง implements KeyListener ...................................................................................................................................... ขั้นตอนที่ 2 ใหนักศึกษาทดลอง Compile ผลการ Compile ................................................................................................................................................................... ขั้นตอนที่ 3 ใหนักศึกษาเพิ่ม Method ตางๆ ตอไปนี้ ! public void keyPressed(KeyEvent event) { } ! public void keyReleased(KeyEvent event) { } ! public void keyTyped(KeyEvent event) { } ขั้นตอนที่ 4 ใหนักศึกษาทดลอง Compile อีกครั้ง ผลการ Compile ................................................................................................................................................................... ขั้นตอนที่ 5 ใหนักศึกษาเพิ่มการทำงานในสวนตางๆ ดังนี้ ประกาศคาตางๆ ! private int x, y, size; ! Color fgcolor; ! private char typeShape = 'R'; Page 3 of 7
  • 4. สรางคาเริ่มตน ! public void init () { ! ! x = 140; ! ! y = 100; ! ! size = 50; ! ! fgcolor = Color.BLUE; ! ! addKeyListener( this ); ! } ! public void paint(Graphics g) { ! ! super.paint(g); ! ! if (typeShape == 'R') { ! ! ! g.setColor(fgcolor); ! ! ! g.fillRect( x, y, size , size); ! ! ! g.setColor( Color.BLACK ); ! ! ! g.drawRect( x, y, size, size); ! ! } ! ! else { ! ! ! g.setColor(fgcolor); ! ! ! g.fillOval( x, y, size , size); ! ! ! g.setColor( Color.BLACK ); ! ! ! g.drawOval( x, y, size, size); ! ! } ! } สรางในสวนของการรองรับการทำงานตางๆ ของ Keyboard ! public void keyPressed(KeyEvent event) { ! ! if ( event.getKeyChar() == 'c') typeShape = 'C'; ! ! if ( event.getKeyChar() == 'r') typeShape = 'R'; ! } ! public void keyReleased(KeyEvent event) { ! ! repaint(); Note :: ..................................................................... ! } ! public void keyTyped(KeyEvent event) { ! ! Graphics g = getGraphics(); ! ! g.drawString( "keyTyped: "+ event.getKeyChar(), 50, 50); ! } ขั้นตอนที่ 6 ใหนักศึกษาทดลอง Compile อีกครั้ง ผลของ keyPressed ............................................................................................................................................................ ผลของ keyReleased .......................................................................................................................................................... ผลของ keyTyped ............................................................................................................................................................... การใชงาน MouseListener และ ActionListener ตัวอยางหนาจอการทำงาน (http://202.44.47.108/~ssc/mouse-keyboard/testKeyAndAction.html) testKeyAndAction.html <html> ! <body> <h1>Test Keyboard and ActionListener</h1> <h3>Create by: 5366261111 Supaporn Simcharoen IT1-RC</h3> ! ! <applet code="testKeyAndAction.class" height="240" width="400"> ! ! </applet> ! </body> </html> Page 4 of 7
  • 5. testKeyAndAction.java ขั้นตอนที่ 1 สราง Class พรอมกับ import package ที่ตองใช และสราง Method ที่นาจะมี import java.awt.*; import java.awt.event.*; import javax.swing.*; public class testKeyAndAction extends JApplet implements ActionListener,MouseListener { ! public void init () { ! } ! public void paint(Graphics g) { ! } } ทำไมตอง implements ActionListener .................................................................................................................................. ทำไมตอง implements MouseListener ................................................................................................................................. ขั้นตอนที่ 2 ใหนักศึกษาทดลอง Compile ผลการ Compile ................................................................................................................................................................... ขั้นตอนที่ 3 ใหนักศึกษาเพิ่ม Method ตางๆ ตอไปนี้ ! public void mousePressed(MouseEvent event) { } ! public void mouseReleased(MouseEvent event) { } ! public void mouseClicked(MouseEvent event) { } ! public void mouseEntered( MouseEvent event ) { } ! public void mouseExited(MouseEvent event) { } ขั้นตอนที่ 4 ใหนักศึกษาทดลอง Compile อีกครั้ง ผลการ Compile ................................................................................................................................................................... ขั้นตอนที่ 5 ใหนักศึกษาเพิ่ม Method นี้ ! public void actionPerformed(ActionEvent e) { } ขั้นตอนที่ 6 ใหนักศึกษาทดลอง Compile อีกครั้ง ผลการ Compile ................................................................................................................................................................... ขั้นตอนที่ 7 ใหนักศึกษาเพิ่มการทำงานในสวนตางๆ ดังนี้ ประกาศคาตางๆ ! int x1, y1, x2,y2; ! JButton btnLine, btnRect, btnCircle, btnClear; ! JTextField text1, text2; ! Color oldColor1, oldColor2; ! char typeShape = 'L'; สรางคาเริ่มตน ! public void init () { ! ! Container c = getContentPane(); ! ! c.setLayout( new FlowLayout() ); ! ! btnLine = new JButton("Line"); ! ! btnLine.addActionListener( this ); ! ! c.add(btnLine); ! ! btnRect = new JButton("Rectangle"); ! ! btnRect.addActionListener( this ); ! ! c.add(btnRect); ! ! btnCircle = new JButton("Circle"); ! ! btnCircle.addActionListener( this ); ! ! c.add(btnCircle); ! ! btnClear = new JButton("Clear"); ! ! btnClear.addActionListener( this ); ! ! c.add(btnClear); ! ! text1 = new JTextField(12); ! ! text1.setEditable( false ); ! ! c.add( text1 ); ! ! text2 = new JTextField(12); ! ! text2.setEditable( false ); ! ! c.add( text2 ); Page 5 of 7
  • 6. ! ! addMouseListener(this); ! } ! public void paint(Graphics g) { ! ! super.paint(g); ! ! switch (typeShape) { ! ! ! case 'L' : ! ! ! ! g.drawLine(x1, y1, x2, y2); ! ! ! ! break; ! ! ! case 'R' : ! ! ! ! g.drawRect(x1, y1, x2-x1, y2-y1); ! ! ! ! break; ! ! ! case 'C' : ! ! ! ! g.drawOval(x1, y1, x2-x1, y2-y1); ! ! ! ! break; ! ! } ! } สรางในสวนของการรองรับการทำงานตางๆ ของ Mouse ! public void mousePressed(MouseEvent event) { ! ! if (event.getButton() == 1) { // Mouse Left ! ! ! x1 = event.getX(); ! ! ! y1 = event.getY(); ! ! ! text1.setText("(X1 = "+x1+",Y1 = "+y1+")" ); ! ! } ! ! else if (event.getButton() == 3) { // Mouse Right ! ! ! x2 = event.getX(); ! ! ! y2 = event.getY(); ! ! ! text2.setText(" (X2 = "+x2+",Y2 = "+y2+")" ); ! ! } ! } ! public void mouseReleased(MouseEvent event) { } ! public void mouseClicked(MouseEvent event) { } ! public void mouseEntered( MouseEvent event ) { ! ! repaint(); ! } ! public void mouseExited(MouseEvent event) { ! ! repaint(); ! } สรางในสวนของการรองรับการทำงานตางๆ ของ Action (การคลิกที่ปุมตางๆ) ! public void actionPerformed(ActionEvent e) { ! ! if (e.getSource() == btnLine) typeShape = 'L'; ! ! else if (e.getSource() == btnRect) typeShape = 'R'; ! ! else if (e.getSource() == btnCircle) typeShape = 'C'; ! ! else if (e.getSource() == btnClear) { ! ! ! x1 = y1 = x2 = y2 = 0; ! ! ! text1.setText(""); ! ! ! text2.setText(""); ! ! } ! ! repaint(); ! } ขั้นตอนที่ 8 ใหนักศึกษาทดลอง Compile อีกครั้ง ผลของการคลิกที่ปุมตางๆ ............................................................................................................................................................................................. ............................................................................................................................................................................................. ............................................................................................................................................................................................. ............................................................................................................................................................................................. ผลของ ​KeyPressed ............................................................................................................................................................................................. ............................................................................................................................................................................................. Page 6 of 7
  • 7. การบาน 1) ใหนักศึกษานำการทำงานของ Class testMouse และ Class testKeyboard มารวมกัน โดยใหชื่อ Class ใหมวา testMouseKeyboard โดยใหเขาไปดูตัวอยางไดที่ Q http://202.44.47.108/~ssc/mouse-keyboard-hw/testMouseKeyboard.html 2) ใหอธิบายการทำงานของ Code ขางลางนี้ พรอมทั้งเขียนผลของการทำงาน import java.awt.event.*; import java.awt.*; import javax.swing.*; public class hw2 extends JApplet implements ActionListener, MouseListener, MouseMotionListener{ ! private int lastx, lasty; ! private JButton redBtn, greenBtn, blueBtn,clearBtn; ! private Graphics g; ! private Color color; ! ! public void init() { ! ! Container c = getContentPane(); ! ! c.setLayout(new FlowLayout()); ! ! ! ! redBtn = new JButton("Red"); ! ! redBtn.addActionListener(this); ! ! c.add(redBtn); ! ! greenBtn = new JButton("Green"); ! ! greenBtn.addActionListener(this); ! ! c.add(greenBtn); ! ! blueBtn = new JButton("Blue"); ! ! blueBtn.addActionListener(this); ! ! c.add(blueBtn); ! ! clearBtn = new JButton("Clear"); ! ! clearBtn.addActionListener(this); ! ! c.add(clearBtn); ! ! ! ! g = getGraphics(); ! ! color = Color.BLACK; ! ! ! ! addMouseListener(this); ! ! addMouseMotionListener(this); ! } ! public void paint(Graphics g) { ! ! super.paint(g); ! } ! public void mousePressed(MouseEvent event) { ! ! lastx = event.getX(); ! ! lasty = event.getY(); ! } ! public void mouseReleased(MouseEvent event) { } ! public void mouseClicked(MouseEvent event) { } ! public void mouseEntered( MouseEvent event ) { } ! public void mouseExited(MouseEvent event) { } ! public void mouseDragged(MouseEvent event) { ! ! int x = event.getX(); ! ! int y = event.getY(); ! ! g.setColor( color ); ! ! g.drawLine(lastx, lasty, x, y); ! ! lastx = x; ! ! lasty = y; ! } ! public void mouseMoved(MouseEvent event) { ! ! showStatus(event.getX() + ", " + event.getY() ); ! } ! public void actionPerformed(ActionEvent e) { ! ! if (e.getSource() == redBtn) color = Color.RED; ! ! else if (e.getSource() == greenBtn) color = Color.GREEN; ! ! else if (e.getSource() == blueBtn) color = Color.BLUE; ! ! else if (e.getSource() == clearBtn) { ! ! ! color = Color.BLACK; ! ! ! clear(); ! ! } ! } ! public void clear() { ! ! repaint(); ! ! g.setColor(this.getBackground()); ! ! g.fillRect(0, 0, bounds().width, bounds().height ); ! } } 3) MouseMotionListener เปน Class ที่ประกอบดวย Method อะไรบาง 4)ใหศึกษาวิธีการนำไฟลรูปภาพเขาไปไวที่ server ดวย ftp Page 7 of 7