SlideShare ist ein Scribd-Unternehmen logo
1 von 2
Downloaden Sie, um offline zu lesen
การใชงาน KeyListener กับ Timer                                                                                                                            ssc


พื้นฐานการใชงาน ImageIcon
ตัวอยางหนาจอการทำงาน (http://202.44.47.108/~ssc/Keyboard-Timer/testKeyTimer.html)




testImageIcon.html
 <html>
 !     <body>
         <h1>Test KeyListener and Timer</h1>
         <h3>Create by: 5366261111 Supaporn Simcharoen IT1-RC</h3>
 !     !     <applet code="testMouseKeyboard.class" height="400" width="500">
 !     !     </applet>
 !     </body>
 </html>

testImageIcon.java
 import    java.applet.*;
 import    java.awt.event.*;
 import    java.awt.*;
 import    javax.swing.*;

 public class testKeyTimer extends JApplet implements KeyListener,ActionListener {
 !     private int width, height;
 !     private JButton startBtn, stopBtn;
 !     private int xMin, xMax, yMin, yMax;
 !     private int x, y, size, xSpeed, ySpeed;


 !     Timer swTimer;
                                 Note :: ................................................................
 !
 !     public void init () {
 !     !
 !     !        Container c = getContentPane();
 !     !        c.setLayout(new FlowLayout());

 !     !          startBtn = new JButton("Play");
 !     !          startBtn.addActionListener(this);
 !     !          startBtn.addKeyListener(this);
 !     !          c.add(startBtn);

 !     !          stopBtn = new JButton("Stop");
 !     !          stopBtn.addActionListener(this);
 !     !          stopBtn.addKeyListener(this);
 !     !          c.add(stopBtn);

 !     !          width = 480; height = 320;
 !     !          xMin = 1; xMax = 478;
 !     !          yMin = 30; yMax = 319;
 !     !          xSpeed = 2; ySpeed = 2;
 !     !          x = 240; y = 160; size = 40;
 !     !

 !     !          swTimer = new Timer(10, this);
                                                                              Note :: ................................................................
 !     !          addKeyListener( this );
 !     }




                                                                                                                                             Page 1 of 2
!    public void paint(Graphics g) {
!    !        super.paint(g);

!    !         g.setColor( Color.BLACK );
!    !         g.drawRect( 4, 30, 471, 285);

!    !         g.setColor( Color.BLUE );
!    !         g.fillOval( x, y, size, size);
!    }

!    public void keyPressed(KeyEvent event) {
!    !        if ( event.getKeyChar() == 'p')
              {
!    !        !
              }
                      swTimer.start();
                                                Note :: ................................................................

!    !         else if ( event.getKeyChar() == 's')
               {
!    !         !      swTimer.stop();

!    }
               }                                Note :: ................................................................

!    public void keyReleased(KeyEvent event) { }
!    public void keyTyped(KeyEvent event) { }

!    public void actionPerformed(ActionEvent e) {
!    !        if (e.getSource() == startBtn) { swTimer.start(); }
!    !        else if (e.getSource() == stopBtn) { swTimer.stop(); }
!    !        else {
!    !        !       move();
!    !        !       repaint();
!    !        }
!    }

!    public void move() {
!    !        x = x + xSpeed;
!    !        y = y + ySpeed;
!    !        if (x < xMin) {
!    !        !       x = xMin;
!    !        !       xSpeed = -xSpeed;
!    !        }
!    !        else if (x+size > xMax) {
!    !        !       x = xMax - size;
!    !        !       xSpeed = -xSpeed;
!    !        }
!    !        if (y < yMin) {
!    !        !       y = yMin;
!    !        !       ySpeed = -ySpeed;
!    !        }
!    !        else if (y+size > yMax) {
!    !        !       y = yMax - size;
!    !        !       ySpeed = -ySpeed;
!    !        }
!    }

}//end class




                                                                                                                           Page 2 of 2

Weitere ähnliche Inhalte

Mehr von Nitigan Nakjuatong

Set putty to use numeric keyboard in pico
Set putty to use numeric keyboard in picoSet putty to use numeric keyboard in pico
Set putty to use numeric keyboard in pico
Nitigan Nakjuatong
 

Mehr von Nitigan Nakjuatong (18)

Applet 5 class_inheritance
Applet 5 class_inheritanceApplet 5 class_inheritance
Applet 5 class_inheritance
 
Applet 4 class_composition
Applet 4 class_compositionApplet 4 class_composition
Applet 4 class_composition
 
662305 11
662305 11662305 11
662305 11
 
662305 10
662305 10662305 10
662305 10
 
662305 09
662305 09662305 09
662305 09
 
Applet 3 design_class_composition
Applet 3 design_class_compositionApplet 3 design_class_composition
Applet 3 design_class_composition
 
662305 08
662305 08662305 08
662305 08
 
Applet 2 container and action_listener
Applet 2 container and action_listenerApplet 2 container and action_listener
Applet 2 container and action_listener
 
662305 Lab7new
662305 Lab7new662305 Lab7new
662305 Lab7new
 
New Assingment3 array2D
New Assingment3 array2DNew Assingment3 array2D
New Assingment3 array2D
 
Assingment3 array2 d
Assingment3 array2 dAssingment3 array2 d
Assingment3 array2 d
 
Lab 6 new
Lab 6 newLab 6 new
Lab 6 new
 
Array2D
Array2DArray2D
Array2D
 
Array
ArrayArray
Array
 
Method part2
Method part2Method part2
Method part2
 
Control structure
Control structureControl structure
Control structure
 
Set putty to use numeric keyboard in pico
Set putty to use numeric keyboard in picoSet putty to use numeric keyboard in pico
Set putty to use numeric keyboard in pico
 
Putty basic setting
Putty basic settingPutty basic setting
Putty basic setting
 

Applet 6 mouse_keyboard_เพิ่มเติม

  • 1. การใชงาน KeyListener กับ Timer ssc พื้นฐานการใชงาน ImageIcon ตัวอยางหนาจอการทำงาน (http://202.44.47.108/~ssc/Keyboard-Timer/testKeyTimer.html) testImageIcon.html <html> ! <body> <h1>Test KeyListener and Timer</h1> <h3>Create by: 5366261111 Supaporn Simcharoen IT1-RC</h3> ! ! <applet code="testMouseKeyboard.class" height="400" width="500"> ! ! </applet> ! </body> </html> testImageIcon.java import java.applet.*; import java.awt.event.*; import java.awt.*; import javax.swing.*; public class testKeyTimer extends JApplet implements KeyListener,ActionListener { ! private int width, height; ! private JButton startBtn, stopBtn; ! private int xMin, xMax, yMin, yMax; ! private int x, y, size, xSpeed, ySpeed; ! Timer swTimer; Note :: ................................................................ ! ! public void init () { ! ! ! ! Container c = getContentPane(); ! ! c.setLayout(new FlowLayout()); ! ! startBtn = new JButton("Play"); ! ! startBtn.addActionListener(this); ! ! startBtn.addKeyListener(this); ! ! c.add(startBtn); ! ! stopBtn = new JButton("Stop"); ! ! stopBtn.addActionListener(this); ! ! stopBtn.addKeyListener(this); ! ! c.add(stopBtn); ! ! width = 480; height = 320; ! ! xMin = 1; xMax = 478; ! ! yMin = 30; yMax = 319; ! ! xSpeed = 2; ySpeed = 2; ! ! x = 240; y = 160; size = 40; ! ! ! ! swTimer = new Timer(10, this); Note :: ................................................................ ! ! addKeyListener( this ); ! } Page 1 of 2
  • 2. ! public void paint(Graphics g) { ! ! super.paint(g); ! ! g.setColor( Color.BLACK ); ! ! g.drawRect( 4, 30, 471, 285); ! ! g.setColor( Color.BLUE ); ! ! g.fillOval( x, y, size, size); ! } ! public void keyPressed(KeyEvent event) { ! ! if ( event.getKeyChar() == 'p') { ! ! ! } swTimer.start(); Note :: ................................................................ ! ! else if ( event.getKeyChar() == 's') { ! ! ! swTimer.stop(); ! } } Note :: ................................................................ ! public void keyReleased(KeyEvent event) { } ! public void keyTyped(KeyEvent event) { } ! public void actionPerformed(ActionEvent e) { ! ! if (e.getSource() == startBtn) { swTimer.start(); } ! ! else if (e.getSource() == stopBtn) { swTimer.stop(); } ! ! else { ! ! ! move(); ! ! ! repaint(); ! ! } ! } ! public void move() { ! ! x = x + xSpeed; ! ! y = y + ySpeed; ! ! if (x < xMin) { ! ! ! x = xMin; ! ! ! xSpeed = -xSpeed; ! ! } ! ! else if (x+size > xMax) { ! ! ! x = xMax - size; ! ! ! xSpeed = -xSpeed; ! ! } ! ! if (y < yMin) { ! ! ! y = yMin; ! ! ! ySpeed = -ySpeed; ! ! } ! ! else if (y+size > yMax) { ! ! ! y = yMax - size; ! ! ! ySpeed = -ySpeed; ! ! } ! } }//end class Page 2 of 2