Submit Search
Applet 6 mouse_keyboard
0 likes
337 views
Nitigan Nakjuatong
mouse_keyboard
Read more
1 of 7
Download now
Downloaded 18 times
1
2
3
4
5
6
7
More Related Content
PDF
Applet 5 class_inheritance
Nitigan Nakjuatong
PDF
กิจกรรมพื้นหลังและตัวละคร
Munmuang Tik
PDF
Applet 7 image_j_panel
Nitigan Nakjuatong
PDF
New Assingment3 array2D
Nitigan Nakjuatong
PDF
Applet 7 image_j_panel
Nitigan Nakjuatong
PPT
Array
Nitigan Nakjuatong
PDF
Applet 5 class_inheritance
Nitigan Nakjuatong
PDF
662305 LAB13
Nitigan Nakjuatong
Applet 5 class_inheritance
Nitigan Nakjuatong
กิจกรรมพื้นหลังและตัวละคร
Munmuang Tik
Applet 7 image_j_panel
Nitigan Nakjuatong
New Assingment3 array2D
Nitigan Nakjuatong
Applet 7 image_j_panel
Nitigan Nakjuatong
Array
Nitigan Nakjuatong
Applet 5 class_inheritance
Nitigan Nakjuatong
662305 LAB13
Nitigan Nakjuatong
Similar to Applet 6 mouse_keyboard
(17)
PDF
Basic Event Listener
kunemata
PDF
662305 08
Nitigan Nakjuatong
PDF
Java-Chapter 14 Creating Graphics with DWindow
Wongyos Keardsri
PPT
Mobile Gameand Application withJ2ME
Jenchoke Tachagomain
PDF
662305 Lab7new
Nitigan Nakjuatong
PDF
Java Programming: การจัดการกับเหตุการณ์กราฟิก
Thanachart Numnonda
PDF
2D Graphics and Animations in Java World
kunemata
PPT
Mobile Gameand Application with J2ME
Jenchoke Tachagomain
PPT
Mobile Gameand Application withJ2ME
Jenchoke Tachagomain
PPT
Java Programming [5/12] : Build Graphical User Interface
IMC Institute
PPT
J2ME Game Concept
Jenchoke Tachagomain
PDF
662327 08-2-53 [โหมดความเข้ากันได้]
Deity Toon
PDF
Java AWT
Thanachart Numnonda
PDF
Java Programming: การสร้างส่วนต่อประสานกราฟิกกับผู้ใช้ (Java GUI)
Thanachart Numnonda
PDF
Java-Chapter 13 Advanced Classes and Objects
Wongyos Keardsri
PPT
๋Java Programming [7/12] : GUI Event Handling
IMC Institute
PDF
Java-Chapter 02 Data Operations and Processing
Wongyos Keardsri
Basic Event Listener
kunemata
662305 08
Nitigan Nakjuatong
Java-Chapter 14 Creating Graphics with DWindow
Wongyos Keardsri
Mobile Gameand Application withJ2ME
Jenchoke Tachagomain
662305 Lab7new
Nitigan Nakjuatong
Java Programming: การจัดการกับเหตุการณ์กราฟิก
Thanachart Numnonda
2D Graphics and Animations in Java World
kunemata
Mobile Gameand Application with J2ME
Jenchoke Tachagomain
Mobile Gameand Application withJ2ME
Jenchoke Tachagomain
Java Programming [5/12] : Build Graphical User Interface
IMC Institute
J2ME Game Concept
Jenchoke Tachagomain
662327 08-2-53 [โหมดความเข้ากันได้]
Deity Toon
Java AWT
Thanachart Numnonda
Java Programming: การสร้างส่วนต่อประสานกราฟิกกับผู้ใช้ (Java GUI)
Thanachart Numnonda
Java-Chapter 13 Advanced Classes and Objects
Wongyos Keardsri
๋Java Programming [7/12] : GUI Event Handling
IMC Institute
Java-Chapter 02 Data Operations and Processing
Wongyos Keardsri
Ad
More from Nitigan Nakjuatong
(16)
PDF
วิธีการกำหนดสิทธิให้กับ Directory
Nitigan Nakjuatong
PDF
662305 LAB12
Nitigan Nakjuatong
PDF
Applet 4 class_composition
Nitigan Nakjuatong
PDF
662305 11
Nitigan Nakjuatong
PDF
662305 10
Nitigan Nakjuatong
PDF
662305 09
Nitigan Nakjuatong
PDF
Applet 3 design_class_composition
Nitigan Nakjuatong
PDF
Applet 2 container and action_listener
Nitigan Nakjuatong
PDF
Assingment3 array2 d
Nitigan Nakjuatong
PDF
Lab 6 new
Nitigan Nakjuatong
PPT
Array2D
Nitigan Nakjuatong
PPT
Method part2
Nitigan Nakjuatong
PPT
Control structure
Nitigan Nakjuatong
PPT
Method JAVA
Nitigan Nakjuatong
PPT
Set putty to use numeric keyboard in pico
Nitigan Nakjuatong
PDF
Putty basic setting
Nitigan Nakjuatong
วิธีการกำหนดสิทธิให้กับ Directory
Nitigan Nakjuatong
662305 LAB12
Nitigan Nakjuatong
Applet 4 class_composition
Nitigan Nakjuatong
662305 11
Nitigan Nakjuatong
662305 10
Nitigan Nakjuatong
662305 09
Nitigan Nakjuatong
Applet 3 design_class_composition
Nitigan Nakjuatong
Applet 2 container and action_listener
Nitigan Nakjuatong
Assingment3 array2 d
Nitigan Nakjuatong
Lab 6 new
Nitigan Nakjuatong
Array2D
Nitigan Nakjuatong
Method part2
Nitigan Nakjuatong
Control structure
Nitigan Nakjuatong
Method JAVA
Nitigan Nakjuatong
Set putty to use numeric keyboard in pico
Nitigan Nakjuatong
Putty basic setting
Nitigan Nakjuatong
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
Download